I have a encrypted password , which is encrypted in python. It is encrtypted using the base 64 encoded result of the AES 128 encryption with a fixed key.
Now my application is a C# application and I am decrypting it using RijndaelManaged. My code is
static String Decrypt(string textToDecrypt, string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = 0x80; //128
rijndaelCipher.BlockSize = 0x80; //128
byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10]; //16
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.Unicode.GetString(plainText);
}
I know how the password is encrtpyed in the python.Its like
key: encryption key
s: string to be encrypted
cipher = AES.new(key, AES.MODE_CBC)
str_to_encrypt = _create_nonce() + _to_utf16(s)
encrypted = base64.b64encode(cipher.encrypt(str_to_encrypt))
_create_nonce() returns some 16 digit value depending on current time.
The issue here is I am getting first 16 as garbage and rest I am getting correctly. What can be the issue? I think the issue might be rijndaelCipher.IV. How do we calculate the rijndaelCipher.IV when its encrypted in other language?
I am using .Net 2.0 in my application and I can't change that. Python Code:
DEFAULT_KEY = 'SV5@9raQSV5@9raQ'
aes_encrypt(self.DEFAULT_KEY, password)
def _create_nonce():
+ t1 = time.strftime("%Y%m%d%H%M%S")
+ t2 = time.strftime("%Y%m%d%H%M%S")
+
+ return struct.pack('dd', float(t1), float(t2))
+
+
+def _to_utf16(s, max_len=32, pad='\0'):
+
+ padded = str.ljust(str(s), max_len, pad)
+ utf16_padded, _ = codecs.utf_16_be_encode(padded)
+ buffer = struct.Struct(str(max_len * 2) + 'p')
+ return buffer.pack(utf16_padded)
+
+
+def aes_encrypt(key, s):
+ This will encrypt username and/or password
+ for IP Office Manager application.
+
+ Args:
+
+ key: encryption key
+ s: string to be encrypted
+ """
+ cipher = AES.new(key, AES.MODE_CBC)
+ str_to_encrypt = _create_nonce() + _to_utf16(s)
+ encrypted = base64.b64encode(cipher.encrypt(str_to_encrypt))
+
+ return encrypted