Using SharpAESCrypt to encrypt strings
I decided to use the SharpAESCrypt implementation for AES encryption in C #. According to their documentation ( https://www.aescrypt.com/sharp_aes_crypt.html ), you should be able to use a static method, providing a password string, a text input stream, and an output stream. The data I am getting from my output stream appears to be null.
I suspect I am doing something wrong converting strings to and from a stream. Can anyone see something that clearly does not match the code below? (It compiles and runs, but newByteArray is filled with 0 at the end)
private void encryptMemStream()
{
byte[] byteArray = Encoding.UTF8.GetBytes(DecryptedTB.Text);
using (MemoryStream plainText = new MemoryStream(byteArray))
{
using (MemoryStream encryptedData = new MemoryStream())
{
//plainText.Write(byteArray, 0, byteArray.Length);
SharpAESCrypt.Encrypt(PasswordTB.Text, plainText, encryptedData);
encryptedData.Position = 0;
byte[] newByteArray = new byte[encryptedData.Length];
newByteArray = encryptedData.ToArray();
EncryptedTB.Text = Encoding.UTF8.GetString(newByteArray);
}
}
}
Edit: The built-in implementation of this code uses fileStreams, but it should work with MemoryStreams as well. I will be testing sounds and adding results here.
Additional changes:
So when you use file stream you call this code
//Code from the AES Crypt Library
public static void Encrypt(string password, string inputfile, string outputfile)
{
using (FileStream infs = File.OpenRead(inputfile))
using (FileStream outfs = File.Create(outputfile))
Encrypt(password, infs, outfs);
}
What is calling the function that I was calling directly
//Code from the AES Crypt Library
public static void Encrypt(string password, Stream input, Stream output)
{
int a;
byte[] buffer = new byte[1024 * 4];
SharpAESCrypt c = new SharpAESCrypt(password, output, OperationMode.Encrypt);
while ((a = input.Read(buffer, 0, buffer.Length)) != 0)
c.Write(buffer, 0, a);
c.FlushFinalBlock();
}
There is obviously a slight difference in using MemoryStream and FileStream that I don't understand. FileStream works fine when MemoryStream returns an empty array ...
source to share
A quick overview of the code reveals several things, including what from the discussion appears to be the main problem you are having. Problem line
EncryptedTB.Text = Encoding.UTF8.GetString(newByteArray);
This string takes binary data into newByteArray and tells your code that it is a valid UTF8 byte array. Chances are it will almost certainly not be valid UTF8. Parts of it might be (like a header), but the actual data won't be encrypted, and therefore you shouldn't use this method to get the string.
If you need a printable string to fit into some kind of text field, then the correct way to do it for binary data is through base64 encoding.
EncryptedTB.Text = Convert.ToBase64String(newByteArray);
Basic encoding effectively takes groups of three bytes (24 bits) and converts them to groups of four characters.
Another note, while I'm here, is that in these two lines:
byte[] newByteArray = new byte[encryptedData.Length];
newByteArray = encryptedData.ToArray();
You are declaring a byte array and allocating a new array to it. Then you immediately dump that in favor of an array of encryptedData.ToArray()
. This does not put the data into the allocated array, new byte[encryptedData.Length]
but creates a new array and puts it into a variable newByteArray
. Better would be to simply:
byte[] newByteArray = encryptedData.ToArray();
Full working code below:
byte[] byteArray = Encoding.UTF8.GetBytes(sourceText);
Byte[] newByteArray;
using (MemoryStream plainText = new MemoryStream(byteArray))
{
using (MemoryStream encryptedData = new MemoryStream())
{
SharpAESCrypt.Encrypt(password, plainText, encryptedData);
newByteArray = encryptedData.ToArray();
}
}
EncryptedTB.Text = Convert.ToBase64String(newByteArray);
source to share