Friday, November 26, 2021

Encryption and decryption of password in asp.net

 //For decryption of password

-----------------------------***************************------------------------------

  1. private string Decrypt(string clearText)  
  2.         {  
  3.             string EncryptionKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";  
  4.             byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);  
  5.             using (Aes encryptor = Aes.Create())  
  6.             {  
  7.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });  
  8.                 encryptor.Key = pdb.GetBytes(32);  
  9.                 encryptor.IV = pdb.GetBytes(16);  
  10.                 using (MemoryStream ms = new MemoryStream())  
  11.                 {  
  12.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))  
  13.                     {  
  14.                         cs.Write(clearBytes, 0, clearBytes.Length);  
  15.                         cs.Close();  
  16.                     }  
  17.                     clearText = Convert.ToBase64String(ms.ToArray());  
  18.                 }  
  19.             }  
  20.             return clearText;  
  21.         }  

------------------------********************--------------------
For encryption of password

  1.  private string Encrypt(string clearText)  
  2.         {  
  3.             string EncryptionKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";  
  4.             byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);  
  5.             using (Aes encryptor = Aes.Create())  
  6.             {  
  7.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });  
  8.                 encryptor.Key = pdb.GetBytes(32);  
  9.                 encryptor.IV = pdb.GetBytes(16);  
  10.                 using (MemoryStream ms = new MemoryStream())  
  11.                 {  
  12.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))  
  13.                     {  
  14.                         cs.Write(clearBytes, 0, clearBytes.Length);  
  15.                         cs.Close();  
  16.                     }  
  17.                     clearText = Convert.ToBase64String(ms.ToArray());  
  18.                 }  
  19.             }  
  20.             return clearText;  
  21.         } 

No comments:

Post a Comment