150 lines
4.6 KiB
C#
150 lines
4.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Security.Cryptography;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace MySQLPassword
|
|||
|
{
|
|||
|
public partial class Form1 : Form
|
|||
|
{
|
|||
|
private static string MemoryMasterKey = "!ZEqy2Zsb#VK6<8`H6;W~VxJ$r:w.{ffzwDt=<yKC6m6N3;T<9nyeF&+.4&D@rhK";
|
|||
|
|
|||
|
public Form1()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
private void btnEncrypt_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(txtHost.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Bitte geben Sie einen Hostnamen oder eine IP des Servers ein");
|
|||
|
}
|
|||
|
else if (txtPort.Value <= 0)
|
|||
|
{
|
|||
|
MessageBox.Show("Bitte geben Sie eine gültige Port des MySQL Servers ein");
|
|||
|
}
|
|||
|
else if (string.IsNullOrEmpty(txtDatabase.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Bitte geben Sie den Datenbanknamen ein");
|
|||
|
}
|
|||
|
else if (string.IsNullOrEmpty(txtUser.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Bitte geben Sie den Benutzernamen der Datenbank ein");
|
|||
|
}
|
|||
|
else if (string.IsNullOrEmpty(txtPassword.Text))
|
|||
|
{
|
|||
|
MessageBox.Show("Bitte geben Sie das Passwort des Datenbankbenutzers ein");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string cs = "Server=" + txtHost.Text + ";Port=" + txtPort.Value.ToString() + ";Database=" + txtDatabase.Text + ";Uid=" + txtUser.Text + ";Pwd=" + txtPassword + ";";
|
|||
|
|
|||
|
txtOutput.Text = Encrypt(cs);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static string Encrypt(string value, byte[] deriveKey = null)
|
|||
|
{
|
|||
|
string result = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (deriveKey == null)
|
|||
|
{
|
|||
|
deriveKey = GetKey(MemoryMasterKey);
|
|||
|
}
|
|||
|
|
|||
|
using (AesGcm aes = new AesGcm(deriveKey))
|
|||
|
{
|
|||
|
byte[] nonce = new byte[AesGcm.NonceByteSizes.MaxSize];
|
|||
|
RandomNumberGenerator.Fill(nonce);
|
|||
|
byte[] plainText = Encoding.UTF8.GetBytes(value);
|
|||
|
byte[] chiperBuffer = new byte[plainText.Length];
|
|||
|
byte[] tag = new byte[AesGcm.TagByteSizes.MaxSize];
|
|||
|
aes.Encrypt(nonce, plainText, chiperBuffer, tag);
|
|||
|
byte[] resultBuffer = nonce.Concat(chiperBuffer).Concat(tag).ToArray();
|
|||
|
result = Convert.ToBase64String(resultBuffer);
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = null;
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public static byte[] GetKey(string password)
|
|||
|
{
|
|||
|
byte[] result = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(password))
|
|||
|
{
|
|||
|
if (password.Length < 32)
|
|||
|
{
|
|||
|
password = password.PadRight(32, '@');
|
|||
|
}
|
|||
|
else if (password.Length > 32)
|
|||
|
{
|
|||
|
password = MD5Encrypt(password);
|
|||
|
}
|
|||
|
|
|||
|
result = Encoding.ASCII.GetBytes(password);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message);
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public static string MD5Encrypt(string value)
|
|||
|
{
|
|||
|
string result = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
|||
|
|
|||
|
using (System.Security.Cryptography.MD5 hash = System.Security.Cryptography.MD5.Create())
|
|||
|
{
|
|||
|
byte[] hashedInputBytes = hash.ComputeHash(bytes);
|
|||
|
|
|||
|
StringBuilder hashedInputStringBuilder = new StringBuilder(128);
|
|||
|
|
|||
|
foreach (var b in hashedInputBytes)
|
|||
|
{
|
|||
|
hashedInputStringBuilder.Append(b.ToString("X2"));
|
|||
|
}
|
|||
|
|
|||
|
result = hashedInputStringBuilder.ToString().ToLower();
|
|||
|
}
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = null;
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|