11,482,402 members (75,029 online)

# How To Use Rijndael to Encrypt Any File Easily?

, 2 Jul 2009 CPOL 22.9K 1.6K 35
 Rate this:
Encrypt any file easily
Screenshot of the tool, and the test procedures of the tool are as follows:

Encrypt:

Result after encrypting:

Decrypt:

Result after decrypting:

## Introduction

This article introduces how to use the Rijndael to encrypt any file, such as *.jpg, *.doc... and decrypt the encrypted file.

## Background

I promised my friend to help her to program one encrypt & decrypt tool for the examination,^-^. I searched some articles, but without the implementation of Rijndael, so I decided to do the Rijndael with the help of the MSDN.

## Using the Code

The whole code is in the attachment. You can download it if you prefer, or use it directly. Files in the attachment are as follows:

Aes.cs is the implementation of the Rijndael, and the main functions are `Cipher `and `InvCipher`.

```public void Cipher(byte[] input, byte[] output)  // encipher 16-bit input
{
// state = input
this.State = new byte[4, Nb];  // always [4,4]
for (int i = 0; i < (4 * Nb); ++i)
{
this.State[i % 4, i / 4] = input[i];
}

for (int round = 1; round <= (Nr - 1); ++round)  // main round loop
{
SubBytes();
ShiftRows();
MixColumns();
}  // main round loop

SubBytes();
ShiftRows();

for (int i = 0; i < (4 * Nb); ++i)
{
output[i] = this.State[i % 4, i / 4];
}

}  // Cipher()

public void InvCipher(byte[] input, byte[] output)  // decipher 16-bit input
{
// state = input
this.State = new byte[4, Nb];  // always [4,4]
for (int i = 0; i < (4 * Nb); ++i)
{
this.State[i % 4, i / 4] = input[i];
}

for (int round = Nr - 1; round >= 1; --round)  // main round loop
{
InvShiftRows();
InvSubBytes();
InvMixColumns();
}  // end main round loop for InvCipher

InvShiftRows();
InvSubBytes();

// output = state
for (int i = 0; i < (4 * Nb); ++i)
{
output[i] = this.State[i % 4, i / 4];
}

}  // InvCipher()```
RijndaelFrm.cs: The window of encryption & decryption by the Rijndael and the two functions(`Encrypt `& `Decrypt`) in RijndaelFrm.cs called the IF of the class Aes for encryption and decryption. You can get detailed information from the class.
```private Boolean Encrypt(string cOpenFile,string cSaveFile,string cPassword)
{
//check param
if (("" == cOpenFile) ||
("" == cSaveFile) ||
{
return false;
}

if (false == File.Exists(cOpenFile))
{
return false;
}

while (true == File.Exists(cSaveFile))
{
cSaveFile = Rename(cSaveFile);
}

byte[] plainText = new byte[MAX_BLOCK_LENGTH];
byte[] cipherText = new byte[MAX_BLOCK_LENGTH];
byte[] bzkey = new byte[MAX_KEY_LENGTH];

//get bytes from file
FileStream fileStream = new FileStream(cOpenFile, FileMode.Open);
fileStream.Seek(0, SeekOrigin.Begin);

//get the file stream for save
FileStream saveStream = new FileStream(cSaveFile, FileMode.Append);

//set length of the file
long lFileLength = fileStream.Length;
//set position of the file
long lPostion = fileStream.Position;

while (lPostion < lFileLength)
{
//Initialize  the buffer
Initialize(plainText, MAX_BLOCK_LENGTH);

{
break;
}
//set current cursor position
lPostion = fileStream.Position;

//Encrypt
Aes aes = new Aes(ekeySize, bzkey, eblockSize);

//Initialize  the buffer
Initialize(cipherText, MAX_BLOCK_LENGTH);

aes.Cipher(plainText, cipherText);
saveStream.Write(cipherText, 0, MAX_BLOCK_LENGTH);
}

saveStream.Close();
fileStream.Close();
return true;
}

private Boolean Decrypt(string cOpenFile, string cSaveFile, string cPassword)
{
//check param
if (("" == cOpenFile) ||
("" == cSaveFile) ||
{
return false;
}

if (0 > cOpenFile.LastIndexOf(".aes"))
{
return false;
}

if (false == File.Exists(cOpenFile))
{
return false;
}

while (true == File.Exists(cSaveFile))
{
cSaveFile = Rename(cSaveFile);
}

byte[] plainText = new byte[MAX_BLOCK_LENGTH];
byte[] cipherText = new byte[MAX_BLOCK_LENGTH];
byte[] bzkey = new byte[MAX_KEY_LENGTH];

//get bytes from file
FileStream fileStream = new FileStream(cOpenFile, FileMode.Open);
fileStream.Seek(0, SeekOrigin.Begin);

//get the file stream for save
FileStream saveStream = new FileStream(cSaveFile, FileMode.Append);

//set length of the file
long lFileLength = fileStream.Length;
//set position of the file
long lPostion = fileStream.Position;

while (lPostion < lFileLength)
{
//Initialize  the buffer
Initialize(plainText, MAX_BLOCK_LENGTH);

{
break;
}
//set current cursor position
lPostion = fileStream.Position;

//Encrypt
Aes aes = new Aes(ekeySize, bzkey, eblockSize);

//Initialize  the buffer
Initialize(cipherText, MAX_BLOCK_LENGTH);
//Decrypt
aes.InvCipher(plainText, cipherText);
saveStream.Write(cipherText, 0, MAX_BLOCK_LENGTH);
}

saveStream.Close();
fileStream.Close();

return true;
}```

Or you can transform the aes.cs to your project, and call the IF as shown above. You can easily encrypt or decrypt any of your files.

## Note

The form is only compatible with the password whose max length is 128 bits, but the algorithm of the aes is compatible with the password whose max length can be 128, 192 or 256 bits. The user can complete this, or I could complete it later.

## History

• 2009-06-20: First build ver. 1.01

## Share

Software Developer (Senior) Pioneer
China
Begin work with high-level programming language since 2008/09;
Master at the embedded system and firmware.
I'm glad to make friend with someone who likes the embedded system or IT.

O(∩_∩)O

Contact Me:
MSN:qxcjust@hotmail.com

 First Prev Next
 My vote of 5 nghiemvanhung28-Aug-11 0:49 nghiemvanhung 28-Aug-11 0:49
 problem password.. cover8517-Jun-11 7:24 cover85 17-Jun-11 7:24
 MAX_BLOCK_LENGTH brodhol31-Aug-10 23:02 brodhol 31-Aug-10 23:02
 [My vote of 2] Why do not you use the class System.Security.Cryptography.Aes? Iker eL_FRuTeRo12-Jul-09 22:53 Iker eL_FRuTeRo 12-Jul-09 22:53
 Re: [My vote of 2] Why do not you use the class System.Security.Cryptography.Aes? qxcjust13-Jul-09 0:30 qxcjust 13-Jul-09 0:30
 Password working? pimb210-Jul-09 5:36 pimb2 10-Jul-09 5:36
 Re: Password working? qxcjust12-Jul-09 17:00 qxcjust 12-Jul-09 17:00
 Re: Password working? shpile3-Jul-10 23:57 shpile 3-Jul-10 23:57
 Last Visit: 31-Dec-99 19:00     Last Update: 24-May-15 17:42 Refresh 1