Click here to Skip to main content
13,259,427 members (51,844 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 23 Aug 2007

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.


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.


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:


  • 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){
        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); 
        byte[] cipherTextBytes = memoryStream.ToArray();
        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.


  • string Text
  • byte[] keyBytes
  • byte[] VectorBytes
public static string Decrypt(string Text, byte[] keyBytes, byte[] VectorBytes)
        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,
        byte[] pTextBytes = new byte[TextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(pTextBytes,0,pTextBytes.Length);
        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 or take a look at the sample project.


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


About the Author

Web Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

Questionrijndael Pin
Md Adam17-Jun-14 0:10
memberMd Adam17-Jun-14 0:10 
Questionhow to implemented using java swing Pin
code99031-Oct-12 17:44
membercode99031-Oct-12 17:44 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:56
memberTony Selke27-Sep-07 7:56 
QuestionWhy use streams? Pin
hal9000lives10-Sep-07 4:35
memberhal9000lives10-Sep-07 4:35 
AnswerRe: Why use streams? Pin
Colin Angus Mackay10-Sep-07 5:24
mvpColin Angus Mackay10-Sep-07 5:24 
GeneralRe: Why use streams? Pin
hal9000lives10-Sep-07 5:37
memberhal9000lives10-Sep-07 5:37 
GeneralRe: Why use streams? Pin
Colin Angus Mackay10-Sep-07 7:57
mvpColin Angus Mackay10-Sep-07 7:57 
GeneralRe: Why use streams? [modified] Pin
cute-solutions11-Sep-07 2:40
membercute-solutions11-Sep-07 2:40 

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
Web03 | 2.8.171114.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by cute-solutions
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid