diff --git a/.gitignore b/.gitignore index 29c1e61..fa59f04 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -SendEmail/bin/SendEmail/obj/ +SendEmail/bin/ +SendEmail/obj/ diff --git a/.vs/Send2Email/FileContentIndex/d19558e7-9a35-45c3-bb5c-9512209306da.vsidx b/.vs/Send2Email/FileContentIndex/d19558e7-9a35-45c3-bb5c-9512209306da.vsidx new file mode 100644 index 0000000..86368e4 Binary files /dev/null and b/.vs/Send2Email/FileContentIndex/d19558e7-9a35-45c3-bb5c-9512209306da.vsidx differ diff --git a/.vs/Send2Email/v17/.suo b/.vs/Send2Email/v17/.suo index 209ef9e..2f5854f 100644 Binary files a/.vs/Send2Email/v17/.suo and b/.vs/Send2Email/v17/.suo differ diff --git a/SendEmail/Form2.cs b/SendEmail/Form2.cs index 36194b3..bad855b 100644 --- a/SendEmail/Form2.cs +++ b/SendEmail/Form2.cs @@ -27,8 +27,8 @@ namespace SendEmail private void PassButtonClick(object sender, EventArgs e) { string input = textBox1.Text; - string encryptedInput = Program.Encrypt(input); // Encrypt the user input - if (encryptedInput == Program.config_pass) // If encrypted password is correct + string decryptedConfigPass = Program.Decrypt(Program.config_pass); + if (input == decryptedConfigPass) { this.Hide(); // Hide Form2 var form3 = new Form3(); // Create Form3 diff --git a/SendEmail/Form3.cs b/SendEmail/Form3.cs index 868616a..36c09d4 100644 --- a/SendEmail/Form3.cs +++ b/SendEmail/Form3.cs @@ -132,10 +132,8 @@ namespace SendEmail 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 - 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 string[] json = { JsonConvert.SerializeObject(cfg, Formatting.Indented) }; @@ -202,7 +200,6 @@ namespace SendEmail Program.config_pass = config.Config_Pass; Program.file_extensions = config.File_Extensions; - Program.key_public = config.Key_Public; // Load public key return true; } @@ -274,7 +271,7 @@ namespace SendEmail public string File_Extensions { get; set; } 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_Port = smtp_port; @@ -287,7 +284,6 @@ namespace SendEmail File_Extensions = file_extensions; Config_Pass = config_pass; - Key_Public = key_public; // New property } } #endregion diff --git a/SendEmail/Program.cs b/SendEmail/Program.cs index a39c80e..c04c007 100644 --- a/SendEmail/Program.cs +++ b/SendEmail/Program.cs @@ -6,6 +6,7 @@ using System.Globalization; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Security.Cryptography; +using System.Text; namespace SendEmail { @@ -36,8 +37,8 @@ namespace SendEmail public static string config_pass; // Encryption keys (8 bytes for DES) - public static string key_secret = "8byteKey"; - public static string key_public; // Moved to config file + public static string EncryptionKey = "8byteKey"; + #endregion #region Main Method @@ -72,77 +73,66 @@ namespace SendEmail #endregion #region Encryption Methods - public static string Encrypt(string input) + public static string Encrypt(string plainText) { - try - { - 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); + if (string.IsNullOrEmpty(plainText)) + return string.Empty; - 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; - des.Padding = PaddingMode.PKCS7; - des.Key = publickeybyte; - des.IV = secretkeyByte; - - using (MemoryStream ms = new MemoryStream()) + ms.Write(aes.IV, 0, aes.IV.Length); // Store IV at the start + using (var cryptoStream = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) + using (var writer = new StreamWriter(cryptoStream)) { - using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) - { - cs.Write(inputbyteArray, 0, inputbyteArray.Length); - cs.FlushFinalBlock(); - ToReturn = Convert.ToBase64String(ms.ToArray()); - } + writer.Write(plainText); } + 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 - { - 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(" ", "+")); + if (string.IsNullOrEmpty(encryptedText)) + return string.Empty; - 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; - 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 reader.ReadToEnd(); } - - 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 #region Email Methods