0

There is a similar question here Problem Updating to .Net 6 - Encrypting String But it is not same as what I am facing.

My plain text is as below

+6+HGeVqjF6OwJzD/bKZaLyORmYBu0o/uk1Tj0T3ksI=

Encrypted it using below code

private string Encrypt(string data, string saltKey)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
            byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            byte[] rgbKey =
                new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations)
                    .GetBytes(this.keySize / 8);
            using RijndaelManaged managed = new() { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 };
            using ICryptoTransform transform = managed.CreateEncryptor(rgbKey, bytes);
            using MemoryStream stream = new();
            using CryptoStream stream2 = new(stream, transform, CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            byte[] inArray = stream.ToArray();
            stream.Close();
            stream2.Close();
            return Convert.ToBase64String(inArray);
        }

Encrypted string looks like below

EF+roawjFQJ+2+LeZc3v+v26xdcu2XXszcdnDjNqE49KDbNTNPeGx5nCRivcOxjm

Above string when decrypted with below code

private string Decrypt(string data, string saltKey)
        {
            saltValue = string.IsNullOrWhiteSpace(saltKey)
                ? _appSettingService.AppSetting.GetByKey((int)ModuleType.Core, AppKey.CoreLicenseSalt, 0)?.Value??""
                : saltKey;
            byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
            byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
            byte[] buffer = Convert.FromBase64String(data);
            byte[] rgbKey =
                new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations)
                    .GetBytes(this.keySize / 8);
            using RijndaelManaged managed = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 };
            using ICryptoTransform transform = managed.CreateDecryptor(rgbKey, bytes);
            using MemoryStream stream = new(buffer);
            //using MemoryStream stream = new();
            using CryptoStream stream2 = new(stream, transform, CryptoStreamMode.Read);
            byte[] buffer5 = new byte[buffer.Length];
            int count = stream2.Read(buffer5, 0, buffer5.Length);
            //stream2.CopyTo(stream);
            var streamBytes = stream.ToArray();
            stream.Close();
            stream2.Close();
            return Encoding.UTF8.GetString(buffer5, 0, count);
            //return Encoding.UTF8.GetString(streamBytes, 0, streamBytes.Length);
        }

it returns the plain text partially as below

+6+HGeVqjF6OwJzD/bKZaLyORmYBu0o/

Before encryption vs after decryption

+6+HGeVqjF6OwJzD/bKZaLyORmYBu0o/uk1Tj0T3ksI=
+6+HGeVqjF6OwJzD/bKZaLyORmYBu0o/

This code was working all good in .NET 4.8m when migrated to .NET 7.0 this issue started. Any help is greatly appreciated.

As recommended over here https://stackoverflow.com/a/69911546/942855 , I made suggested changes but now I don't get any value. Its blank

private string Decrypt(string data, string saltKey)
        {
            saltValue = string.IsNullOrWhiteSpace(saltKey)
                ? _appSettingService.AppSetting.GetByKey((int)ModuleType.Core, AppKey.CoreLicenseSalt, 0)?.Value??""
                : saltKey;
            byte[] bytes = Encoding.ASCII.GetBytes(this.initVector);
            byte[] rgbSalt = Encoding.ASCII.GetBytes(this.saltValue);
            byte[] buffer = Convert.FromBase64String(data);
            byte[] rgbKey =
                new PasswordDeriveBytes(this.passPhrase, rgbSalt, this.hashAlgorithm, this.passwordIterations)
                    .GetBytes(this.keySize / 8);
            using RijndaelManaged managed = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 };
            using ICryptoTransform transform = managed.CreateDecryptor(rgbKey, bytes);
            //using MemoryStream stream = new(buffer);
            //using CryptoStream stream2 = new(stream, transform, CryptoStreamMode.Read);
            //byte[] buffer5 = new byte[buffer.Length];
            //int count = stream2.Read(buffer5, 0, buffer5.Length);
            //var streamBytes = stream.ToArray();
            //stream.Close();
            //stream2.Close();
            //return Encoding.UTF8.GetString(buffer5, 0, count);

            using MemoryStream memoryStream = new();
            using CryptoStream cryptoStream = new(memoryStream, transform, CryptoStreamMode.Read);
            cryptoStream.CopyTo(memoryStream);
            var streamBytes = memoryStream.ToArray();
            return Encoding.UTF8.GetString(streamBytes, 0, streamBytes.Length);
        }

Solved


Change

using MemoryStream memoryStream = new();

to

using MemoryStream memoryStream = new(buffer);
HaBo
  • 13,999
  • 36
  • 114
  • 206

0 Answers0