Search code examples
pythonc#aesrfc2898

Translate from C# to Python | Rijndael, AES algorithms


I have code in C#, could you describe in words what is happening in the code? I tried to reproduce using this theme and this one aaand this one. But I didn't succeed. If you throw an example, it would be absolutely wonderful.

        public static string Decrypt(byte[] bytesToBeDecrypted)
        {
            byte[] decryptedBytes;
            using (var ms = new MemoryStream())
            {
                using (var aes = new RijndaelManaged())
                {
                    aes.KeySize = 256;
                    aes.BlockSize = 128;
                    var key = new Rfc2898DeriveBytes(CryptKey, SaltBytes, 1000);
                    aes.Key = key.GetBytes(aes.KeySize / 8);
                    aes.IV = key.GetBytes(aes.BlockSize / 8);
                    aes.Mode = CipherMode.CBC;
                    using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }

                    decryptedBytes = ms.ToArray();
                }
            }

            return Encoding.UTF8.GetString(decryptedBytes);
        }

libs: System, System.IO, System.Linq, System.Security.Cryptography, System.Text


Solution

  • MemoryStream -> Documentation
    Creates a stream whose backing store is memory.
    RijndaelManaged -> Documentation
    A class that allows you to use the Rijndeal algorithm for symmetric encryption/decryption. Appears to be deprecated.Note here that "In .NET Core, it is the same as AES and supports only a 128-bit block size." Which makes sense as to why your aes.BlockSize = 128.
    Rfc2898DeriveBytes -> Documentation
    Implements password-based key derivation functionality, PBKDF2, by using a pseudo-random number generator based on HMACSHA1.
    CryptoStream -> Documentation
    Defines a stream that links data streams to cryptographic transformations.

    Thankfully, whoever wrote this previously helped you out quite a bit being as the name of the method is exactly what it is doing. In the middle, you are initializing your AES class (RijndaelManaged) and setting the values. Creating a CryptoStream with the MemoryStream to write to, the decryptor you want (aes.CreateDecryptor()), and setting the mode to write. I'm not familiar with the CrypoStream class, but I assume the cs.Write is using the bytes to decrypt, the initial index of the array to start writing to, and the full length of the array as parameters. This gets written to the Memory Stream (ms), so you need to convert that to an array, which is done with ms.ToArray(). Lastly, you're returning the decrypted bytes as a UTF-8 String. \

    I'd suggest checking this response (not the exact question, but similar in implementation in Python): C# RFC2898DeriveBytes is working but Python PBKDF2 generated key and IV are not working with Python AES Decryption