Encrypt and Decrypt a String in ASP.NET

Encrypt and decrypt are very important data with C# play. For security purpose, we are storing some valuable things in Encrypt format.


I have two code examples that I wrote for best practices encrypting a string and second is again Decrypt the value.

Microsoft has a simple method to convert string in Encrypt and Decrypt at any time.

Encrypt and Decrypt Simple Method
  1. string encryptedString = SomeStaticClass.Encrypt(sourceString);    
  2.     
  3. string decryptedString = SomeStaticClass.Decrypt(encryptedString);    
To use the code sample, you can copy the CryptorEngine to your project and start playing or copy method bodies and paste to your application projects.

I wrote a class to access the Encrypt and Decrypt Method. because I wanted to use this method at many places. And this made my programming simple.


Encrypt and Decrypt Using Class

  1. public string DecryptString(string encrString)  
  2. {  
  3.     byte[] b;  
  4.     string decrypted;  
  5.     try  
  6.     {  
  7.         b = Convert.FromBase64String(encrString);  
  8.         decrypted = System.Text.ASCIIEncoding.ASCII.GetString(b);  
  9.     }  
  10.     catch (FormatException fe)   
  11.     {  
  12.         decrypted = "";  
  13.     }  
  14.     return decrypted;  
  15. }  
  16.   
  17. public string EnryptString(string strEncrypted)   
  18. {  
  19.     byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);  
  20.     string encrypted = Convert.ToBase64String(b);  
  21.     return encrypted;  
  22. }  

Still The above code is not enough for me because I want to do something with my choice. Then I had implemented the code with specify charter to use in Encrypt method. I have created two method class.

 
Encrypt and Decrypt with Character Choice
  1. public string encrypt(string encryptString)   
  2. {  
  3.     string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  4.     byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);  
  5.     using(Aes encryptor = Aes.Create())   
  6.     {  
  7.         Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
  8.             0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
  9.         });  
  10.         encryptor.Key = pdb.GetBytes(32);  
  11.         encryptor.IV = pdb.GetBytes(16);  
  12.         using(MemoryStream ms = new MemoryStream())  
  13.         {  
  14.             using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) {  
  15.                 cs.Write(clearBytes, 0, clearBytes.Length);  
  16.                 cs.Close();  
  17.             }  
  18.             encryptString = Convert.ToBase64String(ms.ToArray());  
  19.         }  
  20.     }  
  21.     return encryptString;  
  22. }  
  23.   
  24. public string Decrypt(string cipherText)   
  25. {  
  26.     string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  27.     cipherText = cipherText.Replace(" ""+");  
  28.     byte[] cipherBytes = Convert.FromBase64String(cipherText);  
  29.     using(Aes encryptor = Aes.Create())   
  30.     {  
  31.         Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
  32.             0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
  33.         });  
  34.         encryptor.Key = pdb.GetBytes(32);  
  35.         encryptor.IV = pdb.GetBytes(16);  
  36.         using(MemoryStream ms = new MemoryStream())   
  37.         {  
  38.             using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) {  
  39.                 cs.Write(cipherBytes, 0, cipherBytes.Length);  
  40.                 cs.Close();  
  41.             }  
  42.             cipherText = Encoding.Unicode.GetString(ms.ToArray());  
  43.         }  
  44.     }  
  45.     return cipherText;  
  46. }  

At last the above code is totally perfect to make such a good application and now I'm filling good because I did what I want.

Thanks for more code visit stupidcodes.com
Ebook Download
View all
Learn
View all