I have decrypted the string "12345678901234567890123456789012" in C#, using AES
The string is 32 bytes and the resulting "array" (see code) is of length 48, so both are even multiples of 16.
key is "b14ca5898a4e4133bbce2ea2315a1916"
public static string EncryptString(string key, string plainInput)
{
byte[] emptyIv = new byte[16];
byte[] array;
List<byte> result;
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.Mode = CipherMode.CBC;
aes.BlockSize = 128;
aes.Padding = PaddingMode.PKCS7;
//aes.GenerateIV();
aes.IV = emptyIv;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
{
streamWriter.Write(plainInput);
}
array = memoryStream.ToArray();
}
}
result = new List<byte>(aes.IV); #EDIT AFTER COMMENT
result.AddRange(new List<byte>(array));#EDIT AFTER COMMENT
}
return Base64UrlEncoder.Encode(result.ToArray());
}
Im trying to decrypt it in my python-code
import base64
import hashlib
from Cryptodome import Random
from Cryptodome.Cipher import AES
#...
def decrypt( self, encryptedB64):
encrypted = base64.b64decode(encryptedB64 + '===')
iv = encrypted[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv )
decrypted = cipher.decrypt(encrypted[AES.block_size:]) #<-- error on this line
return AESEncryptionHelper._unpad(decrypted)
When I try to decrypt it (cipher.decrypt(...) I get the error message:
"Data must be padded to 16 byte boundary in CBC mode".
I have read that data must be of multiple 16, but Im not really clear on what data needs to be of multiple 16.
I have pasted in my resulting (base64-encoded) encrypted string into this website (no IV, 256bits, CBC-mode, see key above) and it works well.
So to keep it short, what am I doing wrong?