傢伙,我是新來的這個網站,我是一個真正的新編程任何幫助將是太棒了!謝謝以下是我的加密和解密代碼。圖像和PDF文件得到加密和解密沒有問題,但文本文件我不知道它解密後沒有在文件中顯示任何內容。當我解密文本文件的文本文件變空
private void encrypt (string input, string output, string strHash)
{
FileStream inStream, outStream;
CryptoStream CryStream;
TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteTexto;
inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
byteTexto = File.ReadAllBytes(input);
md5.Clear();enter code here
TDC.Key = byteHash;
TDC.Mode = CipherMode.ECB;
CryStream = new CryptoStream(outStream, TDC.CreateEncryptor(), CryptoStreamMode.Write);
int bytesRead;
long length, position = 0;
length = inStream.Length;
while(position < length)
{
bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length);
position += bytesRead;
CryStream.Write(byteTexto, 0, bytesRead);
}
inStream.Close();
outStream.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.ShowDialog();
txtLocalDecFile.Text = open.FileName;
SaveFileDialog save = new SaveFileDialog();
save.ShowDialog();
txtNewDecFile.Text = save.FileName;
decrypt(txtLocalDecFile.Text, txtNewDecFile.Text, key);
}
private void decrypt(string input, string output, string strHash)
{
FileStream inStream, outStream;
CryptoStream CryStream;
TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteTexto;
inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
byteTexto = File.ReadAllBytes(input);
md5.Clear();
TDC.Key = byteHash;
TDC.Mode = CipherMode.ECB;
CryStream = new CryptoStream(outStream, TDC.CreateDecryptor(), CryptoStreamMode.Write);
int bytesRead;
long length, position = 0;
length = inStream.Length;
while (position < length)
{
bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length);
position += bytesRead;
CryStream.Write(byteTexto, 0, bytesRead);
}
inStream.Close();
outStream.Close();
}
除了一個例子:什麼是與未使用'strHash' /'byteHash'? – Cameron