I encrypted a file using DES then After decrypting it successfully at the server and using
System.IO.File.WriteAllBytes(@"c:\test\" + fileName, decryptedFile);
method the file data changed a little it the text is “Encrypting and Decrypting usind DES blah blah blah blah”
the text in the end file after decrypting is ” k$nlng and Decrypting usind DES blah blah blah blah”
and i also tried this:
using (BinaryWriter binWriter =
new BinaryWriter(File.Open(@"C:\Test\" + fileName, FileMode.Create)))
{
binWriter.Write(decryptedFile);
}
the text still not the same
encrypting by :
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;
}
decrypting by:
static public byte[] DESdecrypt(byte [] fileBytes)
{
ICryptoTransform decryptor = cryptoProvider.CreateDecryptor();
byte[] originalAgain = decryptor.TransformFinalBlock(fileBytes, 0, fileBytes.Length);
return originalAgain;
}
Thanks
You are passing the same value for your “key” and “iv” value. Each time you call the function, your “iv” value gets updated (thus, your key gets changed).
So you basically are doing this:
key = “key”
Encrypt (key, key)
— key has now changed.
What you need to do is:
key = “key”
iv = copy of key
Encrypt (key, iv)