mirror of
https://github.com/Muskingum-County-Library-System/Send2Email.git
synced 2025-04-19 11:59:49 -04:00
Updated to use AES encryption for better security
This commit is contained in:
parent
68ef7ecdfc
commit
d8e0591dba
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
SendEmail/bin/SendEmail/obj/
|
SendEmail/bin/
|
||||||
|
SendEmail/obj/
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -27,8 +27,8 @@ namespace SendEmail
|
|||||||
private void PassButtonClick(object sender, EventArgs e)
|
private void PassButtonClick(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
string input = textBox1.Text;
|
string input = textBox1.Text;
|
||||||
string encryptedInput = Program.Encrypt(input); // Encrypt the user input
|
string decryptedConfigPass = Program.Decrypt(Program.config_pass);
|
||||||
if (encryptedInput == Program.config_pass) // If encrypted password is correct
|
if (input == decryptedConfigPass)
|
||||||
{
|
{
|
||||||
this.Hide(); // Hide Form2
|
this.Hide(); // Hide Form2
|
||||||
var form3 = new Form3(); // Create Form3
|
var form3 = new Form3(); // Create Form3
|
||||||
|
@ -132,10 +132,8 @@ namespace SendEmail
|
|||||||
|
|
||||||
int cfg_port = Int32.Parse(string_cfg_port);
|
int cfg_port = Int32.Parse(string_cfg_port);
|
||||||
|
|
||||||
string cfg_public_key = Program.key_public; // Get public key
|
|
||||||
|
|
||||||
// Create a JSON object with the configuration data
|
// Create a JSON object with the configuration data
|
||||||
Config cfg = new Config(cfg_host, cfg_port, cfg_user, cfg_pass, cfg_from, cfg_subject, cfg_body, cfg_extensions, cfg_config_pass, cfg_public_key);
|
Config cfg = new Config(cfg_host, cfg_port, cfg_user, cfg_pass, cfg_from, cfg_subject, cfg_body, cfg_extensions, cfg_config_pass);
|
||||||
|
|
||||||
// Serialize the JSON data so it can be written to a text file
|
// Serialize the JSON data so it can be written to a text file
|
||||||
string[] json = { JsonConvert.SerializeObject(cfg, Formatting.Indented) };
|
string[] json = { JsonConvert.SerializeObject(cfg, Formatting.Indented) };
|
||||||
@ -202,7 +200,6 @@ namespace SendEmail
|
|||||||
Program.config_pass = config.Config_Pass;
|
Program.config_pass = config.Config_Pass;
|
||||||
|
|
||||||
Program.file_extensions = config.File_Extensions;
|
Program.file_extensions = config.File_Extensions;
|
||||||
Program.key_public = config.Key_Public; // Load public key
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -274,7 +271,7 @@ namespace SendEmail
|
|||||||
public string File_Extensions { get; set; }
|
public string File_Extensions { get; set; }
|
||||||
public string Key_Public { get; set; } // New property
|
public string Key_Public { get; set; } // New property
|
||||||
|
|
||||||
public Config(string smtp_host, int smtp_port, string smtp_user, string smtp_pass, string mail_from, string mail_subject, string mail_body, string file_extensions, string config_pass, string key_public)
|
public Config(string smtp_host, int smtp_port, string smtp_user, string smtp_pass, string mail_from, string mail_subject, string mail_body, string file_extensions, string config_pass)
|
||||||
{
|
{
|
||||||
SMTP_Host = smtp_host;
|
SMTP_Host = smtp_host;
|
||||||
SMTP_Port = smtp_port;
|
SMTP_Port = smtp_port;
|
||||||
@ -287,7 +284,6 @@ namespace SendEmail
|
|||||||
|
|
||||||
File_Extensions = file_extensions;
|
File_Extensions = file_extensions;
|
||||||
Config_Pass = config_pass;
|
Config_Pass = config_pass;
|
||||||
Key_Public = key_public; // New property
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -6,6 +6,7 @@ using System.Globalization;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace SendEmail
|
namespace SendEmail
|
||||||
{
|
{
|
||||||
@ -36,8 +37,8 @@ namespace SendEmail
|
|||||||
public static string config_pass;
|
public static string config_pass;
|
||||||
|
|
||||||
// Encryption keys (8 bytes for DES)
|
// Encryption keys (8 bytes for DES)
|
||||||
public static string key_secret = "8byteKey";
|
public static string EncryptionKey = "8byteKey";
|
||||||
public static string key_public; // Moved to config file
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Main Method
|
#region Main Method
|
||||||
@ -72,77 +73,66 @@ namespace SendEmail
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Encryption Methods
|
#region Encryption Methods
|
||||||
public static string Encrypt(string input)
|
public static string Encrypt(string plainText)
|
||||||
{
|
{
|
||||||
try
|
if (string.IsNullOrEmpty(plainText))
|
||||||
{
|
return string.Empty;
|
||||||
string textToEncrypt = input;
|
|
||||||
string ToReturn = "";
|
|
||||||
byte[] secretkeyByte = System.Text.Encoding.UTF8.GetBytes(key_secret);
|
|
||||||
byte[] publickeybyte = System.Text.Encoding.UTF8.GetBytes(key_public);
|
|
||||||
byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(textToEncrypt);
|
|
||||||
|
|
||||||
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
|
using (Aes aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = DeriveKey(EncryptionKey);
|
||||||
|
aes.GenerateIV(); // Generate a unique IV
|
||||||
|
|
||||||
|
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
{
|
{
|
||||||
des.Mode = CipherMode.CBC;
|
ms.Write(aes.IV, 0, aes.IV.Length); // Store IV at the start
|
||||||
des.Padding = PaddingMode.PKCS7;
|
using (var cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
|
||||||
des.Key = publickeybyte;
|
using (var writer = new StreamWriter(cryptoStream))
|
||||||
des.IV = secretkeyByte;
|
|
||||||
|
|
||||||
using (MemoryStream ms = new MemoryStream())
|
|
||||||
{
|
{
|
||||||
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
|
writer.Write(plainText);
|
||||||
{
|
|
||||||
cs.Write(inputbyteArray, 0, inputbyteArray.Length);
|
|
||||||
cs.FlushFinalBlock();
|
|
||||||
ToReturn = Convert.ToBase64String(ms.ToArray());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Convert.ToBase64String(ms.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
return ToReturn;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new Exception(ex.Message, ex.InnerException);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Decrypt(string input)
|
|
||||||
|
public static string Decrypt(string encryptedText)
|
||||||
{
|
{
|
||||||
try
|
if (string.IsNullOrEmpty(encryptedText))
|
||||||
{
|
return string.Empty;
|
||||||
string textToDecrypt = input;
|
|
||||||
string ToReturn = "";
|
|
||||||
byte[] privatekeyByte = System.Text.Encoding.UTF8.GetBytes(key_secret);
|
|
||||||
byte[] publickeybyte = System.Text.Encoding.UTF8.GetBytes(key_public);
|
|
||||||
byte[] inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ", "+"));
|
|
||||||
|
|
||||||
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
|
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
|
||||||
|
using (Aes aes = Aes.Create())
|
||||||
|
{
|
||||||
|
aes.Key = DeriveKey(EncryptionKey);
|
||||||
|
byte[] iv = new byte[aes.BlockSize / 8];
|
||||||
|
|
||||||
|
if (cipherTextBytes.Length < iv.Length)
|
||||||
|
return string.Empty; // Prevents IV-related decryption errors
|
||||||
|
|
||||||
|
Array.Copy(cipherTextBytes, iv, iv.Length); // Extract stored IV
|
||||||
|
|
||||||
|
using (var decryptor = aes.CreateDecryptor(aes.Key, iv))
|
||||||
|
using (var ms = new MemoryStream(cipherTextBytes, iv.Length, cipherTextBytes.Length - iv.Length))
|
||||||
|
using (var cryptoStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
|
||||||
|
using (var reader = new StreamReader(cryptoStream))
|
||||||
{
|
{
|
||||||
des.Mode = CipherMode.CBC;
|
return reader.ReadToEnd();
|
||||||
des.Padding = PaddingMode.PKCS7;
|
|
||||||
des.Key = publickeybyte;
|
|
||||||
des.IV = privatekeyByte;
|
|
||||||
|
|
||||||
using (MemoryStream ms = new MemoryStream())
|
|
||||||
{
|
|
||||||
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
|
|
||||||
{
|
|
||||||
cs.Write(inputbyteArray, 0, inputbyteArray.Length);
|
|
||||||
cs.FlushFinalBlock();
|
|
||||||
ToReturn = System.Text.Encoding.UTF8.GetString(ms.ToArray());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ToReturn;
|
|
||||||
}
|
|
||||||
catch (Exception ae)
|
|
||||||
{
|
|
||||||
throw new Exception(ae.Message, ae.InnerException);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static byte[] DeriveKey(string passphrase)
|
||||||
|
{
|
||||||
|
using (SHA256 sha256 = SHA256.Create())
|
||||||
|
{
|
||||||
|
return sha256.ComputeHash(Encoding.UTF8.GetBytes(passphrase));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Email Methods
|
#region Email Methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user