1

我正在尝试使用此功能进行解密,但我不确定是什么导致它失败

  public string Decrypt(byte[] cipherText, byte[] IV, byte[] key)
    {

        using (AesCryptoServiceProvider AESDecrypt = new AesCryptoServiceProvider())
        {

            //here we set the key and IV instead having the class generate them.
            AESDecrypt.IV = IV;
            AESDecrypt.Key = key;

            ICryptoTransform decryptor = AESDecrypt.CreateDecryptor(AESDecrypt.Key, 
                                 AESDecrypt.IV);

            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
                     decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        csDecrypt.Flush();
                        plainText = srDecrypt.ReadToEnd();
                        return plainText;
                    }
                }
            }
       }

    }

纯文本返回一个空字符串。密钥和 IV 是系统在以前的函数中生成的,并且正在正确传递。

4

1 回答 1

0

我误读了第一个问题的答案,并使用 .flushfinalblock 让它返回“密文”,而没有真正解决根本问题。当我移动 cipher = msEncrypt.ToArray(); 在加密流之外它工作得很好。并删除了 .flush()。.FacePalm()

于 2014-12-13T16:13:01.183 回答