10

I am trying to encrypt a string using C# and decrypt it using Python. The encryption/decryption part works as expected (i.e. I am able to decrypt the string I originally encrypted). However the string returned by Python has 2 extra bytes at the beginning and each character is separated by a space.

**Original string** (before encryption -- encrypted using C#) = "Something you want to keep private with AES"

**Decrypted string** (using Python) = "��S o m e t h i n g  y o u   w a n t   t o   k e e p   p r i v a t e   w i t h  A E S"

Why am I getting these two extra bytes at the beginning of the string? Why all those spaces in the decrypted string? Any idea why?

Thanks!

Encryption with C#

public static string Encrypt<T>(string value, string password, string salt)
         where T : SymmetricAlgorithm, new()
{
    DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));

    SymmetricAlgorithm algorithm = new T();

    byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
    byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);

    ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

    using (MemoryStream buffer = new MemoryStream())
    {
        using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
        {
            using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
            {
                writer.Write(value);
            }
        }

        return Convert.ToBase64String(buffer.ToArray());
    }
}


string plain = "Something you want to keep private with AES";
string encrypted = CipherUtility.Encrypt<AesManaged>(plain, "password", "salt");

Decryption with Python + pycrypto

import base64, sys
import Crypto.Cipher.AES

password = base64.b64decode('PSCIQGfoZidjEuWtJAdn1JGYzKDonk9YblI0uv96O8s=') # See rgbKey
salt = base64.b64decode('ehjtnMiGhNhoxRuUzfBOXw==') # See rgbIV
aes = Crypto.Cipher.AES.new(password, Crypto.Cipher.AES.MODE_CBC, salt)
text = base64.b64decode('QpHn/fnraLswwI2Znt1xTaBzRtDqO4V5QI78jLOlVsbvaIs0yXMUlqJhQtK+su2hYn28G2vNyLkj0zLOs+RIjElCSqJv1aK/Yu8uY07oAeStqRt4u/DVUzoWlxdrlF0u')

print aes.decrypt(text)
1
  • How can I decrypt using Python for .NET RSA algorithm encryption ? Commented Oct 18, 2017 at 10:05

2 Answers 2

12

The string is encoded to bytes using the UTF-16 encoding. The first two bytes are a BOM. Then each character is encoded to two bytes.

From the documentation for Encoding.Unicode:

Gets an encoding for the UTF-16 format using the little endian byte order.

To get the original string you need to decode it back from UTF-16 bytes to a Unicode string.

print aes.decrypt(text).decode('utf-16')
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! Thank you Mark! Now that I am decoding the string using utf-16 (as you proposed), the resulting string is: "Something you want to keep private with AESࠈࠈࠈࠈ". Any idea how to get rid of the last 4 characters?
Try to set padding for SymmetricAlgorithm to zeros msdn.microsoft.com/en-us/library/…. By default it uses PKCS7
i was looking for a method of encrypting in .net/C# and decrypting in python and came across this post.. i too am getting the padding. Mine shows up as "test2ЄЄ" with the extra E looking chars . i tried changing PaddingMode to None on the .net side from PKCS7 and it made no difference??
0
 def decrypted(self) -> str:
    _pwd = base64.b64decode(self._key)
    _salt = base64.b64decode(self._salt)
    _aes = Crypto.Cipher.AES.new(_pwd, Crypto.Cipher.AES.MODE_CBC, _salt)
    _text = base64.b64decode(self._encrypted_str)
    _decode = _aes.decrypt(_text).decode('utf-16')
    _regex = '[a-zA-Z0-9 +-,\/ ]+'
    return re.findall(_regex, _decode)

is using regex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.