Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, January 7, 2018

Encrypt And Decrypt String Without Using Special Character In C#

Encrypt


 public static string Encrypt(string plainText)  
     {  
       try  
       {  
         System.Text.Encoding encoding = System.Text.Encoding.Unicode;  
         Byte[] stringBytes = encoding.GetBytes(plainText);  
         StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);  
         foreach (byte b in stringBytes)  
         {  
           sbBytes.AppendFormat("{0:X2}", b);  
         }  
         return sbBytes.ToString();  
       }  
       catch (Exception tx)  
       {  
         return "0";  
       }  
     }  

Decrypt


  public static string Decrypt(string encryptedText)  
     {  
       try  
       {  
         System.Text.Encoding encoding = System.Text.Encoding.Unicode;  
         int numberChars = encryptedText.Length;  
         byte[] bytes = new byte[numberChars / 2];  
         for (int i = 0; i < numberChars; i += 2)  
         {  
           bytes[i / 2] = Convert.ToByte(encryptedText.Substring(i, 2), 16);  
         }  
         return encoding.GetString(bytes);  
       }  
       catch (Exception ex)  
       {  
         return "0";  
       }  
     }  

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";  
       }  
     }