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:
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.
Encryption
byte[] en;
ICryptoTransform ict = aes.CreateEncryptor(key, iv);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(text);
sw.Close();
en = ms.ToArray();
Decryption
ICryptoTransform ict = aes.CreateDecryptor(key, iv);
MemoryStream ms = new MemoryStream(text);
CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read);
StreamReader sw = new StreamReader(cs);
string str=sw.ReadToEnd(text);
sw.Close();
Mail
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("abc@example.com");
message.From = new System.Net.Mail.MailAddress("xyz@example.com");
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.gmail.com";
smtp.Port = 587;
Then use Send()
function to send the message
smtp.EnableSsl = true;
smtp.Send(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.