AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Thursday, 10 December 2015

Add encryption class in project

Encryption is used to convert simple text into meaningless text & it appears random. Here I am adding some methods for encryption that will helpful for your project. Encryption helpful for query parameters as encrypted form if you don't want to show it .

Example
http://localhost:57536/Agencies/agency.aspx?id=NNE6IVASSVFetePvHfioEA==


public class Encrypter
   {
       #region---Declaration---
       //Set Password here
       static string Password = ConfigurationManager.AppSettings["EncrypterPwd"].ToString();
       #endregion
 
       #region---Methods---
       public static string Decrypt(string TextToBeDecrypted)
       {
           RijndaelManaged RijndaelCipher = new RijndaelManaged();
           string DecryptedData = null;
           try
           {
               byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
               byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
               //Making of the key for decryption     
               PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
               //Creates a symmetric Rijndael decryptor object.  
               ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
               MemoryStream memoryStream = new MemoryStream(EncryptedData);
               //Defines the cryptographics stream for decryption.THe stream contains decrpted data  
               CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
               byte[] PlainText = new byte[(EncryptedData.Length)];
               int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
               memoryStream.Close();
               cryptoStream.Close();
               //Converting to string  
               DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
           }
           catch 
           {
               DecryptedData = TextToBeDecrypted;
           }
           return DecryptedData;
       }
       public static string Encrypt(string TextToBeEncrypted)
       {
           RijndaelManaged RijndaelCipher = new RijndaelManaged();
           byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
           byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
           PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
           //Creates a symmetric encryptor object.        
           ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
           MemoryStream memoryStream = new MemoryStream();
           //Defines a stream that links data streams to cryptographic transformations        
           CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
           cryptoStream.Write(PlainText, 0, PlainText.Length);
           //Writes the final state and clears the buffer       
           cryptoStream.FlushFinalBlock();
           byte[] CipherBytes = memoryStream.ToArray();
           memoryStream.Close();
           cryptoStream.Close();
           string EncryptedData = Convert.ToBase64String(CipherBytes);
           return EncryptedData;
       }
       public static string EncryptQryParam(string TextToBeEncrypted)
       {
           RijndaelManaged RijndaelCipher = new RijndaelManaged();
           byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
           byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
           PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
           //Creates a symmetric encryptor object.        
           ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
           MemoryStream memoryStream = new MemoryStream();
           //Defines a stream that links data streams to cryptographic transformations        
           CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
           cryptoStream.Write(PlainText, 0, PlainText.Length);
           //Writes the final state and clears the buffer       
           cryptoStream.FlushFinalBlock();
           byte[] CipherBytes = memoryStream.ToArray();
           memoryStream.Close();
           cryptoStream.Close();
           string EncryptedData = Convert.ToBase64String(CipherBytes);
           return EncryptedData.Replace("+", "~").Replace("/", "^");
 
       }
       public static string DecryptQryParam(string TextToBeDecrypted)
       {
           TextToBeDecrypted = TextToBeDecrypted.Replace("~", "+").Replace("^", "/");
           RijndaelManaged RijndaelCipher = new RijndaelManaged();
           string DecryptedData = null;
           try
           {
               byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
               byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
               //Making of the key for decryption     
               PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
               //Creates a symmetric Rijndael decryptor object.  
               ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
               MemoryStream memoryStream = new MemoryStream(EncryptedData);
               //Defines the cryptographics stream for decryption.THe stream contains decrpted data  
               CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
               byte[] PlainText = new byte[(EncryptedData.Length)];
               int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
               memoryStream.Close();
               cryptoStream.Close();
               //Converting to string  
               DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
           }
           catch 
           {
               DecryptedData = TextToBeDecrypted;
           }
           return DecryptedData;
       }
       #endregion
   }

Decrypt & Encrypt methods are use to encrypt & decryption of simple text. Method EncryptQryParam DecryptQryParam  are use to encrypt & decryption  query string parameters.

0 comments :

Post a comment

  • Popular Posts
  • Comments