Encrypt and Decrypt String (TripleDES)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TripleDES
{
   public class ClsTripleDES
   {
      private const string mysecurityKey = "SampleKey";

      public static string Encrypt(string TextToEncrypt)
      {
         byte[] MyEncryptedArray = UTF8Encoding.UTF8
            .GetBytes(TextToEncrypt);

         MD5CryptoServiceProvider MyMD5CryptoService = new
            MD5CryptoServiceProvider();

         byte[] MysecurityKeyArray = MyMD5CryptoService.ComputeHash
            (UTF8Encoding.UTF8.GetBytes(mysecurityKey));

         MyMD5CryptoService.Clear();

         var MyTripleDESCryptoService = new
            TripleDESCryptoServiceProvider();

         MyTripleDESCryptoService.Key = MysecurityKeyArray;

         MyTripleDESCryptoService.Mode = CipherMode.ECB;

         MyTripleDESCryptoService.Padding = PaddingMode.PKCS7;

         var MyCrytpoTransform = MyTripleDESCryptoService
            .CreateEncryptor();

         byte[] MyresultArray = MyCrytpoTransform
            .TransformFinalBlock(MyEncryptedArray, 0,
            MyEncryptedArray.Length);

         MyTripleDESCryptoService.Clear();

         return Convert.ToBase64String(MyresultArray, 0,
            MyresultArray.Length);
      }



      public static string Decrypt(string TextToDecrypt)
      {
         byte[] MyDecryptArray = Convert.FromBase64String
            (TextToDecrypt);

         MD5CryptoServiceProvider MyMD5CryptoService = new
            MD5CryptoServiceProvider();

         byte[] MysecurityKeyArray = MyMD5CryptoService.ComputeHash
            (UTF8Encoding.UTF8.GetBytes(mysecurityKey));

         MyMD5CryptoService.Clear();

         var MyTripleDESCryptoService = new
            TripleDESCryptoServiceProvider();

         MyTripleDESCryptoService.Key = MysecurityKeyArray;

         MyTripleDESCryptoService.Mode = CipherMode.ECB;

         MyTripleDESCryptoService.Padding = PaddingMode.PKCS7;

         var MyCrytpoTransform = MyTripleDESCryptoService
            .CreateDecryptor();

         byte[] MyresultArray = MyCrytpoTransform
            .TransformFinalBlock(MyDecryptArray, 0,
            MyDecryptArray.Length);

         MyTripleDESCryptoService.Clear();

         return UTF8Encoding.UTF8.GetString(MyresultArray);
      }
   }
}

DLL file : Click Me

No comments:

Post a Comment