Thursday, March 20, 2014

Encryption/Decryption for the Beginner's Level

Introduction

It often comes to the task list for the College/Uni Level Students, that they have to store passwords, pass the Query String with URL or other stuff in the Encrypted way. So this Trick would be focuses on the same to provide an easy to understand way of performing this task with the minimal time and effort.

Using the code

I have been doing some coding tasks, when this thing comes into my play, and after searching on GOOGLE, I found the following methods for both Encryption and Decryption.

Hence sharing here for others to get the tasks done in an understandable way.

Encryption

 private string Encrypt(string clearText)
    {
        string EncryptionKey = "KEY"; // See NOTE at end of TIP
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    } 

Decryption

private string Decrypt(string cipherText)
    {
        string EncryptionKey = "KEY"; //See Note at the End of TIP
        cipherText = cipherText.Replace(" ", "+");
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    } 
NOTE: in Place of KEY, You have to provide the key, based on which both Encryption and Decryption will be done. So same key would be provided in both methods.

Points of Interest

This tip while performing my self, taught me about the way encryption and decryption works and how they can be implemented in Web Application.

Comments, Positive Criticism and Advice's are heartily welcome. Smile | :)

No comments:

Post a Comment