Encrypt and Decrypt Passwords in a Project for Security

Here in this example, I have explained how to encrypt a password before storing it in a database and secondly, how to decrypt it. In most of the project we need Encrypt and Decrypt of User id and password, so here, I have given a simple example and shown how could we Encrypt and Decrypt our UserId and password.

First take a web form into your project and design the form like the following to enter the Password. Here is how my form will look like:

HTML view

 Following is the HTML view:

  1. <div>  
  2.     <asp:TextBox ID="txt_password" runat="server"></asp:TextBox>  
  3.     <asp:Button runat="server" ID="btn_subbmit" Text="ENCRYPT" OnClick="btn_subbmit_Click" />  
  4.     <p> </p>  
  5. </div>  
  6. <div>  
  7.     <asp:TextBox ID="txt_enpass" runat="server"></asp:TextBox>  
  8.     <asp:Button runat="server" ID="btn_decrypt" Text="DECRYPT" OnClick="btn_decrypt_Click" />  
  9.     <p> </p>  
  10. </div>  
And in the .cs page I have declared the following two methods for encrypting and decrypting the password that user entered.
  1. public string EncryptPwd(String strString)   
  2. {  
  3.     // //====================================  
  4.     // // Purpose : To Encrypt the password  
  5.     // // Parameter : Password to encrypt  
  6.     // // Returns : Encrypted password  
  7.     int intAscii;  
  8.     string strEncryPwd = "";  
  9.     for (int intIndex = 0; intIndex < strString.ToString().Length; intIndex++)   
  10.     {  
  11.         intAscii = (intchar.Parse(strString.ToString().Substring(intIndex, 1));  
  12.         intAscii = intAscii + 5;  
  13.         strEncryPwd += (char) intAscii;  
  14.     }  
  15.     return strEncryPwd;  
  16. }  
And for decrypting the password, here is my method:
  1. public string DecryptPwd(string strEncryptedPwd)   
  2. {  
  3.     //====================================  
  4.     // Purpose : To Decrypt the password  
  5.     // Parameter : Password to Decrypt  
  6.     // Returns : Decrypted password  
  7.     int intAscii;  
  8.     string strDecryPwd = "";  
  9.     for (int intIndex = 0; intIndex < strEncryptedPwd.ToString().Length; intIndex++)   
  10.     {  
  11.         intAscii = (intchar.Parse(strEncryptedPwd.ToString().Substring(intIndex, 1));  
  12.         intAscii = intAscii - 5;  
  13.         strDecryPwd += (char) intAscii;  
  14.     }  
  15.     //  
  16.     return strDecryPwd;  
  17. }  
The main thing here i did for encryption is:

 

  1. Finding the ASCII key of the letter entered as password.
  2. Adding +5 to each ascii key.
  3. similarly while DECRYPT my password finding the ASCII key and subtracting -5 from that to get the original data.

Now call the encrypt method on Encrypt button click.

  1. protected void btn_subbmit_Click(object sender, EventArgs e)  
  2. {  
  3.    string password = txt_password.Text;  
  4.    string encript = EncryptPwd(password);  
  5.    Response.Write("The Encripted password is:"+encript);  
  6. }  
Now this will be like-

enter password

Now for decrypting the same password in original form, place the password in decrypt textbox and press the decrypt button.

Here is how this decrypt button will work.
  1. protected void btn_decrypt_Click(object sender, EventArgs e)  
  2. {  
  3.    string depasstxt = txt_enpass.Text;  
  4.    string encript = DecryptPwd(depasstxt);  
  5.    Response.Write("The Password is:" + encript);  
  6. }  
And my form will be like the following:

password

Thus in this way we can manage encryption and decryption of password in our project.