Click here to Skip to main content
12,751,306 members (33,189 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 14 Dec 2012

Secure Mailing

, 14 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
An application which allows user to create the encrypted message and then emails it to the desired person.


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.


Before starting, I would suggest to have a basic knowledge of C# and cryptography.


The code consits of two modules: AES encryption/decryption and Mail.


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:

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 AES, .NET provides the various classes. Here we use AesCryptoServiceProvider class to encryp 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.


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


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

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

Now prepare the message and the required details

message.To.Add("");// the to field
message.From = new System.Net.Mail.MailAddress("");// 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");

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

smtp.Host = "";
smtp.Port = 587;

Then use Send() function to send the message

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.


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


About the Author

Help desk / Support
India India
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 14 Dec 2012
Article Copyright 2012 by Rumman92
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid