Updated to use Encryption for smtp_password, and added some QOL features

This commit is contained in:
unknown 2024-05-24 22:29:09 -04:00
parent ba537545e2
commit 3ec05584ad
15 changed files with 232 additions and 269 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
.vs/Scan2Email/v17/.suo Normal file

Binary file not shown.

View File

@ -1,7 +1,6 @@
using System;
using System.Windows.Forms;
namespace SendEmail
{
public partial class Form1 : Form
@ -10,7 +9,7 @@ namespace SendEmail
* Form1 is the main window for Send2Email
*/
#region UI
public Form1() //Autogenerated Code Block
public Form1()
{
InitializeComponent();
}
@ -42,9 +41,7 @@ namespace SendEmail
// Error Message for invalid email
MessageBox.Show(email + " is not a valid email address.\r\nPlease enter a valid email address.");
}
}
#endregion
// Check if enter key is pressed in email address input

View File

@ -18,7 +18,7 @@ namespace SendEmail
int wrongCount = 0; // Counts how many times the password is entered incorrectly
const int LIMIT = 5; // The amount of times a password can be tried before exiting
public Form2() //Autogenerated Code Block
public Form2()
{
InitializeComponent();
}
@ -27,7 +27,8 @@ namespace SendEmail
private void PassButtonClick(object sender, EventArgs e)
{
string input = textBox1.Text;
if (input == Program.smtp_pass) //If password is correct
string encryptedInput = Program.Encrypt(input); // Encrypt the user input
if (encryptedInput == Program.smtp_pass) // If encrypted password is correct
{
this.Hide(); // Hide Form2
var form3 = new Form3(); // Create Form3

View File

@ -24,7 +24,7 @@ namespace SendEmail
// Default info in the info textbox
string defaultInfo = "Helpful information about each section will display here when you hover over a textbox with your mouse.";
public Form3() //Automatically generated codeblock
public Form3()
{
InitializeComponent();
SetInfoBox(defaultInfo);
@ -43,7 +43,7 @@ namespace SendEmail
hostTB.Text = Program.smtp_host;
portTB.Text = Program.smtp_port.ToString();
emailTB.Text = Program.smtp_user;
passwordTB.Text = Program.smtp_pass;
passwordTB.Text = Program.Decrypt(Program.smtp_pass); // Decrypt password for display
fromTB.Text = Program.mail_from;
subjectTB.Text = Program.mail_subject;
@ -63,7 +63,7 @@ namespace SendEmail
switch (holdName) // Switch through the possible input fields and sets the output to the correct values
{
case "hostTB":
output = "SMTP Host: SMTP Host. At the time of creation, we utalize Google's SMTP server located at smtp.google.com";
output = "SMTP Host: SMTP Host. At the time of creation, we utilize Google's SMTP server located at smtp.google.com";
break;
case "portTB":
output = "SMTP Port: SMTP Port";
@ -101,13 +101,14 @@ namespace SendEmail
#region Save/Load Configuration
// Save the configuration data, will overwrite existing settings
// Save the configuration data, will overwrite existing settings
private void SaveConfig(object sender, EventArgs e)
{
string cfg_host = hostTB.Text;
string string_cfg_port = portTB.Text;
string cfg_user = emailTB.Text;
string cfg_pass = passwordTB.Text;
string cfg_pass = Program.Encrypt(passwordTB.Text); // Encrypt the password
string cfg_from = fromTB.Text;
string cfg_subject = subjectTB.Text;
@ -126,29 +127,52 @@ namespace SendEmail
// Write Config file to Program.appPath (default is Appdata/Local/Send2Email)
WriteFile(json, Program.configName, Program.configFileExt);
// Close Form3 and open Form1
this.Hide();
var form1 = new Form1();
form1.Closed += (s, args) => this.Close();
form1.Show();
}
public static bool LoadConfig() // Boolean method, returns true or false to whatever called LoadConfig
{
//Config filepath
string cfg = Program.appPath + "/" + Program.configFileName;
// Config filepath in AppData
string cfgFilePath = Path.Combine(Program.appPath, Program.configFileName);
// Example config filepath in the bin directory of the project
string exampleCfgFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "example.cfg.json");
//Check if config file exists
bool cExist = File.Exists(cfg);
// Check if config file exists in AppData
bool cExist = File.Exists(cfgFilePath);
if (!cExist) // If a config doesn't exist
{
// Check if example config exists in the bin directory of the project
if (File.Exists(exampleCfgFilePath))
{
// Ensure the directory exists
if (!Directory.Exists(Program.appPath))
{
Directory.CreateDirectory(Program.appPath);
}
// Copy the example config to the AppData config file path
File.Copy(exampleCfgFilePath, cfgFilePath);
}
else
{
// Display an error message before closing the application
MessageBox.Show("A critical error has occurred. Example configuration file is missing. Please contact IT to resolve this issue.");
return false; // Return false for LoadConfig boolean
}
else //Config file does exist
{
}
// Create an array to store the configuration file line by line
string[] cfgFile = ReadFile("cfg", "json");
string cfgData = ConvertStringArray(cfgFile);
// Get the first line of our configuration
Config config = JsonConvert.DeserializeObject<Config>(cfgData);
Program.smtp_host = config.SMTP_Host;
@ -165,7 +189,7 @@ namespace SendEmail
return true;
}
}
#endregion
@ -174,7 +198,6 @@ namespace SendEmail
// Create a string array with the lines of text
public static void WriteFile(string[] data, string fileName, string fileExtension)
{
string temp = "";
string file = fileName + "." + fileExtension;
@ -189,7 +212,6 @@ namespace SendEmail
}
}
public static string[] ReadFile(string fileName, string fileExtension)
{
string temp = "";
@ -223,10 +245,6 @@ namespace SendEmail
}
#region JSON Config
/* Sources
* https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
* https://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm
*/
public class Config
{
public string SMTP_Host { get; set; }
@ -256,11 +274,8 @@ namespace SendEmail
File_Extensions = file_extensions;
}
}
#endregion
#endregion
}
}

