Advertisement
priore

3DES Encrypt/Decrypt

Nov 10th, 2014
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. //
  2. // https://github.com/priore/SOAPEngine/blob/master/CSHARP/3DESClass.cs
  3. //
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Security.Cryptography;
  9. using System.IO;
  10. using System.Text;
  11.  
  12. namespace Priore.Cryptography
  13. {
  14.     public class TripleDESClass
  15.     {
  16.         public static string EncryptText(string input, string password)
  17.         {
  18.             // Get the bytes of the string
  19.             byte[] bytesToBeEncrypted = UTF8Encoding.UTF8.GetBytes(input);
  20.             byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
  21.  
  22.             // Hash the password with SHA256
  23.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes).Take(24).ToArray();
  24.             byte[] bytesEncrypted = TripleDES_Encrypt(bytesToBeEncrypted, passwordBytes);
  25.             return Convert.ToBase64String(bytesEncrypted);
  26.         }
  27.  
  28.         public static byte[] TripleDES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  29.         {
  30.             byte[] encryptedBytes = null;
  31.  
  32.             using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
  33.             {
  34.                 tdes.KeySize = 192;
  35.                 tdes.BlockSize = 64;
  36.                 tdes.Mode = CipherMode.ECB;
  37.                 tdes.Padding = PaddingMode.PKCS7;
  38.                 tdes.Key = passwordBytes;
  39.  
  40.                 using (ICryptoTransform encrypto = tdes.CreateEncryptor())
  41.                 {
  42.                     encryptedBytes = encrypto.TransformFinalBlock(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  43.                 }
  44.             }
  45.  
  46.             return encryptedBytes;
  47.         }
  48.  
  49.  
  50.         public static string DecryptText(string input, string password)
  51.         {
  52.             // Get the bytes of the string
  53.             byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
  54.             byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
  55.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes).Take(24).ToArray();
  56.  
  57.             byte[] bytesDecrypted = TripleDES_Decrypt(bytesToBeDecrypted, passwordBytes);
  58.             return UTF8Encoding.UTF8.GetString(bytesDecrypted);
  59.         }
  60.  
  61.         public static byte[] TripleDES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  62.         {
  63.             byte[] decryptedBytes = null;
  64.  
  65.             using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())
  66.             {
  67.                 tdes.KeySize = 192;
  68.                 tdes.BlockSize = 64;
  69.                 tdes.Mode = CipherMode.ECB;
  70.                 tdes.Padding = PaddingMode.PKCS7;
  71.                 tdes.Key = passwordBytes;
  72.  
  73.                 using (ICryptoTransform decrypto = tdes.CreateDecryptor())
  74.                 {
  75.                     decryptedBytes = decrypto.TransformFinalBlock(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  76.                 }
  77.             }
  78.  
  79.             return decryptedBytes;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement