c# - 将流转换为字符串

标签 c# wpf encryption stream

我有一个加密文件存储在我的本地机器上。我解密文件,将解密数据作为流获取,然后尝试将其转换为字符串。下面是我的代码,但我得到的文本总是空的。

private void Decrypt(string inputFilePath, string outputfilePath)
{
   string EncryptionKey = "MAKV2SPBNI99212";
   using (Aes encryptor = Aes.Create())
   {
      Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, 
                               new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
                               0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
      encryptor.Key = pdb.GetBytes(32);
      encryptor.IV = pdb.GetBytes(16);
      using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
      {
         using (CryptoStream cs = new CryptoStream(fsInput, 
                            encryptor.CreateDecryptor(), CryptoStreamMode.Read))
         {
            using (Stream s = new MemoryStream())
            {
               int data;
               while ((data = cs.ReadByte()) != -1)
               {
                  s.WriteByte((byte)data);
               }
               StreamReader reader = new StreamReader(s);
               string text = reader.ReadToEnd();
            }
         }
      }
   }
}

最佳答案

在你的内存流上——在你向它写入数据后尝试 .Flush() 和 Position = 0。

关于c# - 将流转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23528127/

相关文章:

c# - "Repair"VS 2019 中的 Android SDK 管理器失败

c# - Xamarin iOS(WebClient IPv6 拒绝)

wpf - 应用程序图标在任务栏中正确,但在开始菜单中不正确?

c# - 在命令提示符下使用 c# 为 wpf 应用程序执行命令

c# - 使用 XML 序列化序列化没有父节点的集合

c# - 在不从 LCID 创建 RegionInfo 的情况下比较文化区域

.net - WPF 运行时控件创建

encryption - AES 加密输出中包含的特殊字符

c++ - 使用 OpenSSL 实现密文窃取

c# - 使用 Entity Framework 在 sql server 中加密/解密列