I have created a test (not real) encryption function which takes a byte[] and replaces all the bytes with 0xff and returns
private byte[] encrypt(byte[] input)
{
for (int i = 0; i < input.Length; i++)
{
input[i] = 0xff;
}
return input;
}
Now i want to try this test encryption procedure on a file. But i want to be able to read and write to the SAME file.
This is what I have done so far
using (FileStream myfileStream = new FileStream(openFile, FileMode.Open,FileAccess.ReadWrite))
{
byte[] buffer = new byte[16];
while (myfileStream.Position < myfileStream.Length)
{
myfileStream.Read(buffer, 0, 16);
buffer = encrypt(buffer);
myfileStream.Position -= 16;
myfileStream.Write(buffer, 0, 16);
}
myfileStream.Close();
}
This works fine but I know I am not doing this right. This seems to have VERY serious performance issues where it took 24 seconds for a 1 MB file. (Tested with a StopWatch in a WinMo 6 Pro emulator).
What am I doing wrong here? What can I do to increase the performance when reading and writing to/from the same file at the same time ? Please advise. Thanx a lot in advance :)
UPDATE:
I reduced the time it took significantly (from 24 seconds, down to 6 seconds) by using 2 FileStream objects pointing to the same file with FileShare.ReadWrite
property.
Is this safe to do? Is this ok?
UPDATE AGAIN
Although I have used a fake encryption algorithm, I am hoping to use AES with CBC + CTS.