new to AES, I visited several threads about it, ex: examples of Dan Esparza, Zeeshan Amber at this thread Simple encryption algorithm in c#.net , and smdrager at Simple encryption algorithm in c#.net
I tried to use some examples in my case, but I found something incorrect, let me explain:
- I want to encrypt a binary file (named "exa" ).
- I use git to check whether output file is different from input file.
- I read input file to stream(byte[] or string).
- Just after that, write to a different file by using File.WriteAllBytes() right away. ( make sure the reading bytes are right , succseed )
- Then, I used the examples of Dan Esparza, smdrager, Zeeshan Amber
- Every time I finished, I compared the decrypt bytes in memory from the original ones and see if the bytes different.
- I saved the decrypt result to file and compare.
- The results are all disappointing, all files failed, but in some cases the bytes comparison are fine.
The example of smdrager
byte[] orgBytes = File.ReadAllBytes("exa");
byte[] encQuote = EncryptByteToBytes(orgBytes , "password" );
byte[] endByte = DecryptToByte(encQuote, "password");
File.WriteAllBytes("exaOutputBytes", endByte);
The comparison failed in byte number, also failed in file comparison.
orgBytes.Length : 55732
endByte.Length : 55744
The example of Dan Esparza
In this case, bytes input will fail, therefore, I read by text with ASCII.
string original = File.ReadAllText("exa" , Encoding.ASCII );
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
File.WriteAllText("exaOutputString", roundtrip , Encoding.ASCII);
The bytes comparison was fine, both in length and each byte content. But file comparison still failed.
The example of Zeeshan Amber
I use password and iv the same as example.
Crypt cryptObj = new Crypt();
string encryStr = cryptObj.Encrypt(original, "AGARAMUDHALA", "EZHUTHELLAM", "SHA1", 3, "@1B2c3D4e5F6g7H8", 256);
string decryStr = cryptObj.Decrypt(encryStr, "AGARAMUDHALA", "EZHUTHELLAM", "SHA1", 3, "@1B2c3D4e5F6g7H8", 256);
In this case, the same with Dan Esparza , failed in file comparison.
I think the problem should be at my file importing/exporting, maybe special character in file, EOL, or BOM. I also tried different encoding when writing files, but found nothing.
Does anyone have something in mind?