Hi guys!
I’m playing with encryption/decryption and having the same error while deciphering the message no matter what I’m doing (tried to change some code with what Google gives me for that problem, tried to switch to another ciphering algorithm etc), just the number {x} is different every time. Here’s my code;
public class EncryptionUtil {
private static int KEY_SIZE = 256;
private static string KEY = "OklrgxUOBGIILOOzy6NpT2BWjkPSwFDw";
private static byte[] KEY_BYTES = Encoding.UTF8.GetBytes(KEY);
public static string Encrypt(string input) {
using (Rijndael aes = Rijndael.Create()) {
aes.Key = KEY_BYTES;
aes.KeySize = KEY_SIZE;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.GenerateIV();
byte[] initializationVector = aes.IV;
using (var encrypter = aes.CreateEncryptor(aes.Key, aes.IV))
using (var cipherStream = new MemoryStream()) {
using (var tCryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var tBinaryWriter = new BinaryWriter(tCryptoStream)) {
cipherStream.Write(initializationVector, 0, initializationVector.Length);
tBinaryWriter.Write(input);
tCryptoStream.FlushFinalBlock();
}
byte[] result_b = cipherStream.ToArray();
string result = Convert.ToBase64String(result_b);
return result;
}
}
}
public static string Decrypt(string input) {
using (Rijndael aes = Rijndael.Create()) {
aes.Key = KEY_BYTES;
aes.KeySize = KEY_SIZE;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
byte[] inputBytes = Convert.FromBase64String(input);
using (var decipherStream = new MemoryStream(inputBytes)) {
byte[] initializationVector = new byte[16];
decipherStream.Read(initializationVector, 0, 16);
aes.IV = initializationVector;
using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) {
using (var cs = new CryptoStream(decipherStream, decryptor, CryptoStreamMode.Read)) {
using (var sr = new StreamReader(cs)) {
return sr.ReadToEnd();
}
}
}
}
}
}
}
Main thing that I’m writing init vector along with result string when encrypting. Then, when decrypting - I’m reading this IV first (first 16 bytes) then reading the rest of the encrypted message.
Here’s how I’m testing:
class EncryptionUtilTest {
private const string INPUT = "This is some test JSON {data : \"value\", object: {data : \"value\"}}";
[Test]
public void EncryptionAndDecryptionMatch() {
string encryptedInput = EncryptionUtil.Encrypt(INPUT);
string decryptedInput = EncryptionUtil.Decrypt(encryptedInput);
Assert.AreEqual(decryptedInput, INPUT, "Original and decrypted inputs do not match");
}
}
Here’s my stacktrace :
System.Security.Cryptography.CryptographicException : Bad PKCS7 padding. Invalid length 105.
---
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) [0x0005c] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:363
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x001a3] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:515
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00034] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:554
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Security.Cryptography/RijndaelManagedTransform.cs:94
at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00240] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Security.Cryptography/CryptoStream.cs:205
at System.IO.StreamReader.ReadBuffer () [0x00012] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:338
at System.IO.StreamReader.Read (System.Char[] buffer, Int32 index, Int32 count) [0x00077] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:415
at System.IO.StreamReader.ReadToEnd () [0x00040] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/StreamReader.cs:519
at EncryptionUtil.Decrypt (System.String input) [0x00085] in D:\My_documents runk\Assets\Scripts\utils\EncryptionUtil.cs:58
at EncryptionUtilTest.EncryptionAndDecryptionMatch () [0x0000b] in D:\My_documents runk\Assets\Editor\Tests\EncryptionUtilTest.cs:16
Can someone , please, help me to get it work?