You are omitting some code, but I assume you are attempting to read the stream that you are supposed to write.
You can only write to a stream that you are encrypting, and you can only read from a stream that you are decrypting. The streams do not support seeking of any kind.
Try using this template, and adapt it to your code:
Code: Select all
byte[] Encrypt(string password, byte[] data)
{
using(var inputStream = new MemoryStream(data))
using(var outputStream = new MemoryStream())
{
SharpAESCrypt.SharpAESCrypt.Encrypt(password, inputStream, outputStream);
outputStream.Position = 0;
return outputStream.ToArray();
}
}
For this simple usage, you may want to reduce the size of the output by setting these:
Code: Select all
SharpAESCrypt.SharpAESCrypt.Extension_InsertPlaceholder = false;
SharpAESCrypt.SharpAESCrypt.Extension_InsertCreateByIdentifier = false;
SharpAESCrypt.SharpAESCrypt.Extension_InsertTimeStamp = false;
If you want to use the stream, you must use it as a filter that wraps a stream.
For encrypting, you wrap a stream that will be encrypted, and write unencrypted data into it.
For decrypting, you wrap an encrypted stream and read unencrypted data.
To encrypt:
Code: Select all
using(var inputfile = System.IO.File.OpenRead("plaintext.txt"))
using(var outputfile = System.IO.File.OpenWrite("plaintext.txt.aes"))
using(var encStream = new SharpAESCrypt.SharpAESCrypt(password, outputfile, SharpAESCrypt.OperationMode.Encrypt))
inputfile.CopyTo(encStream);
To decrypt:
Code: Select all
using(var outputfile = System.IO.File.OpenWrite("plaintext.txt"))
using(var inputfile = System.IO.File.OpenRead("plaintext.txt.aes"))
using(var encStream = new SharpAESCrypt.SharpAESCrypt(password, inputfile, SharpAESCrypt.OperationMode.Decrypt))
encStream.CopyTo(outputfile);