12,753,195 members (39,643 online)
alternative version

#### Stats

34.2K views
24 bookmarked
Posted 22 Aug 2007

# Encrypt and Decrypt Data Using a Symmetric (Rijndael) Key

, 23 Aug 2007 CPOL
 Rate this:
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,
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.

## About the Author

 Web Developer Germany
No Biography provided

## You may also be interested in...

 Pro Pro

## Comments and Discussions

 First Prev Next
 rijndael Md Adam17-Jun-14 0:10 Md Adam 17-Jun-14 0:10
 how to implemented using java swing code99031-Oct-12 17:44 code990 31-Oct-12 17:44
 Two other related encryption articles in CodeProject ... Tony Selke27-Sep-07 7:56 Tony Selke 27-Sep-07 7:56
 Why use streams? hal9000lives10-Sep-07 4:35 hal9000lives 10-Sep-07 4:35
 Re: Why use streams? Colin Angus Mackay10-Sep-07 5:24 Colin Angus Mackay 10-Sep-07 5:24
 Re: Why use streams? hal9000lives10-Sep-07 5:37 hal9000lives 10-Sep-07 5:37
 Re: Why use streams? Colin Angus Mackay10-Sep-07 7:57 Colin Angus Mackay 10-Sep-07 7:57
 Re: Why use streams? [modified] cute-solutions11-Sep-07 2:40 cute-solutions 11-Sep-07 2:40
 Last Visit: 31-Dec-99 19:00     Last Update: 21-Feb-17 22:31 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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