In asp.net for encryption and decryption follow these  steps:


Step1:  Create a class and make 2 function in this class . one for encrypting the string and another for decrypting that string.

Step2: In class firstly include this NameSpace.


 using System.Text;


Step3: Now Create a Function for encrypting the string

  public string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new
        byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }


Step4: Now Create a Function for decrypting the string 


    public string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;


        UTF8Encoding encodepwd = new UTF8Encoding();
        System.Text.Decoder utf8Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;


    }


Step5: Now on your aspx.cs page:

Now for encrpting a string:



string str="Pradeep Lodhi";


string encrptedstr=Encryptdata(str);



for decrypting :


string decryptstr=Decryptdata(str);

0 comments:

Post a Comment

 
Top