questions about sharpAESCrypt.dll

Discussion related to AES Crypt, the file encryption software for Windows, Linux, Mac, and Java.
Post Reply
cyc1425
Posts: 1
Joined: Wed Sep 25, 2013 8:03 am

questions about sharpAESCrypt.dll

Post by cyc1425 »

hi, i am trying to encrypt the password string with sharpAESCrypt.dll in my software,wich is building with c#.
for example:

Code: Select all

byte[] byteArr=Encoding.UTF8.GetBytes(password);

stream midAESCrypt=new MemoryStream(byteArr);

stream aesStream= new SharpAESCrypt.SharpAESCrypt(username,midAESCrypt,SharpAESCrypt.OperationMode.Encrypt);
//i use username as the password of AES
but,when i read aesStream, the builder throw error.
i want to know how to read the output stream,and convert it to string. thanks!
kenkendk
Posts: 5
Joined: Tue Jan 03, 2012 11:04 am

Re: questions about sharpAESCrypt.dll

Post by kenkendk »

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);
Post Reply