.NET Core 6 和 .NET 4.5 上的 ASP.NET MVC 之间的加密差异

问题描述 投票:0回答:1

我在 .NET 4.5 上的 ASP.NET MVC 应用程序和 .NET Core 6 应用程序中使用此方法。我没有得到相同的结果

decryptedValues

我调试了一下,发现

decryptedByteCount
在 ASP.NET MVC 中是 20,但在 .NET 6 中是 16。请注意,
cipherTexts
和密码是完全相同的。

.NET 6 版本中我需要更改哪些内容?

public static Dictionary<string, string> Decrypt(string[] cipherTexts, string passPhrase)
{
    var decryptedValues = new Dictionary<string, string>();

    if (cipherTexts == null || cipherTexts.Count() == 0 || string.IsNullOrEmpty(passPhrase))
    {
        return decryptedValues;
    }

    cipherTexts = cipherTexts.Where(q => !string.IsNullOrEmpty(q))
                             .Distinct().ToArray();

    using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
    {
        byte[] keyBytes = password.GetBytes(keysize / 8);

        using (RijndaelManaged symmetricKey = new RijndaelManaged())
        {
            symmetricKey.Mode = CipherMode.CBC;
            symmetricKey.Key = keyBytes;
            symmetricKey.IV = initVectorBytes;

            using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor())
            {
                foreach (var cipherTextOrig in cipherTexts)
                {
                    // Revert mis behaved characters. StackOverflow: "some type of standard... substituting '-' with '+' and '_' by '/'"
                    // the Trim will remove the delimited char in case user choose the null value
                    var cipherTextFixed = cipherTextOrig.Trim(',').Replace("-", "+").Replace("_", "/");

                    var decryptedArray = new List<string>();

                    foreach (var cipherText in cipherTextFixed.Split(','))
                    {
                        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

                        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);
                                decryptedArray.Add(Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount));
                            }
                        }
                    }

                    decryptedValues.Add(cipherTextOrig, String.Join(",", decryptedArray));
                }
            }
        }
    }

    return decryptedValues;
} 
c# asp.net-mvc .net-6.0 .net-4.5
1个回答
0
投票

更换

byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                        decryptedArray.Add(Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount));

与 `

using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader streamReader = new StreamReader(cryptoStream))
                                {
                                    return streamReader.ReadToEnd().ToString();
                                }
                            }`
© www.soinside.com 2019 - 2024. All rights reserved.