2

I am migrating some code from .NET Framework to .NET Core and came across an issue.

I have a lot of strings that were encrypted on the old system and saved on the database on their encrypted version.

I've moved the code to .NET core, created a new class for Encrypt and Decrypt the strings, but when I try to use it I get the following error.

PlatformNotSupportedException: BlockSize must be 128 in this implementation.

My function is as follow

public static string DecryptString(string cipherText)
        {
            string passPhrase = EncryptingKey(EncKey);

            byte[] cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
            byte[] saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
            byte[] ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
            byte[] cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();

            using (Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
            {
                byte[] keyBytes = password.GetBytes(Keysize / 8);
                using (RijndaelManaged symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.BlockSize = 256;
                    symmetricKey.Mode = CipherMode.CBC;
                    symmetricKey.Padding = PaddingMode.PKCS7;
                    using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
                    {
                        using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                            {
                                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                                int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                                memoryStream.Close();
                                cryptoStream.Close();
                                return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                            }
                        }
                    }
                }
            }
        }

I understand RijndaelManaged doesn't seem to support a keysize other than 128 on .NET Core, do I have any workaround to use 256 instead and not have to decrypt thousands of records and re-encrypt them again?

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rijndaelmanaged?view=net-5.0

APearTree
  • 51
  • 1
  • 8
  • Check the remarks in the link you have provided: _"In .NET Core, it is the same as AES and supports only a 128-bit block size."_ and _"The Rijndael class is the predecessor of the Aes algorithm. You should use the Aes algorithm instead of Rijndael. "_ – Guru Stron Oct 22 '21 at 16:25
  • 1
    the issue is that I have another app that is encrypting at 256 blocks does AES supports it? – APearTree Oct 22 '21 at 16:44
  • did you get it working @APearTree ? – Luca Nov 05 '21 at 10:44
  • No, I will need to do my algorithm and decrypt and encrypt my data again. – APearTree Nov 11 '21 at 09:19

0 Answers0