Introduction
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.
Background
Before starting, I would suggest to have a basic knowledge of C# and cryptography.
Modules
The code consists of two modules: AES encryption/decryption and Mail.
Using the code
The Namespaces
using System.Net;
Provides the classes and functions used in the program by the user for sending the mail.
using System.Security.Cryptography;
Provides the classes and functions related to the encryption.
The Code
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.
Encryption
byte[] en;
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
Decryption
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
Mail
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("[email protected]");// the to field
message.From = new System.Net.Mail.MailAddress("[email protected]");// the from field
message.Subject = "Encrypted mail";
message.Body = "Encrypted text with key and iv";
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Kuppurasu NagarajPosted Apr 11, 2016, 1:57 PM
Nice Sharing
Rumman SiddiquiPosted Dec 20, 2012, 4:57 AM
Just a simple step for more secure results.
Sam HobbsPosted Dec 19, 2012, 9:43 PM
Thank you, Rumman. I think it shoud be mentioned that most email client programs have a way to send and receive encrypted messages so if that is used then it is not necessary to do any further programming.