Comments (4)
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.
Thank you very much for your response, that definitely should work.
And again, you're doing a great work with this.
from mysqlbackup.net.
Thanks.
from mysqlbackup.net.
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)
- 可以把这个做个界面管理工具 可以定时备份吗 HOT 3
- Procedure name contains character ` cannot backup HOT 3
- Views aren't be exported HOT 2
- Import doesn't support 2 procedures/functions in a row HOT 2
- [Feature Request] Renaming columns HOT 6
- When server does not support column-statistics HOT 2
- How to disable column-statistics ? on shared server ? HOT 2
- Exception when trying to back up tables with more than 2GB HOT 8
- Datetime Convert Error. HOT 1
- Generated Columns are not ignored after "ON DUPLICATE KEY UPDATE"
- Remote Backup (Not a Bug) (Save File) HOT 1
- Mysqlbackup.net mysql transaction HOT 2
- Double quotes in create table instead of backtick? HOT 1
- The given key '16706' was not present in the dictionary HOT 1
- --single-transaction HOT 1
- Question and pull request HOT 3
- tinyInt HOT 3
- How to get the progress report? HOT 1
- MySqlConnector.MySqlDateTime is unhandled
- After upgrading to 2.3.8, the prompt file does not exist HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mysqlbackup.net.