I'm using the code bellow to Encrypt and Decrypt a file:
// Encryption
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes("password");
FileStream fsCrypt = new FileStream("cryptFile", FileMode,create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream("FileName", FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
// Decryption
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes("password");
FileStream fsCrypt = new FileStream("filename", FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
int data;
while ((data = cs.ReadByte()) != -1)
memorystream.WriteByte((byte)data);
It works well, without any problem!
For some reasons I've added 10 bytes at the first of the encrypted file! Actually I've created a 10 bytes file (the file size is EXACTLY 10 Bytes), then I've appended the encrypted file to this file.
Note that the 10 bytes file is not encrypted, and is created using simple filestream, and it could be read in notepad.
Now in decryption code, how could I eliminate the first 10 bytes and decrypt remain data in the file?
I've tried to call ReadByte() for 10 times, and then goto the WHILE part and decrypt file, but it doesn't work and I get length invalid exception.
Thanks in advance.