Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

System.Security.Cryptography.CryptographicException: Wrong Key Length

$
0
0
When I started using this plugin for encryption purpose: [Secured PlayerPrefs][1] Now after run, I was getting this exclamation message in console, so its not working as per my thinking. ![alt text][2] This is source code for encryption and decryption. static string Encrypt(string strPlain, string password) { if (!useSecure) return strPlain; try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, GetIV(), Iterations); byte[] key = rfc2898DeriveBytes.GetBytes(8); using (var memoryStream = new MemoryStream()) using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, GetIV()), CryptoStreamMode.Write)) { memoryStream.Write(GetIV(), 0, GetIV().Length); byte[] plainTextBytes = Encoding.UTF8.GetBytes(strPlain); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); return Convert.ToBase64String(memoryStream.ToArray()); } } catch (Exception e) { Debug.LogWarning("Encrypt Exception: " + e); return strPlain; } } static string Decrypt(string strEncript, string password) { if (!useSecure) return strEncript; try { byte[] cipherBytes = Convert.FromBase64String(strEncript); using (var memoryStream = new MemoryStream(cipherBytes)) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] iv = GetIV(); memoryStream.Read(iv, 0, iv.Length); // use derive bytes to generate key from password and IV var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, iv, Iterations); byte[] key = rfc2898DeriveBytes.GetBytes(8); using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read)) using (var streamReader = new StreamReader(cryptoStream)) { string strPlain = streamReader.ReadToEnd(); return strPlain; } } } catch (Exception e) { Debug.LogWarning("Decrypt Exception: " + e); return strEncript; } } Please share your suggestion for this. [1]: https://www.assetstore.unity3d.com/en/#!/content/32357 [2]: /storage/temp/81179-screen-shot-2016-10-30-at-35201-pm.png

Viewing all articles
Browse latest Browse all 1333

Trending Articles