blog

Home / DeveloperSection / Blogs / Encryption and Decryption in .NET

Encryption and Decryption in .NET

priyanka kushwaha1867 27-Jan-2015

In this blog, I’m explaining about Encryption and Decryption in .Net

 

Encryption is the process of translating plain text into something that appears to be random and meaningless.

Decryption is the process of translating  random  and meaningless  data to plain text.

Design your aspx like this

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
    <title></title>
 
</head>
<body>
 
    <formid="form1"runat="server">
        <table>
            <tr>
                <td>Password</td>
                <tdcolspan="2">
                    <asp:TextBoxID="txtPassword"runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Encryption text</td>
                <td>
                    <asp:LabelID="lblEnc"runat="server"Text=""></asp:Label></td>
                <td>
                    <asp:ButtonID="btnEncrypt"runat="server"Text="Encrypt"OnClick="btnEncrypt_Click"/></td>
            </tr>
            <tr>
                <td>Decryption text</td>
                <td>
                    <asp:LabelID="lblDec"runat="server"Text=""></asp:Label></td>
                <td>
                    <asp:ButtonID="btnDecrypt"runat="server"Text="Decrypt"Enabled="False"OnClick="btnDecrypt_Click"/></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
 
    </form>
 
</body>
</html>
Step 2:

Create Class file

using System.Security.Cryptography;
using System.IO;
using System.Text;
publicclassClass1
{
    string Encryptionkey = "123";
    publicstring Encrypt(string originalText)
    {
      
        byte[] originalbytes = Encoding.Unicode.GetBytes(originalText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = newRfc2898DeriveBytes(Encryptionkey, newByte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64,  0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = newMemoryStream())
            {
                using (CryptoStream cs = newCryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(originalbytes, 0, originalbytes.Length);
                    cs.Close();
                }
                originalText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return originalText;
 
    }
 
 publicstring Decrypt(string cipherText)
    {
      
            Byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
              Rfc2898DeriveBytes pbd = newRfc2898DeriveBytes(Encryptionkey, newByte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x76 });
                encryptor.Key = pbd.GetBytes(32);
                encryptor.IV = pbd.GetBytes(16);
                using (MemoryStream ms = newMemoryStream())
                {
                    using (CryptoStream cs = newCryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
 
            }
      
        return cipherText;
}
            public Class1()
            {
                        //
                        // TODO: Add constructor logic here
                        //
            }
}
Step 3:


After that add following code in code behind

 

publicpartialclass_Default : System.Web.UI.Page
{
Class1 method = newClass1();
    protectedvoid Page_Load(object sender, EventArgs e)
    {
 
    }
    protectedvoid btnEncrypt_Click(object sender, EventArgs e)
    {
        lblEnc.Text =method.Encrypt(txtPassword.Text.Trim());
        btnDecrypt.Enabled = true;
    }
    protectedvoid btnDecrypt_Click(object sender, EventArgs e)
    {
        lblDec.Text = method.Decrypt(lblEnc.Text.Trim());
    }
}
 

 


Updated 27-Jan-2015

Leave Comment

Comments

Liked By