C# Data Encryption - A Rijndael approach

This article show data encryption and decryption using Rijndael class available in .NET. System.Security.Cryptography is the namespace that provides good techniques and methods for encryption as well as decryption.

Hi all
This article show data encryption and decryption using Rijndael class available in .NET. System.Security.Cryptography is the namespace that provides good techniques and methods for encryption as well as decryption.

Take a look at the code that uses Rijndael class to encrypt and decrypt data.
This is fair and simple with minimum set of code lines.

// use System.Security.Cryptography namespace
using System.Security.Cryptography;

public class
Encryption
    {
        public Encryption()
        {
            //  constructor logic
        }

        // Encrypting array of bytes
        private  byte[] Encrypt(byte[] inputData, byte[] pwd, byte[] value)
        {

            // store bytes in memory stream
            MemoryStream stream = new MemoryStream();

            Rijndael rij = Rijndael.Create();
            rij.pwd = pwd;
            rij.value = value;
            CryptoStream cStream = new CryptoStream(stream, rij.CreateEncryptor(), CryptoStreamMode.Write);

            cStream.Write(inputData, 0, inputData.Length);
            cStream.Close();
            byte[] encryptedData = stream.ToArray();
              return encryptedData;
        }

     
        // Returns Rijndael encrypted string.
        public string Encrypt(string inputData, string pwd,int bits)
        {
           byte[] Bytes = System.Text.Encoding.Unicode.GetBytes(inputData);
            PasswordDeriveBytes pwdBytes = new PasswordDeriveBytes(pwd, new byte[] {0x10, 0x40, 0x00, 0x34,0x1A,0x70,0x01,0x34,0x56,0xFF,0x99,0x77,0x4C,0x22,0x49});
   
            if (bits == 128)
            {
                byte[] encryptedData = Encrypt(Bytes, pwdBytes.GetBytes(16), pwdBytes.GetBytes(16));
                return Convert.ToBase64String(encryptedData);
            }
           else if(bits == 192)
            {
                byte[] encryptedData = Encrypt(Bytes, pwdBytes.GetBytes(24), pwdBytes.GetBytes(16));
                return Convert.ToBase64String(encryptedData);
            }
            else if(bits == 256)
            {
                byte[] encryptedData = Encrypt(Bytes, pwdBytes.GetBytes(32), pwdBytes.GetBytes(16));
                return Convert.ToBase64String(encryptedData);
            }
            else
            {
                // append all bits
                return string.Concat(bits);
            }
        }

        // Decrypt the byte array
        private  byte[] Decrypt(byte[] outputData, byte[] pwd, byte[] value)
        {

            MemoryStream stream = new MemoryStream();
            Rijndael rij = Rijndael.Create();
            rij.pwd = pwd;
            rij.value = value;
            CryptoStream cStream = new CryptoStream(stream, rij.CreateDecryptor(), CryptoStreamMode.Write);
             cStream.Write(outputData, 0, outputSata.Length);
            cStream.Close();
            byte[] decryptedData = stream.ToArray();
            return decryptedData;
        }

       // Decrypt the string.
        public string Decrypt(string str, string pwd,int bits)

        {
            byte[] Bytes = Convert.FromBase64String(str);
              PasswordDeriveBytes pwdBytes = new PasswordDeriveBytes(pwd,
                new byte[] { 0x10, 0x40, 0x00, 0x34, 0x1A, 0x70, 0x01, 0x34, 0x56, 0xFF, 0x99, 0x77, 0x4C, 0x22, 0x49 });
 
            if (bits == 128)
            {
                byte[] decryptedData = Decrypt(Bytes, pwdBytes.GetBytes(16), pwdBytes.GetBytes(16));
                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            else if(bits == 192)
            {
                byte[] decryptedData = Decrypt(Bytes, pwdBytes.GetBytes(24), pwdBytes.GetBytes(16));
                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            else if(bits == 256)
            {
                byte[] decryptedData = Decrypt(Bytes, pwdBytes.GetBytes(32), pwdBytes.GetBytes(16));
                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            else
            {
                return string.Concat(Bits);
            }
       
        }
    }

Using the above class

For 128 bit encryption,
    string text = Encryption.Encrypt(inputText, "MyPassword", 128);

For 128 bit decryption,
   string text = Encryption.Decrypt(inputText, "MyPassword", 128);
        

So simple... From now onwards secure your data using Rijndael technique.
SAN

By san san   Popularity  (7262 Views)