Showing posts with label String Decryption. Show all posts
Showing posts with label String Decryption. Show all posts

Sunday, January 7, 2018

Encrypt and Decrypt string using SQL

Sample Code


 Declare   
 @myData nvarchar(max) = 'My data',  
 @seqKey nvarchar(20) = 'UniQKey07x@a',  
 @encryptedData varbinary(2000)  
 set @encryptedData = EncryptByPassPhrase(@seqKey, @myData)  
 print @encryptedData  
 print convert(nvarchar(max),DecryptByPassPhrase(@seqKey, @encryptedData ))--Decrypted data  

Output

 0x010000008076CB00D6FB7820D77D4B9C3768D596BA1812019E4CFCAEF9B190DAD462A3E3  
 My data  

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