Thursday, January 26, 2017

Simple encryption and decryption using key value in C#

Encryption

 public string Encrypt(string plainText)  
     {  
       try  
       {  
         string key = "abcd123xyz" + DateTime.Now.Year.ToString() + "#";  
         byte[] EncryptKey = { };  
         byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };  
         EncryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));  
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
         byte[] inputByte = Encoding.UTF8.GetBytes(plainText);  
         MemoryStream mStream = new MemoryStream();  
         CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(EncryptKey, IV), CryptoStreamMode.Write);  
         cStream.Write(inputByte, 0, inputByte.Length);  
         cStream.FlushFinalBlock();  
         return Convert.ToBase64String(mStream.ToArray());  
       }  
       catch (Exception)  
       {  
         return "0";  
       }  
     }  

Decryption

 public string Decrypt(string encryptedText)  
     {  
       try  
       {  
         string key = "abcd123xyz" + DateTime.Now.Year.ToString() + "#";           byte[] DecryptKey = { };  
         byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };  
         byte[] inputByte = new byte[encryptedText.Length];  
         DecryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));  
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
         inputByte = Convert.FromBase64String(encryptedText);  
         MemoryStream ms = new MemoryStream();  
         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(DecryptKey, IV), CryptoStreamMode.Write);  
         cs.Write(inputByte, 0, inputByte.Length);  
         cs.FlushFinalBlock();  
         System.Text.Encoding encoding = System.Text.Encoding.UTF8;  
         return encoding.GetString(ms.ToArray());  
       }  
       catch (Exception)  
       {  
         return "0";  
       }  
     }  


No comments:

Post a Comment