Coder Social home page Coder Social logo

Comments (4)

adriancs2 avatar adriancs2 commented on August 27, 2024

I am actually thinking of dropping the encryption function. The encryption is performed line by line which will slow the whole process a lot.

Anyway, at the mean time, the better solution which I recommended is: perform the encryption externally after the Export process completed.

There are many encryption algorithms out there. Here is one of the example of implementing AES 256 bit encryption. Source code can be found at: https://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt

And if you are going to encrypt your file, you definitely also want to compress the file which will make the file 50%-70% smaller in size.

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
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;
}

from mysqlbackup.net.

doItBinary avatar doItBinary commented on August 27, 2024

Thank you very much for your response, that definitely should work.
And again, you're doing a great work with this.

from mysqlbackup.net.

adriancs2 avatar adriancs2 commented on August 27, 2024

Thanks.

from mysqlbackup.net.

phil-w avatar phil-w commented on August 27, 2024

I assume you did drop the function as I can't find it in the docs, but the example here is very useful in getting me that sorted - thanks.

from mysqlbackup.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.