Encrypt and Decrypt password in C#?

Storing sensitive data (like password, credit card no) in database as encrypted form is a good practice.  So many algorithms are available for this. But if we encrypt the data by using a particular algorithm then hacker can easily decrypt the data by using that algorithm. So it is better to add some extra value with the encrypted data. It will generate a completely strong encrypted data.This article describes how to encrypt and decrypt password using C#?; or Encode and Decode Password in C#.  Summary of the article:

  • What is Encryption?
  • What is Decryption?
  • Why we should use Encryption and Decryption?
  • How to Encrypt Password in C#?
  • How to Decrypt Password in C#?

What is Encryption?
Encryption is the process to translate the original data or plain text data into something that appears to be random and meaningless.

What is Decryption?
Decryption is the process to translate encrypted or random and meaningless data into original data or plain text data.

Why we should use Encryption and Decryption?
Encryption and Decryption process help us to hide original data and display some junk data based on this. It provides better security.

How to Encrypt Password in C#?
C# code to encrypt password is given bellow:

    
string Data = "DataForEncryption";
byte[] oByte = new byte[Data.Length];
oByte = System.Text.Encoding.UTF8.GetBytes(Data);
string EncodedData =  Convert.ToBase64String(oByte);

C# method to encrypt password is given bellow:

public string EncodePassword(string Data)
{
    try
    {
        byte[] oByte = new byte[Data.Length];
        oByte = System.Text.Encoding.UTF8.GetBytes(Data);
        string EncodedData = "25" + Convert.ToBase64String(oByte);
        return EncodedData;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

How to Decrypt Password in C#?
C# code to decrypt password is given bellow:

string Data = "DataForDecryption";
System.Text.UTF8Encoding oUTF8Encoding = new System.Text.UTF8Encoding();
System.Text.Decoder oDecoder = oUTF8Encoding.GetDecoder();
byte[] oByte = Convert.FromBase64String(Data);
int CharCount = oDecoder.GetCharCount(oByte, 0, oByte.Length);
char[] DecodedChar = new char[CharCount];
oDecoder.GetChars(oByte, 0, oByte.Length, DecodedChar, 0);
string DecodedData = new String(DecodedChar);

C# method to decrypt password is given bellow:

public string DecodePassword(string Data)
{
    try
    {
        System.Text.UTF8Encoding oUTF8Encoding = new System.Text.UTF8Encoding();
        System.Text.Decoder oDecoder = oUTF8Encoding.GetDecoder();
        byte[] oByte = Convert.FromBase64String(Data);
        int CharCount = oDecoder.GetCharCount(oByte, 0, oByte.Length);
        char[] DecodedChar = new char[CharCount];
        oDecoder.GetChars(oByte, 0, oByte.Length, DecodedChar, 0);
        string DecodedData = new String(DecodedChar);
        return DecodedData;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

If we call the method EncodePassword(“London”) it will generate encrypted data like “TG9uZG9u”
If we call the method DecodePassword(“TG9uZG9u”) it will provides the original data “London”

That’s all about Password encryption and decryption.

Leave a Reply

Your email address will not be published. Required fields are marked *