View File

@ -5,6 +5,7 @@ using System.Windows.Forms;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace SendEmail
{
@ -33,23 +34,34 @@ namespace SendEmail
public static string mail_body;
public static string mail_delivery;
public static string file_extensions;
// Encryption keys (8 bytes for DES)
public static string key_secret = "8byteKey";
public static string key_public = "byte8Key";
#endregion
#region Main Method
[STAThread]
static void Main() //Main method, this runs as soon as the software is started
static void Main()
{
// Set up the basic requirements for a WinForms application
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool cont = Form3.LoadConfig(); // Load Configuration method
if (cont) // If Config exists
{
if (string.IsNullOrEmpty(smtp_pass)) // Check if password is empty
{
// Run first time setup
Application.Run(new Form3());
}
else
{
Application.Run(new Form1()); // Open Form1 (Main Window)
}
}
else // If Config Doesn't Exist
{
// Display an error message before closing the application
@ -59,12 +71,85 @@ namespace SendEmail
}
#endregion
#region Encryption Methods
public static string Encrypt(string input)
{
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);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
des.Key = publickeybyte;
des.IV = secretkeyByte;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputbyteArray, 0, inputbyteArray.Length);
cs.FlushFinalBlock();
ToReturn = Convert.ToBase64String(ms.ToArray());
}
}
}
return ToReturn;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
public static string Decrypt(string input)
{
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(" ", "+"));
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
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 ToReturn;
}
catch (Exception ae)
{
throw new Exception(ae.Message, ae.InnerException);
}
}
#endregion
#region Email Methods
#region IsValidEmail
// Checks if the email address entered is valid.
//Source https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format
public static bool IsValidEmail(string email) //Accepts an email string and returns true if email is valid
public static bool IsValidEmail(string email)
{
if (string.IsNullOrWhiteSpace(email)) // Null strings aren't valid so return false
return false;
@ -72,8 +157,7 @@ namespace SendEmail
try
{
// Normalize the domain
email = Regex.Replace(email, @"(@)(.+)$", DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
email = Regex.Replace(email, @"(@)(.+)$", DomainMapper, RegexOptions.None, TimeSpan.FromMilliseconds(200));
// Examines the domain part of the email and normalizes it.
string DomainMapper(Match match)
@ -98,9 +182,7 @@ namespace SendEmail
try
{
return Regex.IsMatch(email,
@"^[^@\s]+@[^@\s]+\.[^@\s]+$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException)
{
@ -114,7 +196,6 @@ namespace SendEmail
// returns filepaths with matching extensions
public static string[] Attachments()
{
string[] extensions;
List<string> output = new List<string>(); // Holds the output
@ -147,17 +228,14 @@ namespace SendEmail
output.Add(files[i]); // Add it to the output
}
}
}
return output.ToArray(); // Return a list of all attachments
}
#endregion
#region Send Email
public static void Send(string sendTo) //Send email function. This is where most of the work starts
public static void Send(string sendTo)
{
string[] getAttachments = Attachments(); // Gets all valid attachment files in Pictures folder
@ -192,136 +270,8 @@ namespace SendEmail
MessageBox.Show("A critical error has occured please contact IT \r\n" + ex.ToString());
}
}
#endregion
#endregion
}
}
#region [ARCHIVE]
/* This code is commented out, but it might be
* useful if we ever need to work on the software
* again in the future, so I've kept it archived. */
#region [ARCHIVE] Encryption, Decryption
/*
//Methods used to encrypt and decrypt a string
//Source https://www.delftstack.com/howto/csharp/encrypt-and-decrypt-a-string-in-csharp/
public static string Encrypt(string input)
{
try
{
string textToEncrypt = input;
string ToReturn = "";
byte[] secretkeyByte = { };
secretkeyByte = System.Text.Encoding.UTF8.GetBytes(key_secret);
byte[] publickeybyte = { };
publickeybyte = System.Text.Encoding.UTF8.GetBytes(key_public);
MemoryStream ms = null;
CryptoStream cs = null;
byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(textToEncrypt);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateEncryptor(publickeybyte, secretkeyByte), CryptoStreamMode.Write);
cs.Write(inputbyteArray, 0, inputbyteArray.Length);
cs.FlushFinalBlock();
ToReturn = Convert.ToBase64String(ms.ToArray());
}
return ToReturn;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
static string Decrypt(string input)
{
try
{
string textToDecrypt = input;
string ToReturn = "";
byte[] privatekeyByte = { };
privatekeyByte = System.Text.Encoding.UTF8.GetBytes(key_secret);
byte[] publickeybyte = { };
publickeybyte = System.Text.Encoding.UTF8.GetBytes(key_public);
MemoryStream ms = null;
CryptoStream cs = null;
byte[] inputbyteArray = new byte[textToDecrypt.Replace(" ", "+").Length];
inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ", "+"));
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
ms = new MemoryStream();
cs = new CryptoStream(ms, des.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write);
cs.Write(inputbyteArray, 0, inputbyteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
ToReturn = encoding.GetString(ms.ToArray());
}
return ToReturn;
}
catch (Exception ae)
{
throw new Exception(ae.Message, ae.InnerException);
}
}
*/
#endregion
#region [ARCHIVE] Encrypt/Decrypt Files
/*
public static void WriteEncryptedFile(string[] data, string fileName, string fileExtension)
{
string temp = "";
string file = fileName + "." + fileExtension;
// Write the string array to a new file named "WriteLines.txt".
using (StreamWriter outputFile = new StreamWriter(Path.Combine(appPath, file)))
{
foreach (string line in data)
{
temp = Encrypt(line);
}
outputFile.WriteLine(temp);
}
}
public static string[] ReadEncryptedFile(string fileName, string fileExtension)
{
string temp = "";
string file = fileName + "." + fileExtension;
string[] lines = System.IO.File.ReadAllLines(Path.Combine(appPath, file));
string[] output = new string[lines.Length];
for (int i = 0; i < lines.Length; i++)
{
temp = Decrypt(lines[i]);
output[i] = temp;
}
return output;
}
*/
#endregion
#region [ARCHIVE] Keygen
/*
//Used origonally do generate public and secret keys
public static string GenerateKey(int length)
{
//Random
Random rand = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return new string(Enumerable.Repeat(chars, length).Select(s => s[rand.Next(s.Length)]).ToArray());
}
*/
#endregion
#endregion

View File

@ -2,7 +2,7 @@
"SMTP_Host": "smtp.gmail.com",
"SMTP_Port": 587,
"SMTP_User": "email@company.com",
"SMTP_Pass": "smtp_password",
"SMTP_Pass": "",
"Mail_From": "email@company.com",
"Mail_Subject": "Mail Subject Text",
"Mail_Body": "Mail Body Text",