I have a c# client that decrypts an AES encrypted message. I tried to implement the c# logic in my python client, but the result is not the same and is full of question marks and vague characters.
I am using python 3.5 with pycrypto running mint x64. the code for both c# client and my python version of the code provided below:
c# code:
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText); //Get the encrypted message's bytes
using (Aes encryptor = Aes.Create()) //Create a new AES object
{
//Decrypt the text
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
plainText = Encoding.Unicode.GetString(ms.ToArray());
}
my python version:
def decode_base64(data, altchars=b'+/'):
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
"""
data = re.sub(rb'[^a-zA-Z0-9%s]+' % altchars, b'', data) # normalize
missing_padding = len(data) % 4
if missing_padding:
data += b'='* (4 - missing_padding)
return base64.b64decode(data, altchars)
def decode_message(data, key):
enc_txt = decode_base64(bytes(data, 'utf-16'))
salt_t = ["0x49", "0x76", "0x61", "0x6e", "0x20", "0x4d", "0x65", "0x64", "0x76", "0x65", "0x64", "0x65", "0x76"]
salt = bytes([int(x, 0) for x in salt_t])
key_bytes = KDF.PBKDF2(key, salt, 32, 1000)
# iv = enc_txt[:16] // using this line instead of the below line, has no effects on final result
iv = KDF.PBKDF2(key, salt, 16, 1000)
cipher = AES.new(key_bytes, AES.MODE_CBC, iv)
return cipher.decrypt(enc_txt).decode('utf-16')
the c# client is working as expected, but my python client is resulting in vague characters and not the actual expected message.
I ran into this post I think I have a similar problem but I couldn't understand the provided answer. any answer would be appreciated. thanks in advance.
UPDATE: C# Server Side Encryption: This is the C# server side encryption code as well, i think this question covers multiple aspects of the scenario based on the linked questions and could be a reference for anybody that face the same issues (encodings, encryption, padding...)
string EncryptionKey = "MAKV2SPBNI99212"; //Declare the encryption key (it's not the best thing to do)
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); //Get the bytes of the message
using (Aes encryptor = Aes.Create()) //Create a new aes object
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32); //Set the encryption key
encryptor.IV = pdb.GetBytes(16); //Set the encryption IV
using (MemoryStream ms = new MemoryStream()) //Create a new memory stream
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) //Create a new crypto stream
{
cs.Write(clearBytes, 0, clearBytes.Length); //Write the command to the crypto stream
cs.Close(); //Close the crypto stream
}
cipherText = System.Convert.ToBase64String(ms.ToArray()); //Convert the encrypted bytes to a Base64 string