193 lines
5.9 KiB
C#
193 lines
5.9 KiB
C#
|
using Org.BouncyCastle.Crypto;
|
|||
|
using Org.BouncyCastle.Crypto.IO;
|
|||
|
using Org.BouncyCastle.Crypto.Parameters;
|
|||
|
using Org.BouncyCastle.Security;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Security.Cryptography;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
|
|||
|
namespace PasswortEncryptor
|
|||
|
{
|
|||
|
public partial class MainForm : Form
|
|||
|
{
|
|||
|
private static string MemoryMasterKey = "!ZEqy2Zsb#VK6<8`H6;W~VxJ$r:w.{ffzwDt=<yKC6m6N3;T<9nyeF&+.4&D@rhK";
|
|||
|
|
|||
|
public MainForm()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public static string Encrypt(string value)
|
|||
|
{
|
|||
|
string result = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
IBufferedCipher encCipher = CipherUtilities.GetCipher("AES/GCM/NoPadding");
|
|||
|
byte[] keyBytes = GetKey(MemoryMasterKey);
|
|||
|
KeyParameter key = new KeyParameter(keyBytes);
|
|||
|
byte[] nonce = new byte[12];
|
|||
|
RandomNumberGenerator.Create().GetBytes(nonce);
|
|||
|
encCipher.Init(true, new ParametersWithIV(key, nonce));
|
|||
|
byte[] plainText = Encoding.UTF8.GetBytes(value);
|
|||
|
byte[] chiperBuffer = null;
|
|||
|
byte[] tag = new byte[16];
|
|||
|
byte[] resultBuffer = null;
|
|||
|
|
|||
|
using (MemoryStream memStream = new MemoryStream())
|
|||
|
{
|
|||
|
using (CipherStream encStream = new CipherStream(memStream, null, encCipher))
|
|||
|
{
|
|||
|
encStream.Write(plainText, 0, plainText.Length);
|
|||
|
}
|
|||
|
|
|||
|
chiperBuffer = memStream.ToArray();
|
|||
|
|
|||
|
resultBuffer = nonce.Concat(chiperBuffer).ToArray();
|
|||
|
}
|
|||
|
|
|||
|
result = Convert.ToBase64String(resultBuffer);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
result = null;
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public static string Decrypt(string value)
|
|||
|
{
|
|||
|
string result = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
IBufferedCipher decCipher = CipherUtilities.GetCipher("AES/GCM/NoPadding");
|
|||
|
byte[] keyBytes = GetKey(MemoryMasterKey);
|
|||
|
KeyParameter key = new KeyParameter(keyBytes);
|
|||
|
byte[] combined = Convert.FromBase64String(value);
|
|||
|
byte[] tag = combined.Skip(combined.Length - 16).ToArray();
|
|||
|
byte[] nonce = combined.Take(12).ToArray();
|
|||
|
byte[] chiperText = combined.Skip(nonce.Length).Take(combined.Length - nonce.Length).ToArray();
|
|||
|
byte[] plainTextBuffer = null;
|
|||
|
|
|||
|
decCipher.Init(false, new ParametersWithIV(key, nonce));
|
|||
|
|
|||
|
using (MemoryStream memStream = new MemoryStream(chiperText, false))
|
|||
|
{
|
|||
|
MemoryStream dataStream = new MemoryStream();
|
|||
|
|
|||
|
using (dataStream)
|
|||
|
{
|
|||
|
using (CipherStream decStream = new CipherStream(memStream, (IBufferedCipher)decCipher, null))
|
|||
|
{
|
|||
|
int ch;
|
|||
|
|
|||
|
while ((ch = decStream.ReadByte()) >= 0)
|
|||
|
{
|
|||
|
dataStream.WriteByte((byte)ch);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
plainTextBuffer = dataStream.ToArray();
|
|||
|
|
|||
|
result = Encoding.UTF8.GetString(plainTextBuffer);
|
|||
|
}
|
|||
|
}
|
|||
|
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)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
private void cbxTemplate_SelectedIndexChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if(cbxTemplate.SelectedIndex == 1)
|
|||
|
{
|
|||
|
txtInput.Text = "Server=myServer;Uid=myUser;Database=myDatabase;Pwd=myPassword;SSLMode=None;Charset=utf8mb4";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void btnEncrypt_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
txtOutput.Text = Encrypt(txtInput.Text);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
txtOutput.Text = ex.Message;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|