Different ways to encrypt or decrypt password in asp.net

In a real-life situation, the password field should be stored in the database in encrypted mode. Below are some techniques to encrypt or decrypt a password (string) in Asp.Net.

Encrypt or decrypt password in asp.net (Method-1)

Below is the method to encrypt a string in asp.net.

private string encryptPassword(string pass)
{
        string _result = string.Empty;
        char[] temp = pass.ToCharArray();
        foreach (var _singleChar in temp)
        {
                var i = (int)_singleChar;
                i = i – 2;
                _result += (char)i;
        }
        return _result;
}

Below is the method to decrypt a string in asp.net.

private string decryptPassword(string pass)
{
        string _result = string.Empty;
        char[] temp = pass.ToCharArray();
        foreach (var _singleChar in temp)
        {
                var i = (int)_singleChar;
                i = i + 2;
                _result += (char)i;
        }
        return _result;
}

Encrypt or decrypt password in asp.net (Method-2)

Below is the method to encrypt a string in asp.net.

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

Below is the method to decrypt a string in asp.net.

private string DecryptPassword(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}

You may like following asp.net tutorials:

This asp.net tutorial, we learned how to encrypt or decrypt password in asp.net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>