using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Security.Cryptography;     // needed for the encryption classes

using System.IO;                        // needed for the MemoryStream

using System.Text;                      // needed for the UTF8 encoding

using System.Net;                       // needed for the cookie

 

namespace Misc

{

    public partial class EncryptionExample1 : System.Web.UI.Page

    {

 

        private Byte[] key = { 250, 101, 18, 76, 45, 135, 207, 118, 4, 171, 3, 168, 202, 241, 37, 199 };

        private Byte[] vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };

 

 

        protected void Page_Load(object sender, EventArgs e)

        {

            // Read encrypted password from cookie

            if (!IsPostBack && Request.Cookies["LoginCookie"] != null)

            {

                HttpCookie myCookie = Request.Cookies["LoginCookie"];

                txtUserName.Text = myCookie.Values["Username"];

                txtPassword.Text = myCookie.Values["Password"];

                lblDisplay.Text = "Data read from cookie.";

            }

        }

 

        protected void btnEncrypt_Click(object sender, EventArgs e)

        {

            String user = txtUserName.Text;

            String plainTextPassword = txtPassword.Text;

            String encryptedPassword;

 

            UTF8Encoding encoder = new UTF8Encoding();      // used to convert bytes to characters, and back

            Byte[] textBytes;                               // stores the plain text data as bytes

 

            // Perform Encryption

            //-------------------

            // Convert a string to a byte array, which will be used in the encryption process.

            textBytes = encoder.GetBytes(plainTextPassword);

 

            // Create an instances of the encryption algorithm (Rinjdael AES) for the encryption to perform,

            // a memory stream used to store the encrypted data temporarily, and

            // a crypto stream that performs the encryption algorithm.

            RijndaelManaged rmEncryption = new RijndaelManaged();

            MemoryStream myMemoryStream = new MemoryStream();

            CryptoStream myEncryptionStream = new CryptoStream(myMemoryStream, rmEncryption.CreateEncryptor(key, vector), CryptoStreamMode.Write);

 

            // Use the crypto stream to perform the encryption on the plain text byte array.

            myEncryptionStream.Write(textBytes, 0, textBytes.Length);

            myEncryptionStream.FlushFinalBlock();

 

            // Retrieve the encrypted data from the memory stream, and write it to a separate byte array.

            myMemoryStream.Position = 0;

            Byte[] encryptedBytes = new Byte[myMemoryStream.Length];

            myMemoryStream.Read(encryptedBytes, 0, encryptedBytes.Length);

 

            // Close all the streams.

            myEncryptionStream.Close();

            myMemoryStream.Close();

 

            // Convert the bytes to a string and display it.

            encryptedPassword = Convert.ToBase64String(encryptedBytes);

            lblDisplay.Text = encryptedPassword;

            divMethod.InnerText = "Encrypted Data:";

 

            // Write encrypted password to a cookie

            HttpCookie myCookie = new HttpCookie("LoginCookie");

            myCookie.Values["Username"] = user;

            myCookie.Values["Password"] = encryptedPassword;

            myCookie.Expires = new DateTime(2020, 2, 1);

            Response.Cookies.Add(myCookie);

 

        }

 

        protected void btnDecrypt_Click(object sender, EventArgs e)

        {

 

            // Read encrypted password from cookie

            if (Request.Cookies["LoginCookie"] != null)

            {

                HttpCookie myCookie = Request.Cookies["LoginCookie"];

                String encryptedPassword = myCookie.Values["Password"];

 

                Byte[] encryptedPasswordBytes = Convert.FromBase64String(encryptedPassword);

                Byte[] textBytes;

                String plainTextPassword;

                UTF8Encoding encoder = new UTF8Encoding();

 

                // Perform Decryption

                //-------------------

                // Create an instances of the decryption algorithm (Rinjdael AES) for the encryption to perform,

                // a memory stream used to store the decrypted data temporarily, and

                // a crypto stream that performs the decryption algorithm.

                RijndaelManaged rmEncryption = new RijndaelManaged();

                MemoryStream myMemoryStream = new MemoryStream();

                CryptoStream myDecryptionStream = new CryptoStream(myMemoryStream, rmEncryption.CreateDecryptor(key, vector), CryptoStreamMode.Write);

 

                // Use the crypto stream to perform the decryption on the encrypted data in the byte array.

                myDecryptionStream.Write(encryptedPasswordBytes, 0, encryptedPasswordBytes.Length);

                myDecryptionStream.FlushFinalBlock();

 

                // Retrieve the decrypted data from the memory stream, and write it to a separate byte array.

                myMemoryStream.Position = 0;

                textBytes = new Byte[myMemoryStream.Length];

                myMemoryStream.Read(textBytes, 0, textBytes.Length);

 

                // Close all the streams.

                myDecryptionStream.Close();

                myMemoryStream.Close();

 

                // Convert the bytes to a string and display it.

                plainTextPassword = encoder.GetString(textBytes);

                lblDisplay.Text = plainTextPassword;

                txtPassword.Text = plainTextPassword;

                divMethod.InnerText = "Decrypted Data:";

 

            }

 

        }

    }

}