- Home /
Bad PKCS7 While decrypting a file
I copied the code from a youtube tutorial, I don't understand anything from that, it will be really helpful if you could explain me how to fix it. To save the data I am using PlayerPrefs.SetString/GetString.
The error I am getting : CryptographicException: Bad PKCS7 padding. Invalid length 66. Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:363)
Thanks :)
The code I used for encrypt/decrypt.
public static string Encrypt(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] key = md5.ComputeHash(Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider() { Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform tr = trip.CreateEncryptor();
byte[] results = tr.TransformFinalBlock(data, 0, data.Length);
return Convert.ToBase64String(results, 0, results.Length);
}
}
}
public static string Decrypt(string input)
{
byte[] data = Convert.FromBase64String(input);
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
byte[] key = md5.ComputeHash(Encoding.UTF8.GetBytes(hash));
using (TripleDESCryptoServiceProvider trip = new TripleDESCryptoServiceProvider() { Key = key, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
{
ICryptoTransform tr = trip.CreateDecryptor();
byte[] results = tr.TransformFinalBlock(data, 0, data.Length);
return Encoding.UTF8.GetString(results);
}
}
}
Answer by kaarloew · Nov 26, 2018 at 08:03 AM
That code won't even compile. hash is not defined. And if tutorial is using TripleDES, you should just ignore the whole tutorial, since TripleDES is nowadays considered as "weak cipher". More modern ones are AES and ChaCha20.
I understand. Do you know any good encrypting youtube video using more modern methods? Or mabye a forum thread explaining in detail. Thank you
Your answer
Follow this Question
Related Questions
How to communicate between iOS and Unity3D? 0 Answers
How can I collect playerprefs values? 0 Answers
Visual Studio is not recognized by unity 1 Answer
How to manually edit player prefs file on android ? 1 Answer
How to save color 3 Answers