- Home /
Windows Store App Encryption
Is any one knows an alternative to System.Security.Cryptography for Windows Store App ?
I am currently using RijndaelManaged,CipherMode classes and ICryptoTransform interface from this library.
Only alternative i could find was Windows.Security.Cryptography and this is not available in Mono or at least Unity(As name states i believe this library is Windows only).
My Code is something like that if it helps,
public static string Encrypt(string toEncrypt, Byte[] keyArray)
{
//byte[] keyArray = UTF8Encoding.UTF8.GetBytes("!'jsdflkj%+sklj;i#$99skdjksdjk");
// 256-AES key
try
{
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
using (RijndaelManaged rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.CBC;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
//rDel.Padding = PaddingMode.PKCS7;
// better lang support
ICryptoTransform cTransform = rDel.CreateEncryptor(keyArray, IVa);
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
cTransform.Dispose();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
}
catch (Exception ex)
{
UnityEngine.Debug.LogError(ex);
return null;
}
}
public static string Decrypt(string toDecrypt, Byte[] keyArray)
{
//byte[] keyArray = UTF8Encoding.UTF8.GetBytes("(kl4^+lsktýo1283746:#$iþsdsdsl");
// AES-256 key
try
{
byte[] toDecryptArray = Convert.FromBase64String(toDecrypt);
using (RijndaelManaged rDel = new RijndaelManaged())
{
rDel.Key = keyArray;
rDel.Mode = CipherMode.CBC;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
//rDel.Padding = PaddingMode.PKCS7;
// better lang support
ICryptoTransform cTransform = rDel.CreateDecryptor(keyArray,IVa);
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
cTransform.Dispose();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
catch //(Exception ex)
{
//UnityEngine.Debug.LogError(ex);
return null;
}
}
Answer by CanisLupus · Jun 22, 2014 at 04:35 PM
I don't know an exact equivalent of your code, but you CAN use classes in Windows.Security.Cryptography, even though they are not available in Mono. When you press "build" in Unity to start a Windows Store App build, Unity uses a .NET compiler on your Windows Kit folders (not the one it uses normally), and that compiler knows the classes and libraries in Metro apps (such as Windows.Security.Cryptography).
As for them existing in Windows only, that is true, but you can use #if defines in your scripts to handle code for different platforms:
#if NETFX_CORE
// code for Windows 8 here
#else
// code for other platforms
#endif
If what you are after is encrypting and decrypting a string, you can find several examples online in MSDN (using Windows.Security.Cryptography), although I cannot guarantee that it will be easy to find code equivalent to your existing one :( This is relevant if you want to be able to decrypt strings that were encrypted with your current code, for example.
If you want to encrypt and decrypt a string to/from a file, using authenticated encryption (the user cannot change the encrypted data), check this question and answer on StackOverflow. Note that it is certainly not equivalent to your current methods (even if only for the presence of authenticated encryption).
Once we will return back working on our Windows $$anonymous$$obile builds i will try definitely try this out.
If you have problems in the future, related to this question/answer, comment here. Note that Windows Phone builds will not activate NETFX_CORE, but only UNITY_WP8. Nevertheless, you shouldn't need specific code for Windows Phone, as problems with nonexistent classes/methods happen generally in Windows Store apps (which do not include Windows Phone, although the name might be confusing). But I'm not a Windows Phone developer, so take that with a grain of salt :)
Answer by B_Manx · Dec 08, 2013 at 04:43 PM
Don't use encryption. your code is easily decompiled in windows 8. write apps as though you are writing them for a browser which lets users easily decompile and see your code. so SSL certificate for communication is your best choice.
This is not for communication,this is to save local values such as Score etc... etc... I know libraries, apps can be decoded if you try hard enough. The point here is to prevent as much as people from cheating. So i would like to use Encryption of some kind on Windows Phones too.
I ran into the same thing. I'm encrypting a save file just to discourage cheating. If someone wants to decompile our save data format, they can go ahead and do it!
So anyone figure out what encryption classes we can use for the Windows Store?
Your answer
Follow this Question
Related Questions
Can't run game on Windows Metro becouse of error with dll's 1 Answer
Any way around a known issue when deploying to WP8? Number at start of project. 1 Answer
My game doesn't show up when searching for it by name on the Windows Phone Store 0 Answers
When I build Windows Phone version this error may Occur 0 Answers