Click here to Skip to main content
Licence CPOL
First Posted 23 Aug 2007
Views 18,153
Downloads 312
Bookmarked 17 times

Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key

By | 23 Aug 2007 | Article
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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
NewsTwo other related encryption articles in CodeProject ... PinmemberTony Selke6:56 27 Sep '07  
QuestionWhy use streams? Pinmemberhal9000lives3:35 10 Sep '07  
AnswerRe: Why use streams? PinmvpColin Angus Mackay4:24 10 Sep '07  
GeneralRe: Why use streams? Pinmemberhal9000lives4:37 10 Sep '07  
GeneralRe: Why use streams? PinmvpColin Angus Mackay6:57 10 Sep '07  
GeneralRe: Why use streams? [modified] Pinmembercute-solutions1:40 11 Sep '07  

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.

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