This application an be used to encrypt a text message using AES encryption as well as send it to the person whose email is provided. This could be used as the security software for any of us. But to decrypt the encrypted text one must have the same application.
This application can be used to encrypt a text message using AES encryption as well as send it to the person whose email is provided. This could be used as the security software for any of us. But to decrypt the encrypted text one must have the same application.
Before starting, I would suggest to have a basic knowledge of C# and cryptography.
The code consists of two modules: AES encryption/decryption and Mail.
Using the code
Provides the classes and functions used in the program by the user for sending the mail.
Provides the classes and functions related to the encryption.
To encrypt with the AES, .Net provides various classes. Here we use the AesCryptoServiceProvider class to encrypt and decrypt the given text.
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
byte key = aes.Key;
byte iv = aes.IV;
The key and iv (initialization vector) is used to encrypt the text.
ICryptoTransform ict = aes.CreateEncryptor(key, iv);// create the encryptor
MemoryStream ms = new MemoryStream();// To store the encrypted data into the stream(which is hold in memory) so as it could be used as per requirement.
CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);//Is used to link the stream to cryptographic transformations.
StreamWriter sw = new StreamWriter(cs);//used to write the the data in the stream.
sw.Write(text);// write all text to the stream
sw.Close();// close the stream
en = ms.ToArray(); // store the contents
ICryptoTransform ict = aes.CreateDecryptor(key, iv);// create the decryptor
MemoryStream ms = new MemoryStream(text);// created with the text it has to decrypt
CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read);//Is used in read mode
StreamReader sw = new StreamReader(cs);//used to read the data from the stream.
string str = sw.ReadToEnd(text);// write all text to the stream
sw.Close();// close the stream
Create an object of the mailmessage class as:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
Now prepare the message and the required details, as in:
message.To.Add("firstname.lastname@example.org");// the to field
message.From = new System.Net.Mail.MailAddress("email@example.com");// the from field
message.Subject = "Encrypted mail";
message.Body = "Encrypted text with key and iv";
Create the SMTP client object so as to send the mail as:
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
Enter the required credentials needed for login:
smtp.Credentials = new NetworkCredential("your username", "your password");
Provide the SMTP host and port number (I used Gmail here):
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
Then use the Send() function to send the message, as in:
smtp.EnableSsl = true; // enable secure login (optional)
smtp.Send(message); // send the message
Now when the user receives the message he will see the encrypted message with key and iv.
*For full source and exe download the attachments.