Encryption and Decryption in c#

Encryption and Decryption in c#

1)Create new class named as Serialization inside it create two new function copied following code to that class.

2)Passes  value in the query string and call the method Encryptnew to Encript the value.

    Ex:-
         Response.Redirect("Querytest.aspx?val="+ClassName.Seralize.EncryptNew(txtname.Text));


3)Decrypte the value which is getting from query string....

    Ex:-
        Label lblresult = new Label();
            lblresult.Text = BAL_Yahoo.Seralize.DecryptNew(Request.QueryString["val"].ToString());
            form1.Controls.Add(lblresult);

This code is very very important for Encrypte and decrypte values in the C#.........
Used in online banking site and social network site to security perpouse.



public static string EncryptNew(string pstrText)
        {
            string pstrEncrKey = "1239;[pewGKG)NisarFidesTech";
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byKey = System.Text.Encoding.UTF8.GetBytes(pstrEncrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(pstrText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }


public static string DecryptNew(string pstrText)
        {
            pstrText = pstrText.Replace(" ", "+");
            string pstrDecrKey = "1239;[pewGKG)NisarFidesTech";
            byte[] byKey = { };
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byte[] inputByteArray = new byte[pstrText.Length];
            byKey = System.Text.Encoding.UTF8.GetBytes(pstrDecrKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(pstrText);
            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;
            return encoding.GetString(ms.ToArray());
        }

Comments

Popular posts from this blog

SQL Query :- Create Tables,Primary key,Foreign key,Merge Statment

AngularJS

Check folder and Delete All Files from Existing Folder in C#