简体   繁体   中英

converting the C# code to python on decryption

using System.Security.Cryptography;

public Task<byte[]> Decrypt(byte[] encryptedBytes, string encryptionKey)
{
    if (encryptedBytes == null)
        throw new ArgumentNullException("encryptedBytes can't be null");

    if (string.IsNullOrEmpty(encryptionKey))
        throw new ArgumentNullException("encryptionKey can't be null");

    byte[] encryptedTextBytes = encryptedBytes;
    this._encryptionKey = encryptionKey;

    var encryptionKeyBytes = Encoding.UTF32.GetBytes(this._encryptionKey);
    Rfc2898DeriveBytes generatedKey = new Rfc2898DeriveBytes(this._encryptionKey, new byte[] { 0x65, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x65 });
    var sessionKey = generatedKey.GetBytes(32);
    var iV = generatedKey.GetBytes(16);

    return Task.Run(() =>
    {
        return (Decrypt(encryptedTextBytes, sessionKey, iV));

    });
}

public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
{
    using (var aes = new AesCryptoServiceProvider())
    {
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        aes.Key = key;
        aes.IV = iv;

        using (var memoryStream = new MemoryStream())
        {
            var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
            cryptoStream.FlushFinalBlock();

            var decryptBytes = memoryStream.ToArray();

            return decryptBytes;
        }
    }
}

Hi, I need help converting the above-mentioned c# code to its equivalent python code. As I am new to python, hence not sure which are the relevant libraries to be used here. It would be great if someone could help me here.

So far I have tried the below code but looks like it is not working:

from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from PIL import Image
_encryptionKey='secret'.encode("utf-32")
salt = '\0x65\0x76\0x61\0x6e\0x20\0x4d\0x65\0x64\0x76\0x65\0x64\0x65\0x65'.encode("utf-32")
key_bytes = PBKDF2(_encryptionKey, salt, dkLen=64)
session_key=key_bytes[:32]
iv= key_bytes[:16]
cipher = AES.new(session_key, AES.MODE_CBC, iv)
val = cipher.decrypt(ency_img)
Image.open(BytesIO(val))

Here ency_img is encrypted image bytes object coming from the MySQL DB with column type as longblob

error from PIL Image

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fb10d79a270>

I have found the solution to this question with the help of @Topaco's comment. Thank you, buddy.

As mentioned in the comment of @Topaco, I had to do the following changes:

  1. replace the \0x with \x
  2. change encodings of _encryptionKey and salt to utf-8 instead of utf-32
  3. value of iv as key_bytes[32:32+16] instead of key_bytes[:16]

Below is the working code for me:

from Crypto.Cipher import AES
from Crypto.Protocol.KDF import PBKDF2
from PIL import Image

_encryptionKey='secret'.encode("utf-8")
salt = '\x65\x76\x61\x6e\x20\x4d\x65\x64\x76\x65\x64\x65\x65'.encode("utf-8")
key_bytes = PBKDF2(_encryptionKey, salt, dkLen=64)
session_key=key_bytes[:32]
iv= key_bytes[32:32+16]
cipher = AES.new(session_key, AES.MODE_CBC, iv)
img = Image.open(BytesIO(val))
print(img.size)
Image.open(BytesIO(val))

Hope this would help somebody

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM