Click here to Skip to main content
13,198,850 members (46,467 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 30 Jun 2004

Applied Cryptography Part 1: Simple tool to enrcypt/decrypt e-mail messages

, 30 Jun 2004
Rate this:
Please Sign up or sign in to vote.
There is a lot of .NET cryptography articles but not many sample applications using it. This article (part 1) introduces an application that enables to send secure messages via email.

Sample image


Have you ever wondered if your e-mails are secure and secret? Does your network administrator have possibility to read all your correspondence? Do you value your privacy?

If any of the answers is "yes", this article can help you keep your private emails private.

Sample encrypted message looks like this:

-== Begin encryption ==-
-== End encryption ==-

The Application

MailCryptNet is a tool that enables to encrypt any message you want to send via email. Encryption uses Rijndael algorithm and RijndaelManaged class from System.Security.Cryptography namespace. Only thing you have to do is to give your friends a copy of MailCryptNet and prepare passwords for them.

You may ask "why should I bother preparing any passwords? Isn't it enough to give the tool to my friends?". It prevents the case when other people (including your hated admin) downloads the tool and decrypts your messages. Creating secret passwords for your friends ensures you that no one else can read your message.

Creating passwords and keys

Password creation is a bit complicated because Rijndael is a symmetric algorithm which uses the same key to encrypt and decrypt data. In such cases, all your friends (including you) would have to use the same password. But nobody likes to share his password (as it is no longer secret).

But there is a solution to use symmetric algorithm and different passwords. The trick is that there are two levels of passwords. First level is common to all trusted clients, and second level is user-specific and is used to encrypt the first-level one.

For example, all trusted clients (you and your friends) use first level password like that: "oh fishy fishy fishy fish!" (or any other weird and twisted text), then every user creates his own private password to encrypt the first-level one. This is a second-level password.

The algorithm - RijndaelManaged

Why RijndaelManaged? Because it is strong, fast and managed that ensures to run on every machine with .NET framework installed.

Base64 encoding

E-mail messages are sent with pure text format. Encrypted data is a binary format. Therefore, it is necessary to do some conversion between those formats. Base64 is a well known and broadly used method to send binary data as pure text. To do it, Microsoft implemented very useful methods called ToBase64String and FromBase64String located in Convert namespace.


What does MailCryptNet do:

  • on startup, it reads .config file in order to find encrypted keys.
  • if stored keys are empty, it enables to create new encryption keys.
  • when .config contains encrypted key, it is decrypted using your private (second level) password.
  • decrypted keys are used by Rijndael algorithm.
  • any message typed in RichEdit is encrypted (including RTF formatting) and placed on Clipboard.
  • encrypted message can be restored from Clipboard and after decrypting is placed in RichEdit.

Preparing passwords

Default password is "secret". You can test it by copying encrypted message from the beginning of the article and pressing "decrypt" button. To change the password, do the following:

  • remove Key and IV values from .config file.
  • run MailCryptNet.
  • enter first-level password (common for all friends).
  • enter second-level password - your private password.
  • copy generated keys into .config file.
  • restart application.


Normal usage usually involves following steps:

  • type the message.
  • click the "Encrypt to Clipboard" button.
  • create new email (in your email application).
  • paste text from Clipboard.
  • send email.

When receiving encrypted email, do the following:

  • copy email text to Clipboard.
  • in MailCryptNet, click "Decrypt from Clipboard" button.
  • read a message.

The code

Using PasswordDeriveBytes function:

void getKeysFromPassword(string pass,
           out byte[] rijnKey, out byte[] rijnIV)

    byte[] salt =
      System.Text.Encoding.ASCII.GetBytes("some text to give it a bit salt");
    PasswordDeriveBytes pb = new PasswordDeriveBytes(pass,salt);

    //generate key and iv
    rijnKey = pb.GetBytes(32);
    rijnIV = pb.GetBytes(16);


Converting to Base64 blocks:

string makeBlocksFromString(string s)
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    sb.Append("-== Begin encryption ==-\r\n");

    int lineLength = 0;
    for (int i=0;i<s.Length;i++)
        char z = s[i];
        //add "new line" when block width met
        if (lineLength == blockLength)
            lineLength = 0;
    //if the last line shorter than block width - add new line
    if (lineLength > 0)
    sb.Append("-== End encryption ==-\r\n");
    return sb.ToString();
string makeStringFromBlocks(string s)
    char[]seps = new char[] {'\r','\n'};
    string[] tab = s.Split(seps);
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    bool wasBegin = false;
    for (int i=0;i<tab.Length;i++)
        if (tab[i] == "-== Begin encryption ==-")
            wasBegin = true;

        if (wasBegin)
            if (tab[i] == "-== End encryption ==-")
                return sb.ToString();
    throw new
      CryptographicException("missing begin block - end block definitions");

Rijndael encryption:

EncryptData method is used to encode message to be secret.

private  byte[] EncryptData(byte[] dataToEncrypt,
                                  byte[] rijnKey, byte[] rijnIV)
    RijndaelManaged rijn = new RijndaelManaged();
    //Get an encryptor.
    ICryptoTransform encryptor = rijn.CreateEncryptor(rijnKey, rijnIV);

    //Encrypt the data.
    MemoryStream msEncrypt = new MemoryStream();
    CryptoStream csEncrypt = new CryptoStream(msEncrypt,
                         encryptor, CryptoStreamMode.Write);

    //Write all data to the crypto stream and flush it.
    csEncrypt.Write(dataToEncrypt, 0, dataToEncrypt.Length);

    //Get encrypted array of bytes.
    return msEncrypt.ToArray();

Rijndael decryption:

DecryptData method is used to decode secret message.

private  byte[] DecryptData(byte[] dataToDecrypt,
                                 byte[] rijnKey, byte[] rijnIV)
    RijndaelManaged rijn = new RijndaelManaged();
    //Get a decryptor.
    ICryptoTransform decryptor =
           rijn.CreateDecryptor(rijnKey, rijnIV);

    //Now decrypt the message
    MemoryStream msDecrypt = new MemoryStream(dataToDecrypt);
    CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                           decryptor, CryptoStreamMode.Read);

    byte[] fromEncrypt = new byte[dataToDecrypt.Length];

    //Read the data out of the crypto stream.
    int len = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

    byte[] decrypted = new byte[len];
    Array.Copy( fromEncrypt,decrypted,len);

    return decrypted;

