Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Secure Mailing

5.00/5 (1 vote)
14 Dec 2012CPOL2 min read 8.7K   292  
An application which allows user to create the encrypted message and then emails it to the desired person.

Introduction

This application an be used to encrypt the text message using AES encryption as well as sends 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 consits of two modules: AES encryption/decryption and Mail.

AES:

In January 1997, NIST called for cryptographers to develop a new encryption system. As with the call for candidates from which DES was selected, NIST made several important restrictions. The algorithms had to be

  • unclassified 

  • publicly disclosed

  • available royalty-free for use worldwide

  • symmetric block cipher algorithms, for blocks of 128 bits

  • usable with key sizes of 128, 192, and 256 bits

In August 1998, fifteen algorithms were chosen from among those submitted; in August 1999, the field of candidates was narrowed to five finalists. The five then underwent extensive public and private scrutiny. The final selection was made on the basis not only of security but also of cost or efficiency of operation and ease of implementation in software. The winning algorithm, submitted by two Dutch cryptographers, was Rijndael (pronounced RINE dahl or, to hear the inventors pronounce it themselves, the algorithm's name is derived from the creators' names, Vincent Rijmen and Joan Daemen. (NIST described the four not chosen as also having adequate security for the AESno cryptographic flaws were identified in any of the five. Thus, the selection was based on efficiency and implementation characteristics.)

The AES was adopted for use by the U.S. government in December 2001 and became Federal Information Processing Standard 197 [NIS01].

Rijndael is a fast algorithm that can be implemented easily on simple processors. Although it has a strong mathematical foundation, it primarily uses substitution; transposition; and the shift, exclusive OR, and addition operations. Like DES, AES uses repeat cycles. There are 10, 12, or 14 cycles for keys of 128, 192, and 256 bits, respectively. In Rijndael, the cycles are called "rounds. 

Using the code 

The Namespaces:

C#
using System.Net;  

provides the classes and functions used in the program by the user for sending the mail.

C#
using System.Security.Cryptography;

provides the classes and functions related to the encryption.

The Code

To encrypt with AES, .NET provides the various classes. Here we use AesCryptoServiceProvider class to encryp and decrypt the given text. 

C#
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

C#
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 

C#
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 mailmessage class as:

C#
 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

Now prepare the message and the required details

C#
message.To.Add("abc@example.com");// the to field
message.From = new System.Net.Mail.MailAddress("xyz@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:

C#
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

Enter the required credentials needed for login

C#
smtp.Credentials = new NetworkCredential("your username", "your password");

Give the SMTP host and port number (I used gmail here)

C#
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;

Then use Send() function to send the message

C#
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 soucre and exe download the attachments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)