Skip to content

Using Encryption and Compression

Adrian Voo edited this page Sep 25, 2021 · 1 revision

There are many encryption algorithms out there. AES algorithm is used in this example.

I have written an article about AES at:
https://adriancs.com/c-sharp/216/c-aes-256-bits-encryption-library-with-salt/

Example:

Export

using System.IO;

string backupFile = "C:\\backup";
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
string password = "1234";

MemoryStream ms = new MemoryStream();

using (MySqlConnection conn = new MySqlConnection(connstr))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            conn.Open();
            cmd.Connection = conn;

            mb.ExportToMemoryStream(ms);

            conn.Close();
        }
    }
}

byte[] ba = ms.ToArray();

// 1st Compress the file data
// The size is 50%-70% smaller
ba = CompressData(ba);

// 2nd Encrypt the file data
ba = AES_Encrypt(ba, password);

// 3rd Write the file data to disk
File.WriteAllBytes(backupFile, ba);

Import

using System.IO;

string backupFile = "C:\\backup";
string connstr = "server=localhost;user=root;pwd=1234;database=test;";
string password = "1234";

// 1st Read the file bytes
byte[] ba = File.ReadAllBytes(backupFile);

// 2nd Decrypt the file data
ba = AES_Decrypt(ba, password);

// 3rd Decompress the file data
ba = DecompressData(ba);

MemoryStream ms = new MemoryStream(ba);

using (MySqlConnection conn = new MySqlConnection(connstr))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        using (MySqlBackup mb = new MySqlBackup(cmd))
        {
            conn.Open();
            cmd.Connection = conn;

            mb.ImportFromMemoryStream(ms);

            conn.Close();
        }
    }
}

Compression

using System.IO.Compression;

public byte[] CompressData(byte[] data)
{
    MemoryStream output = new MemoryStream();
    using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
    {
        dstream.Write(data, 0, data.Length);
    }
    return output.ToArray();
}

public byte[] DecompressData(byte[] data)
{
    MemoryStream input = new MemoryStream(data);
    MemoryStream output = new MemoryStream();
    using (DeflateStream dstream = new DeflateStream(input, CompressionMode.Decompress))
    {
        dstream.CopyTo(output);
    }
    return output.ToArray();
}

Encryption

using System.Security.Cryptography;

public static string AES_Encrypt(string input, string password)
{
    byte[] clearBytes = System.Text.Encoding.UTF8.GetBytes(input);
    byte[] encryptedData = AES_Encrypt(clearBytes, password);
    return Convert.ToBase64String(encryptedData);
}

public static byte[] AES_Encrypt(byte[] input, string password)
{
    return AES_Encrypt(input, Encoding.UTF8.GetBytes(password));
}

public static byte[] AES_Encrypt(byte[] input, byte[] password)
{
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    return AES_Encrypt(input, pdb.GetBytes(32), pdb.GetBytes(16));
}

public static string AES_Decrypt(string input, string password)
{
    byte[] cipherBytes = Convert.FromBase64String(input);
    byte[] decryptedData = AES_Decrypt(cipherBytes, password);
    return System.Text.Encoding.UTF8.GetString(decryptedData);
}

public static byte[] AES_Decrypt(byte[] input, string password)
{
    return AES_Decrypt(input, Encoding.UTF8.GetBytes(password));
}

public static byte[] AES_Decrypt(byte[] input, byte[] password)
{
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    return AES_Decrypt(input, pdb.GetBytes(32), pdb.GetBytes(16));
}

static byte[] AES_Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
    byte[] encryptedData = null;
    using (MemoryStream ms = new MemoryStream())
    {
        using (Rijndael alg = Rijndael.Create())
        {
            alg.Key = Key;
            alg.IV = IV;
            using (CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearData, 0, clearData.Length);
                cs.Close();
            }
            encryptedData = ms.ToArray();
        }
    }
    return encryptedData;
}

static byte[] AES_Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
    byte[] decryptedData = null;
    using (MemoryStream ms = new MemoryStream())
    {
        using (Rijndael alg = Rijndael.Create())
        {
            alg.Key = Key;
            alg.IV = IV;
            using (CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cipherData, 0, cipherData.Length);
                cs.Close();
            }
            decryptedData = ms.ToArray();
        }
    }
    return decryptedData;
}