Button and Clipboard handlers:

private void btnEncrypt_Click(object sender, System.EventArgs e)
    string toEncrypt = richTextBox1.Rtf;
    byte[] toEncryptBuf = System.Text.Encoding.ASCII.GetBytes(toEncrypt);

    //Rijndael encryption
        byte[] encryptedBuf =
        string encryptedString =
    catch (FormatException ex)
    catch (CryptographicException ex)

private void btnDecrypt_Click(object sender, System.EventArgs e)
    IDataObject iData = Clipboard.GetDataObject();
    if (iData.GetDataPresent(DataFormats.Text))
      //Rijndael decryption
        string encryptedString =

        byte[] encryptedBuf = Convert.FromBase64String(encryptedString);
        byte[] decryptedBuf = DecryptData(encryptedBuf,
        string plainText =
        richTextBox1.Rtf = plainText;

      catch (FormatException ex)
      catch (CryptographicException ex)

Future enhancements

There are some features that could be added to this software:

  • Isolated Storage support - for multiple users to use the application with separate passwords. Instead of .config file, user passwords can be stored in Isolated Storage.
  • Some text formatting while typing message - at the moment, formatting is possible only when you paste formatted text into MailCryptNet edit window. "Reset" button removes the formatting.
  • Some integration with email applications for automatic message encoding/decoding while sending emails.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Marcin Cuprjak
Poland Poland
No Biography provided

You may also be interested in...


Comments and Discussions

NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:07
memberTony Selke27-Sep-07 7:07 
GeneralThanks for sharing Pin
David7774-Jul-05 0:53
memberDavid7774-Jul-05 0:53 
Generalbinary data encryption Pin
sebseb4229-Aug-04 4:07
susssebseb4229-Aug-04 4:07 
GeneralRe: binary data encryption Pin
Vlad Tepes29-Aug-04 8:03
memberVlad Tepes29-Aug-04 8:03 
GeneralThis is kinda on subject but not really Pin
Adam Rogas10-Jul-04 16:22
memberAdam Rogas10-Jul-04 16:22 
GeneralRe: This is kinda on subject but not really Pin
willh82016-Jan-06 14:11
memberwillh82016-Jan-06 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 1 Jul 2004
Article Copyright 2004 by Marcin Cuprjak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid