0

I have update the my project fretwork from .Net Core 3.1 to .Net 6. bellow code working fine with 3.1 but not working with .Net 6. In .net 6 I have missing some data, Like if I encrypt "Ravi Chandra" and than decrypt, I got "Ravi Chand". please suggest any solution.

public class AESEncryption
{
    private const string _IV = "Encryption";
    private const string _Key = "TestEncryption";
    private const int _BlockSize = 128;
    public static string Encrypt(string plainText)
    {
        if (string.IsNullOrEmpty(plainText))
            return ""; byte[] bytes = Encoding.Unicode.GetBytes(plainText);
        using (Aes crypt = Aes.Create())
        {
            using (HashAlgorithm hash = MD5.Create())
            {
                crypt.BlockSize = _BlockSize;
                crypt.Key = hash.ComputeHash(Encoding.Unicode.GetBytes(_Key));
                crypt.IV = hash.ComputeHash(Encoding.Unicode.GetBytes(_IV)); using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream =
                    new CryptoStream(memoryStream, crypt.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(bytes, 0, bytes.Length);
                    }
                    return Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
    }
    public static string Decrypt(string ciphertext)
    {
        if (string.IsNullOrEmpty(ciphertext))
            return ""; byte[] bytes = Convert.FromBase64String(ciphertext);
        using (Aes crypt = Aes.Create())
        {
            using (HashAlgorithm hash = MD5.Create())
            {
                crypt.Key = hash.ComputeHash(Encoding.Unicode.GetBytes(_Key));
                crypt.IV = hash.ComputeHash(Encoding.Unicode.GetBytes(_IV));
                using (MemoryStream memoryStream = new MemoryStream(bytes))
                {
                    using (CryptoStream cryptoStream =
                    new CryptoStream(memoryStream, crypt.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        byte[] decryptedBytes = new byte[bytes.Length];
                        cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);
                        return Encoding.Unicode.GetString(decryptedBytes).Replace("\0", "");
                    }
                }
            }
        }
    }
}
ravi chandra
  • 131
  • 8
  • A common gotcha with .NET 6 -- you're not reading from the stream correctly, and it happened to work in lower .NET versions. See the linked answer. – canton7 May 27 '22 at 12:09
  • @canton7 Please suggest , what i can do for this. – ravi chandra May 27 '22 at 12:33
  • 1
    You need to make sure you read all of the bytes from `cryptoStream`. You're doing a single `cryptoStream.Read`, which might not read all of the data that's in `cryptoStream` (and in fact, the ciphertext and plaintext won't be the same length *anyway*, so your logic is flawed to begin with). See the answer linked at the top of the page for details and code samples. – canton7 May 27 '22 at 12:40
  • Thanks @canton7, this code is working fine using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, crypt.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(bytes, 0, bytes.Length); cs.Close(); } return Encoding.Unicode.GetString(ms.ToArray()); ; } – ravi chandra May 27 '22 at 13:11
  • I would definitely use a `StreamReader` there, as demonstrated in the linked question. – canton7 May 27 '22 at 13:27

0 Answers0