Gcobani Mkontwana

Gcobani Mkontwana

  • 1.2k
  • 690
  • 22.7k

encryption and decryption of data in C#

Feb 27 2020 3:46 AM
Hi Team
 
I am facing an error, i have tried to add and remove, add back dll files into bin folder. Still no luck and dont know how to fix this issue ' the name 'decrypt' does not exist in the current context. Here are my class for this.
  1. //IdentityConfig.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Data.Entity;  
  7. using System.Security.Claims;  
  8. using System.Threading.Tasks;  
  9. using Microsoft.AspNet.Identity;  
  10. using Microsoft.AspNet.Identity.EntityFramework;  
  11. using Microsoft.AspNet.Identity.Owin;  
  12. using Microsoft.Owin;  
  13. using Microsoft.Owin.Security;  
  14. using eNtsaTrainingRegistration.Models;  
  15. using System.Net.Mail;  
  16. using System.Net;  
  17. using System.Web.Configuration;  
  18.   
  19. namespace eNtsaTrainingRegistration  
  20. {  
  21.     public class EmailService : IIdentityMessageService  
  22.     {  
  23.         public Task SendAsync(IdentityMessage message)  
  24.         {  
  25.             var mailMessage = new MailMessage();  
  26.             mailMessage.To.Add(new MailAddress(message.Destination));  
  27.             mailMessage.From = new MailAddress("Gcobani Mkontwana <ggcobani@gmail.com>");  
  28.             mailMessage.Subject = message.Subject;  
  29.             mailMessage.IsBodyHtml = true;  
  30.             mailMessage.Body = message.Body;  
  31.   
  32.             using(var smtp = new SmtpClient())  
  33.             {  
  34.                 var credential = new NetworkCredential  
  35.                 {  
  36.                     UserName = WebConfigurationManager.AppSettings["UserName"],  
  37.                     Password = Helper.Decrypt(WebConfigurationManager.AppSettings["UserPassword"])  
  38.                 };  
  39.                 smtp.Credentials = credential;  
  40.                 smtp.Host = WebConfigurationManager.AppSettings["SMTPName"];  
  41.                 smtp.Port = int.Parse(WebConfigurationManager.AppSettings["SMTPPort"]);  
  42.                 smtp.EnableSsl = true;  
  43.                 smtp.Send(mailMessage);  
  44.             }  
  45.             return Task.FromResult(0);  
  46.         }   
  47.         }  
  48.     }  
  49.   
  50.   
  51. // Helper.cs  
  52. using System;  
  53. using System.Collections.Generic;  
  54. using System.Linq;  
  55. using System.Web;  
  56. using System.Text;  
  57. using System.IO;  
  58. using System.Security.Cryptography;  
  59.   
  60. namespace eNtsaTrainingRegistration.Helper  
  61. {  
  62.     public class Helper  
  63.     {  
  64.         private const string PassPhrase = "3pAc0j$_56K?_S7c9gS!";  
  65.   
  66.         //Encrypt password.  
  67.         public static string Encrypt(string strValue)  
  68.         {  
  69.             byte[] results;  
  70.             UTF8Encoding uTF8 = new UTF8Encoding();  
  71.             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();  
  72.             byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));  
  73.             TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();  
  74.             desalg.Key = deskey;  
  75.             desalg.Mode = CipherMode.ECB;  
  76.             desalg.Padding = PaddingMode.PKCS7;  
  77.             byte[] encrypt_data = uTF8.GetBytes(strValue);  
  78.   
  79.             try  
  80.             {  
  81.                 ICryptoTransform encrytor = desalg.CreateEncryptor();  
  82.                 results = encrytor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);  
  83.             }  
  84.             finally  
  85.             {  
  86.                 desalg.Clear();  
  87.                 md5.Clear();  
  88.             }  
  89.             return Convert.ToBase64String(results);  
  90.         }  
  91.   
  92.         //Decrypt password.  
  93.   
  94.         public static string Decrypt(string strValue)  
  95.         {  
  96.             byte[] results;  
  97.             UTF8Encoding uTF8 = new UTF8Encoding();  
  98.             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();  
  99.             byte[] deskey = md5.ComputeHash(uTF8.GetBytes(PassPhrase));  
  100.             TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();  
  101.             desalg.Key = deskey;  
  102.             desalg.Mode = CipherMode.ECB;  
  103.             desalg.Padding = PaddingMode.PKCS7;  
  104.             byte[] decrypt_data = Convert.FromBase64String(strValue);  
  105.             try  
  106.             {  
  107.                 ICryptoTransform decryptor = desalg.CreateDecryptor();  
  108.                 results = decryptor.TransformFinalBlock(decrypt_data, 0, decrypt_data.Length);  
  109.             }  
  110.             finally  
  111.             {  
  112.                 desalg.Clear();  
  113.                 md5.Clear();  
  114.             }  
  115.             return uTF8.GetString(results);  
  116.         }  
  117.   
  118.         // In between space  
  119.         public static string GetBetween(string strSource, string strStart, string strEnd)  
  120.         {  
  121.             int Start, End;  
  122.             if(strSource.Contains(strStart) && strSource.Contains(strEnd))  
  123.             {  
  124.                 Start = strSource.IndexOf(strStart, 0) + strStart.Length;  
  125.                 End = strSource.IndexOf(strEnd, Start);  
  126.                 return strSource.Substring(Start, End - Start);  
  127.             }else  
  128.             {  
  129.                 return "";  
  130.             }  
  131.         }  
  132.         public static string BytesToString(long byteCount)  
  133.         {  
  134.             string[] suf = { "B""KB""MB""GB""TB""PB""EB" };  
  135.             if (byteCount == 0)  
  136.                 return string.Format("{0} {1}", 0, suf[0]);  
  137.             long bytes = Math.Abs(byteCount);  
  138.             int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));  
  139.             double num = Math.Round(bytes / Math.Pow(1024, place), 1);  
  140.             return string.Format("{0} {1}", (Math.Sign(byteCount) * num).ToString(), suf[place]);  
  141.         }  
  142.     }  
  143. }  

Answers (2)