Description
The same code works differently on two platforms and unfortunately not in favor of .NET6. Which is why I am getting the wrong result.
Reproduction Steps
Execute the code (which I will provide below) and encrypt something and you will see how .NET6 is mistaken.
Expected behavior
The code
Console.WriteLine(Enc.Decrypt(Enc.Encrypt("server=.;database=DB_MyTest;password=MyPassword;user id=sa;")));
should return
server=.;database=DB_MyTest;password=MyPassword;user id=sa;
Actual behavior
The code
Console.WriteLine(Enc.Decrypt(Enc.Encrypt("server=.;database=DB_MyTest;password=MyPassword;user id=sa;")));
returns
server=.;database=DB_MyTest;password=MyPassword;
Regression?
No response
Known Workarounds
No response
Configuration
Windows 10, x64
Visual Studio 2022 RC2
.NET6 RC2
.NET Framework 4.8
Other information
Decryption/Encryption Code:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace EncTest
{
public static class Enc
{
private static string passPhrase = "@y88MP*Qv";
private static string saltValue = "?5b#9DbtS";
private static string hashAlgorithm = "SHA256";
private static int passwordIterations = 2;
private static string initVector = "BSY_DJT7hu#6$sRQ";
private static int keySize = 128;
public static string Encrypt(string plainText)
{
byte[] rgbSalt = Encoding.UTF8.GetBytes(saltValue);
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, rgbSalt, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
public static string Decrypt(string cipherText)
{
byte[] rgbSalt = Encoding.UTF8.GetBytes(saltValue);
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, rgbSalt, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
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();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
return plainText;
}
}
}
Description
The same code works differently on two platforms and unfortunately not in favor of .NET6. Which is why I am getting the wrong result.
Reproduction Steps
Execute the code (which I will provide below) and encrypt something and you will see how .NET6 is mistaken.
Expected behavior
The code
Console.WriteLine(Enc.Decrypt(Enc.Encrypt("server=.;database=DB_MyTest;password=MyPassword;user id=sa;")));should return
Actual behavior
The code
Console.WriteLine(Enc.Decrypt(Enc.Encrypt("server=.;database=DB_MyTest;password=MyPassword;user id=sa;")));returns
Regression?
No response
Known Workarounds
No response
Configuration
Windows 10, x64
Visual Studio 2022 RC2
.NET6 RC2
.NET Framework 4.8
Other information
Decryption/Encryption Code: