Subin Thomas

Subin Thomas

  • NA
  • 4.9k
  • 126k

getting an error in Encryption and Decryption

Feb 14 2019 12:47 AM
i wrote a code for encryption and decryption but im getting error in "encoding" keyword it says
Error: The name 'Encoding' does not exist in the current context
 here is the code below
  1. private string Encrypt(string clearText)  
  2.         {  
  3.             string EncryptionKey = "MAKV2SPBNI99212";  
  4.             byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);  
  5.             using (Aes encryptor = Aes.Create())  
  6.             {  
  7.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });  
  8.                 encryptor.Key = pdb.GetBytes(32);  
  9.                 encryptor.IV = pdb.GetBytes(16);  
  10.                 using (MemoryStream ms = new MemoryStream())  
  11.                 {  
  12.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))  
  13.                     {  
  14.                         cs.Write(clearBytes, 0, clearBytes.Length);  
  15.                         cs.Close();  
  16.                     }  
  17.                     clearText = Convert.ToBase64String(ms.ToArray());  
  18.                 }  
  19.             }  
  20.             return clearText;  
  21.         }  
  22.   
  23.         private string Decrypt(string cipherText)  
  24.         {  
  25.             string EncryptionKey = "MAKV2SPBNI99212";  
  26.             byte[] cipherBytes = Convert.FromBase64String(cipherText);  
  27.             using (Aes encryptor = Aes.Create())  
  28.             {  
  29.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });  
  30.                 encryptor.Key = pdb.GetBytes(32);  
  31.                 encryptor.IV = pdb.GetBytes(16);  
  32.                 using (MemoryStream ms = new MemoryStream())  
  33.                 {  
  34.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))  
  35.                     {  
  36.                         cs.Write(cipherBytes, 0, cipherBytes.Length);  
  37.                         cs.Close();  
  38.                     }  
  39.                     cipherText = Encoding.Unicode.GetString(ms.ToArray());  
  40.                 }  
  41.             }  
  42.             return cipherText;  
  43.         } 
 

Answers (4)