65.9K
CodeProject is changing. Read more.
Home

Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.57/5 (6 votes)

Aug 23, 2007

CPOL

1 min read

viewsIcon

61750

downloadIcon

1802

Encrypt and decrypt simple strings using the Rijndael algorithm.

Introduction

In this article, I will describe how to decrypt/encrypt strings using the Rijndael algorithm. The key is generated out of a password. I had to write an application which encrypts a personal number using the AES/Rijndael method.

Background

The data encryption method Rijndael (spoken "rheindahl") was developed by Joan Daemen and Vincent Rijmen. The Rijndael algorithm has got a block size of 28, 192, or 256 bits, and a variable key length of 128, 192, or 256 bits. In AES, the algorithm is fixed to a block size of 128 bits.

Using the Code

I wrote two methods, one to encrypt the string and the other to decrypt it. First, the Encrypt method:

Parameters:

  • string Text
  • byte[] key
  • byte[] VectorBytes

The init vector should be the same at each time of encryption/decryption.

public static string Encrypt(string Text, byte[] key, byte[] VectorBytes){
    try{
        byte[] TextBytes = Encoding.UTF8.GetBytes(Text);        
        RijndaelManaged rijKey = new RijndaelManaged();
        rijKey.Mode = CipherMode.CBC; 
        ICryptoTransform encryptor = rijKey.CreateEncryptor(key,VectorBytes); 
        MemoryStream memoryStream = new MemoryStream(); 
        cryptoStream.Write(TextBytes, 0, TextBytes.Length); 
        cryptoStream.FlushFinalBlock(); 
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close(); 
        string cipherText = Convert.ToBase64String(cipherTextBytes); 
        return cipherText;
    } 
    catch (Exception e){
        MessageBox.Show("Falsches Passwort "+ e.Message.ToString());
        string t = "";
        return t;
    }
}

The three parameters given to the Encrypt method are the string which should be encrypted, a byte array containing the key, and a byte array containing the initialization vector. First, we have to convert the string Text to a byte array. After that, the RijndealManaged object is created. The CipherMode is set to CBC (more information on cipher mode is available here). The encryption object is now generated and the key and vector are given to it. The memory stream buffers the data coming out of cryptoStream. Then, the data the memoryStream contains is saved to the byte array cipherTextBytes. We then convert ciphreTextBytes, and voila, we can now return the encrypted string.

The Decrypt method is nearly the same, so I won't explain its function.

Parameters:

  • string Text
  • byte[] keyBytes
  • byte[] VectorBytes
public static string Decrypt(string Text, byte[] keyBytes, byte[] VectorBytes)
{
    try{
        byte[] TextBytes = Convert.FromBase64String(Text); 
        RijndaelManaged rijKey = new RijndaelManaged();
        rijKey.Mode = CipherMode.CBC;
        ICryptoTransform decryptor = rijKey.CreateDecryptor(keyBytes,VectorBytes);
        MemoryStream memoryStream = new MemoryStream(TextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream,decryptor,
          CryptoStreamMode.Read);
        byte[] pTextBytes = new byte[TextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(pTextBytes,0,pTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();
        string plainText = Encoding.UTF8.GetString(pTextBytes,0,decryptedByteCount);
        return plainText;
    }catch (Exception a){
        MessageBox.Show("Falsches Passwort "+ a.Message.ToString());
        string t = "";
        return t;
    }
}

For more details, go to www.cute-solutions.de or take a look at the sample project.