文件加密和解密c#Rijndael密码 [英] file encryption and decryption c# Rijndael cipher
本文介绍了文件加密和解密c#Rijndael密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void btnEncrypt_Click(object sender ,EventArgs e)
{
// EncryptFile();
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =所有文件(*。*)|;
dialog.InitialDirectory = @桌面;
dialog.Title =请选择要加密的文件。
dialog.ShowDialog();
inputFile = dialog.FileName;
outputFile = inputFile;
string password = @myKey123; //你的钥匙
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,FileAccess.Read,FileShare.ReadWrite);
int数据;
while((data = fsIn.ReadByte())!= -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
这个功能很好,但是当我尝试解密相同的文本文件时,没有任何错误信息让我知道发生了什么,我使用以下代码进行解密
private void btnDecrypt_Click(object发件人,EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =所有文件(*。*)|;
dialog.InitialDirectory = @桌面;
dialog.Title =请选择要解密的文件。
dialog.ShowDialog();
inputFile = dialog.FileName; //C:\\Users\\daniel\\Desktop\\text.txt;
outputFile = inputFile;
string password = @myKey123; //您的密钥
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.Open,FileAccess.Read,FileShare.ReadWrite);
int数据;
while((data = cs.ReadByte())!= -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
帮助?
解决方案
fsOut
作为只读流打开。
FileStream fsOut = new FileStream(
outputFile,
FileMode.Open,
FileAccess.Write,
FileShare.ReadWrite);
注意:我也会写入一个临时文件并复制原始的,当我完成,但我没有一个很好的的原因。
I can encrypt a text file that is located on my desktop using the following code.
private void btnEncrypt_Click(object sender, EventArgs e)
{
//EncryptFile();
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All Files (*.*)|";
dialog.InitialDirectory = @"Desktop";
dialog.Title = "Please select a file to encrypt.";
dialog.ShowDialog();
inputFile = dialog.FileName;
outputFile = inputFile;
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, FileAccess.Read, FileShare.ReadWrite);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this works great but when i try to decrypt the same text file nothing happens, no error message is thrown to let me know what is going on, i use the following code for decryption
private void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "All Files (*.*)|";
dialog.InitialDirectory = @"Desktop";
dialog.Title = "Please select a file to decrypt.";
dialog.ShowDialog();
inputFile = dialog.FileName; // "C:\\Users\\daniel\\Desktop\\text.txt";
outputFile = inputFile;
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.Open, FileAccess.Read, FileShare.ReadWrite);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
can anybody help?
解决方案
fsOut
is open as a read-only stream. Try this instead.
FileStream fsOut = new FileStream(
outputFile,
FileMode.Open,
FileAccess.Write,
FileShare.ReadWrite);
Note: I would also write to a temp file and copy over the original when completed but I don't have a great reason for it.
这篇关于文件加密和解密c#Rijndael密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文