.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Encryption and Decryption in C# Web Text Box

Posted By:Suderson Sarathy       Posted Date: November 22, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

Encryption and Decryption in C# Web Text Box
 

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;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    string s ,s1;
    public void EncryptText(string strText)
    {
        s1 = Encrypt(strText, "!@#$%^&*()<>?/:;{}[]~`");
    }

//Decrypt the text
    public void DecryptText(string strText)
    {
        s1 = Decrypt(strText, "!@#$%^&*()<>?/:;{}[]~`");
    }

//The function used to encrypt the text
    public string Encrypt(string strText, string strEncrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0X12, 0X34, 0X56, 0X78, 0X90, 0XAB, 0XCD, 0XEF };

        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            Convert.ToBase64String(ms.ToArray());
            s = Convert.ToBase64String(ms.ToArray());
            return s;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }

//The function used to decrypt the text
    private string Decrypt(string strText, string sDecrKey)
    {
        byte[] byKey = null;
        byte[] IV = { 0X12, 0X34, 0X56, 0X78, 0X90, 0XAB, 0XCD, 0XEF };
        byte[] inputByteArray = new byte[strText.Length + 1];

        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;


            s = encoding.GetString(ms.ToArray());
            return s;

        }
        catch (Exception ex)
        {
            return ex.Message;
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        EncryptText(TextBox1.Text.ToString().Trim());
        TextBox2.Text = s1.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        DecryptText(TextBox2.Text.Trim().ToString());
        TextBox1.Text = "";
        TextBox1.Text = s1.ToString();
    }
}
Hi. Here I've pasted the coding of mine where i use the encryption and decryptrion of the text box values and store it in DB. However I didn't mention on saving the data in DB. I've done with the cryptogrpahy



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend