I'm using the OpenSSL Crypto library in my C# project to encrypt/decrypt files. Here is my code :
byte[] key = System.Text.Encoding.ASCII.GetBytes("password");
byte[] iv = System.Text.Encoding.ASCII.GetBytes("1234");
OpenSSL.Crypto.CipherContext cc = new OpenSSL.Crypto.CipherContext(
OpenSSL.Crypto.Cipher.AES_256_ECB);
FileStream fIn = new FileStream("C:\\file.txt", FileMode.Open,
FileAccess.Read);
FileStream fOut = new FileStream("C:\\encrypted.txt", FileMode.OpenOrCreate,
FileAccess.Write);
fOut.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fIn.Length;
int len;
DateTime start = DateTime.Now;
while (rdlen < totlen)
{
// argument 1
len = fIn.Read(bin, 0, 100);
// argument 2
fOut.Write(cc.Crypt(bin,key,iv,true),0,100);
rdlen = rdlen + len;
}
fOut.Flush();
fOut.Close();
fIn.Close();
As a result I got this exception:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
When I changed the values of argument 1 and 2 from 100 to 64 (bin still always byte[100]) it worked, the file was encrypted and decrypted but the size of the decrypted file was bigger than the original one and contained 1 or 2 more lines at the end of the text file.