C# 解密时可以不生成解密文件吗?
时间: 2023-08-21 21:01:41 浏览: 45
是的,你可以在解密时不生成解密文件,而是直接将解密后的数据存储在内存中或者输出到控制台。这可以通过修改程序中的代码来实现。
以下是一个示例代码,演示了如何将解密后的数据输出到控制台:
```
using System;
using System.IO;
using System.Security.Cryptography;
class Program {
static void Main(string[] args) {
string inputFile = "encrypted_file.txt";
string password = "MySecretPassword";
using (FileStream inputStream = new FileStream(inputFile, FileMode.Open))
using (SymmetricAlgorithm algorithm = Aes.Create()) {
byte[] salt = new byte[16];
inputStream.Read(salt, 0, salt.Length);
var rfc = new Rfc2898DeriveBytes(password, salt, 1000);
algorithm.Key = rfc.GetBytes(32);
algorithm.IV = rfc.GetBytes(16);
using (CryptoStream cryptoStream = new CryptoStream(inputStream, algorithm.CreateDecryptor(), CryptoStreamMode.Read))
using (StreamReader reader = new StreamReader(cryptoStream)) {
string decryptedText = reader.ReadToEnd();
Console.WriteLine(decryptedText);
}
}
}
}
```
这个示例代码将解密后的数据直接输出到控制台,而不是将数据写入到文件中。你可以根据自己的需求修改代码,将解密后的数据存储在内存中或者进行其他操作。