Skip to content Skip to sidebar Skip to footer

Decrypting In Python An String Encrypted Using .net

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)

Solution 1:

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')

Solution 2:

 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

Post a Comment for "Decrypting In Python An String Encrypted Using .net"