Max Voisard
Max Voisard

Reputation: 1942

Encrypting a file into Base64 format using AES/Rijndael

In DevGlan, at https://www.devglan.com/online-tools/aes-encryption-decryption, there is an online tool where you can upload a file and then encrypt it into Base64 format by using AES encryption and also choosing a cipher mode, secret key, and initialization vector. I want to achieve the same thing in my C# web app. Here is my code:

FileUpload3.SaveAs(Server.MapPath(FileUpload3.FileName));
string inputFile = Server.MapPath(FileUpload3.FileName);
byte[] bytesToEncrypt = File.ReadAllBytes(inputFile);
byte[] encryptedBytes = EncryptAESfile(bytesToEncrypt, CipherMode.CBC, keyArray, IV);
string encryptedFileBase64 = Convert.ToBase64String(encryptedBytes);
string encryptedFileHex = BitConverter.ToString(encryptedBytes).Replace("-", "");

    public byte[] EncryptAESfile(byte[] data, CipherMode mode, byte[] key, byte[] iv)
    {
        byte[] encryptedData = null;
        if (data == null)
            throw new ArgumentNullException("data");

        if (data == key)
            throw new ArgumentNullException("key");

        if (data == iv)
            throw new ArgumentNullException("iv");

        using (RijndaelManaged aesAlg = new RijndaelManaged())
        {
            aesAlg.Key = key;
            aesAlg.IV = iv;
            aesAlg.Mode = mode;
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            encryptedData = encryptor.TransformFinalBlock(data, 0, data.Length);
        }

        return encryptedData;
    }

The code does return a Base64 string of the file, as in the variable encryptedFileBase64, but not the correct one in reference to DevGlan. My code only returns a Base64 string of length 24 whereas DevGlan returns a string of nearly 100,000 characters. Also, when I test to see if bytes are being read, the following code returns 0, so the problem could be in my first few lines:

lblBytes.Text += "Bytes read: " + bytesToEncrypt.Length;

I've also seen many examples of encrypting files - whether in AES or some other symmetric encryption algorithm - but not ones that return a Base64 string. Most end with lines like this before the CryptoStream is closed:

byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);   // The input FileStream
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

Reference: Encrypting any file using AES

Is there a way to read the byte array of the CryptoStream and then convert it to Base64, because I don't see bytearrayinput left alone as storing the correct information. Help would be much appreciated. Thanks!

Upvotes: 0

Views: 1451

Answers (1)

chrissx
chrissx

Reputation: 13

First: How is key created? Have you double-checked that your salt is the same?

Also I haven't checked, but this code should work:

AesManaged cipher = new AesManaged();
cipher.Mode = MODE;
ICryptoTransform encryptor = cipher.CreateEncryptor(KEY, IV);
MemoryStream to = new MemoryStream();
CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write);

writer.Write(input, 0, input.Length);
writer.FlushFinalBlock();
byte[] encrypted = to.ToArray();
return Convert.ToBase64String(encrypted);

Upvotes: 0

Related Questions