我最近一直在试验“CryptoStream”,并试图通过加密和解密文本 . Aes班 . '命名空间:System.Security.Cryptography'

在创建使用两个输入参数成功加密文本的功能后:Text,Password

static byte[] Salt = { 18,39,27,48,82,32,12,92 };
        public static string EncryptAES(string txt, string Password) {
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(Password, Salt);
            RijndaelManaged RJM = new RijndaelManaged();
            RJM.Key = rfc.GetBytes(RJM.KeySize / 8);
            ICryptoTransform ICT = Aes.Create().CreateEncryptor(RJM.Key, RJM.IV);

            using (MemoryStream MS = new MemoryStream()) {
                using (CryptoStream CS = new CryptoStream(MS, ICT, CryptoStreamMode.Write)) {
                    using (StreamWriter SW = new StreamWriter(CS)) {
                        SW.Write(txt);
                    }
                }

                return Convert.ToBase64String(MS.ToArray());
            }
        }

解密文本的问题发生了 .

我解密文本的尝试如下:

public static string DecryptAES(string EncryptedTxt, string Password) {
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(EncryptedTxt, Salt);
            RijndaelManaged RJM = new RijndaelManaged();
            RJM.Key = rfc.GetBytes(RJM.KeySize / 8);
            ICryptoTransform ICT = Aes.Create().CreateDecryptor(RJM.Key, RJM.IV);
            using (MemoryStream MS = new MemoryStream(Convert.FromBase64String(EncryptedTxt))) {
                using (CryptoStream CS = new CryptoStream(MS, ICT, CryptoStreamMode.Read)) {
                    using (StreamReader SR = new StreamReader(CS)) {
                        return SR.ReadToEnd();
                    }
                }
            }

然而;看来我将内存流转换为字符串然后返回的方式是invaild,因为我收到以下错误: Error: Value cannot be null\nParameter name: inputBuffer

错误发生在 using (MemoryStream MS = new MemoryStream(Convert.FromBase64String(EncryptedTxt)))

How can I converter the MemoryStream into string and back?

提前致谢 :-)