簡體   English   中英

RijndaelManaged 解密 .net 5 不工作

[英]RijndaelManaged Decryption in .net 5 not working

我在 .net 框架中有以下代碼來解密一些信息,它工作正常。 我的任務是將此代碼升級到 .net 5,但相同的代碼無法正常工作。

.net 框架中的當前代碼工作正常

private static string DecryptStringFromBytes(string cypherText, byte[] key)
{
    byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

    if (key == null || key.Length <= 0)
        throw new ArgumentNullException("key");

    string plaintext;
    using (var rijAlg = new RijndaelManaged())
    {
        rijAlg.BlockSize = 256;
        rijAlg.Key = key;
        rijAlg.Mode = CipherMode.CBC;
        rijAlg.Padding = PaddingMode.Zeros;
        rijAlg.IV = ASCIIEncoding.ASCII.GetBytes(MD5(MD5(_encryptionKey)));

        ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
        using (var msDecrypt = new MemoryStream(cipherTextBytes))
        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        using (var srDecrypt = new StreamReader(csDecrypt))
            plaintext = srDecrypt.ReadToEnd();
    }
    return plaintext;
}

private static string MD5(string testString)
{
    byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(testString);
    byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
    string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
    return hashedString;
}

當我在 .net 5 中嘗試使用相同的代碼時,出現以下錯誤。 “在此實現中 BlockSize 必須為 128”

當我將上述代碼中的塊大小更改為 128 時,錯誤消失了,但解密后的文本都是亂七八糟的文本(不是可讀格式)。

我對此做了一些研究,我意識到 .net 內核尚不支持 256 塊大小,解決方法是將其與 Bouncy Castle 庫一起使用。 我對此進行了一些谷歌搜索,在查看了一些示例后,我將代碼更新為以下內容

public static string DecryptFromBouncyCastle(string cipherText, byte[] key)
{
    byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
    if (key == null || key.Length <= 0)
        throw new ArgumentNullException("key");

    var ivStringBytes = new byte[16];

    var engine = new RijndaelEngine(256);
    var blockCipher = new CbcBlockCipher(engine);
    var cipher = new PaddedBufferedBlockCipher(blockCipher, new ZeroBytePadding());
    var keyParam = new KeyParameter(key);
    var keyParamWithIV = new ParametersWithIV(keyParam, ivStringBytes, 0, 32);
    cipher.Init(false, keyParamWithIV);
    var outputBytes = new byte[cipher.GetOutputSize(cipherTextBytes.Length)];
    var length = cipher.ProcessBytes(cipherTextBytes, outputBytes, 0);
    var finalBytes = cipher.DoFinal(outputBytes, 0, length);
    var resultText = Encoding.UTF8.GetString(finalBytes);
    return resultText;
}

private static string MD5(string testString)
{
    byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(testString);
    byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
    string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
    return hashedString;
}

這段代碼沒有給我任何錯誤,但我看到部分解密工作正常。 我看到 30% 的解密文本和 rest 一些垃圾字符。

我不太熟悉加密/解密的內部結構,我很難理解問題出在哪里。 這可能是一個小問題,但我無法弄清楚。

如果有人可以查看此代碼並讓我知道我的代碼的問題所在或建議是否有其他更好的替代解決方案,我將不勝感激。

必須修改 IV 和DoFinal()調用:

var ivStringBytes = ASCIIEncoding.ASCII.GetBytes(MD5(MD5(_encryptionKey)));     // Fix 1
...
length += cipher.DoFinal(outputBytes, length);                                  // Fix 2
var resultText = Encoding.UTF8.GetString(outputBytes, 0, length);               // Fix 3

請注意,static IV 是不安全的。 此外,AES 應該優於具有 256 位塊大小的 Rijndael。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM