Encryption 如何使用MemoryStream而不是FileStream进行解密?
基于此加密/解密代码示例:Encryption 如何使用MemoryStream而不是FileStream进行解密?,encryption,memorystream,rijndaelmanaged,Encryption,Memorystream,Rijndaelmanaged,基于此加密/解密代码示例: ///<summary> /// Steve Lydford - 12/05/2008. /// /// Encrypts a file using Rijndael algorithm. ///</summary> ///<param name="inputFile"></param> ///<param name="outputFile"></param> private void Encry
///<summary>
/// Steve Lydford - 12/05/2008.
///
/// Encrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
private void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
///<summary>
/// Steve Lydford - 12/05/2008.
///
/// Decrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
private void DecryptFile(string inputFile, string outputFile)
{
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
结果现在起作用了(我编辑了,因为我把解密程序指向了错误的文件),但我只收到一组数字作为输出
这是FileStrem示例,它工作得非常好:
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
使用系统安全;
使用System.Security.Cryptography;
使用System.Runtime.InteropServices;
使用System.Text.RegularExpressions;
命名空间解密
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
///
///史蒂夫·利德福德-12/05/2008。
///
///使用Rijndael算法加密文件。
///
///
///
私有void加密文件(字符串输入文件、字符串输出文件)
{
尝试
{
字符串密码=@“myKey123”;//此处输入您的密钥
Unicode编码UE=新的Unicode编码();
byte[]key=UE.GetBytes(密码);
字符串cryptFile=outputFile;
FileStream fsCrypt=newfilestream(cryptFile,FileMode.Create);
RijndaelManaged RMCrypto=新的RijndaelManaged();
CryptoStream cs=新加密流(fsCrypt,
RMCrypto.CreateEncryptor(密钥,密钥),
CryptoStreamMode.Write);
FileStream fsIn=newfilestream(inputFile,FileMode.Open);
int数据;
而((data=fsIn.ReadByte())!=-1)
cs.WriteByte((字节)数据);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
抓住
{
Show(“加密失败!”,“错误”);
}
}
///
///史蒂夫·利德福德-12/05/2008。
///
///使用Rijndael算法解密文件。
///
///
///
私有无效解密文件(字符串输入文件、字符串输出文件)
{
{
字符串密码=@“myKey123”;//此处输入您的密钥
Unicode编码UE=新的Unicode编码();
byte[]key=UE.GetBytes(密码);
FileStream fsCrypt=newfilestream(inputFile,FileMode.Open);
RijndaelManaged RMCrypto=新的RijndaelManaged();
CryptoStream cs=新加密流(fsCrypt,
RMCrypto.CreateDecryptor(密钥,密钥),
CryptoStreamMode.Read);
FileStream fsOut=newfilestream(outputFile,FileMode.Create);
int数据;
而((data=cs.ReadByte())!=-1)
写字节((字节)数据);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
加密文件(“hello.txt”、“hello.007”);
textBox1.Text=“加密”;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
解密文件(“hello.007”、“hello_dec.txt”);
textBox1.Text=“已解密”;
}
}
}
您是否确实尝试过它是否适用于文件流,或者是否也失败了?您是对的。。。我会立即尝试…可能是因为filestream有WriteByte函数,而MemoryStream没有?
namespace EncryptDecrypt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
EncryptFile("Seriali_01.txt", "Seriali_01.007", @"mykey123");
textBox1.Text = "Encrypted";
}
private void EncryptFile(string inputFile, string outputFile, string password)
{
try
{
//string password = @"mykey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
private void DecryptFile(string inputFile, string password)
{
using (var ms = new MemoryStream())
{
//string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
var fsOut = new StreamWriter(ms);
//MemoryStream fsOut = new MemoryStream();
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.Write((byte)data);
fsOut.Flush();
ms.Position = 0;
var sr = new StreamReader(ms);
var myStr = sr.ReadToEnd();
textBox2.Text = myStr;
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
DecryptFile("Seriali_01.007", @"mykey123");
textBox1.Text = "Encrypted";
}
}
}