Click here to Skip to main content
Click here to Skip to main content

Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key

, 23 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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.

License

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

About the Author

cute-solutions
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
Questionrijndael PinmemberMd Adam16-Jun-14 23:10 
Questionhow to implemented using java swing Pinmembercode99031-Oct-12 16:44 
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke27-Sep-07 6:56 
QuestionWhy use streams? Pinmemberhal9000lives10-Sep-07 3:35 
AnswerRe: Why use streams? PinmvpColin Angus Mackay10-Sep-07 4:24 
GeneralRe: Why use streams? Pinmemberhal9000lives10-Sep-07 4:37 
GeneralRe: Why use streams? PinmvpColin Angus Mackay10-Sep-07 6:57 
GeneralRe: Why use streams? [modified] Pinmembercute-solutions11-Sep-07 1:40 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by cute-solutions
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid