Monday 6 June 2016

Symmetric Algorithm - Rijndael Encryption and Decryption

In this article we are going to see how to encrypt and decrypt the data using symmetric algorithm Rijndael.
User text can be encrypted using a password using  different format of bytes like 128,192 and 256.
So we have get the PasswordDerivedBytes , The Keybytes,InitVectorbytes and symmetric mode are important one to set in object of RijndaelManaged.

Symmetric algorithm means, same password is used to encrypt and decrypt the input string. i.e one format string into the another format

Encryption :
Code :
            Console.WriteLine("Enter the input string to encrypt");
            string input = Console.ReadLine();
            Console.WriteLine("Enter the password to encrypt the input string.");
            string password = Console.ReadLine();
            string encstring= RijndaelAlgo.Encrypt(input, password);
            Console.WriteLine("String after Encryption :"+Environment.NewLine + encstring);
            Console.ReadLine();


Output:
Enter the input string to encrypt
Computer science

Enter the password to encrypt the input string.
rajesh

String after Encryption :
qVWbRozCX1680RYmkHIy8vOsctlnanCZP+cnC7I459A=


Decryption:
Code :
            Console.WriteLine("Enter the input string to decrypt");
            string input = Console.ReadLine();
            Console.WriteLine("Enter the password to decrypt the input string.");
            string password = Console.ReadLine();
            string encstring= RijndaelAlgo.Decrypt(input, password);
            Console.WriteLine("String after Decryption :"+Environment.NewLine + encstring);
            Console.ReadLine();



Output:
Enter the input string to decrypt
qVWbRozCX1680RYmkHIy8vOsctlnanCZP+cnC7I459A=

Enter the password to decrypt the input string.
rajesh

String after Decryption :
Computer science

Code for RijndaelAlgo :

  public class RijndaelAlgo
    {

        static string saltValue = "s@!tVal*e";
        static string hashAlgorithm = "SHA1";
        static int passwordIterations = 2;
        static string initVector = "@1B2c3!@)#$%@#X6g7FG";
        static int keySize = 256;

        public static string Encrypt(string plainText, string passPhrase)
        {

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);


            byte[] keyBytes = password.GetBytes(keySize / 8);

            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.ECB;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            string cipherText = Convert.ToBase64String(cipherTextBytes);
            return cipherText;
        }

        public static string Decrypt(string cipherText, string passPhrase)
        {

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

            PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

            byte[] keyBytes = password.GetBytes(keySize / 8);
            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.ECB;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            // Start decrypting.
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

            // Return decrypted string.  
            return plainText;
        }
    }




I Hope from this article you can learn how to encryption and decryption using symmetric algorithm

No comments:

Post a Comment