我使用DES加密了一个文件,然后在服务器上成功解密并使用
System.IO.File.WriteAllBytes(@"c:\test\" + fileName, decryptedFile);
方法文件数据发生了一点变化,文本是“加密和解密usind DES blah blah blah”,解密后最终文件中的文本是“k$nlng and Decrypting usind DES blah blah blah”,我也试过这个:
using (BinaryWriter binWriter =
new BinaryWriter(File.Open(@"C:\Test\" + fileName, FileMode.Create)))
{
binWriter.Write(decryptedFile);
}
文本仍然不一样的加密方式:
public byte [] DESEncrypt(byte [] fileBytes)
{
CryptoStreamMode mode = CryptoStreamMode.Write;
// Set up streams and encrypt
MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream,
cryptoProvider.CreateEncryptor(cryptoProvider.Key, cryptoProvider.Key), mode);
cryptoStream.Write(fileBytes, 0, fileBytes.Length);
cryptoStream.FlushFinalBlock();
// Read the encrypted message from the memory stream
byte[] encryptedMessageBytes = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
MessageBox.Show("encrypted DES");
return encryptedMessageBytes;
}
解密:
static public byte[] DESdecrypt(byte [] fileBytes)
{
ICryptoTransform decryptor = cryptoProvider.CreateDecryptor();
byte[] originalAgain = decryptor.TransformFinalBlock(fileBytes, 0, fileBytes.Length);
return originalAgain;
}
谢谢