Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. using System.Globalization;
  8.  
  9. namespace Gerador.Core.Security
  10. {
  11.     /// <summary>
  12.     /// Cryptographic class for encryption and decryption of string values.
  13.     /// </summary>
  14.     public static class Crypto
  15.     {
  16.         // Arbitrary key and iv vector.
  17.         // You will want to generate (and protect) your own when using encryption.
  18.         private static string defaultKey = "6C8A39995727CA63782A55D283A43CCEC901956342AF62A1C03D92C1B1E7106D";
  19.         private static string defaultIv = "4321E3FB5CC54D0765CCA825BE761237";
  20.  
  21.         private static AesCryptoServiceProvider aes;
  22.  
  23.         /// <summary>
  24.         /// Default constructor. Initializes the DES3 encryption provider.
  25.         /// </summary>
  26.         static Crypto()
  27.         {
  28.             aes = new AesCryptoServiceProvider();
  29.             aes.Mode = CipherMode.CBC;
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Generates a 24 byte Hex key.
  34.         /// </summary>
  35.         /// <returns>Generated Hex key.</returns>
  36.         public static string GenerateKey()
  37.         {
  38.             // Length is 24
  39.             aes.GenerateKey();
  40.             return BytesToHex(aes.Key);
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Generates an 8 byte Hex IV (Initialization Vector).
  45.         /// </summary>
  46.         /// <returns>Initialization vector.</returns>
  47.         public static string GenerateIV()
  48.         {
  49.             // Length = 8
  50.             aes.GenerateIV();
  51.             return BytesToHex(aes.IV);
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Converts a hex string to a byte array.
  56.         /// </summary>
  57.         /// <param name="hex">Hex string.</param>
  58.         /// <returns>Byte array.</returns>
  59.         private static byte[] HexToBytes(string hex)
  60.         {
  61.             byte[] bytes = new byte[hex.Length / 2];
  62.             for (int i = 0; i < hex.Length / 2; i++)
  63.             {
  64.                 string code = hex.Substring(i * 2, 2);
  65.                 bytes[i] = byte.Parse(code, NumberStyles.HexNumber
  66.                     , CultureInfo.CurrentCulture);
  67.             }
  68.             return bytes;
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Converts a byte array to a hex string.
  73.         /// </summary>
  74.         /// <param name="bytes">Byte array.</param>
  75.         /// <returns>Converted hex string</returns>
  76.         private static string BytesToHex(byte[] bytes)
  77.         {
  78.             StringBuilder hex = new StringBuilder();
  79.             for (int i = 0; i < bytes.Length; i++)
  80.                 hex.AppendFormat("{0:X2}", bytes[i]);
  81.             return hex.ToString();
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Encrypts a memory string (i.e. variable).
  86.         /// </summary>
  87.         /// <param name="data">String to be encrypted.</param>
  88.         /// <param name="key">Encryption key.</param>
  89.         /// <param name="iv">Encryption initialization vector.</param>
  90.         /// <returns>Encrypted string.</returns>
  91.         public static string Encrypt(string data, string key, string iv)
  92.         {
  93.             byte[] bdata = Encoding.ASCII.GetBytes(data);
  94.             byte[] bkey = HexToBytes(key);
  95.             byte[] biv = HexToBytes(iv);
  96.  
  97.             MemoryStream stream = new MemoryStream();
  98.             CryptoStream encStream = new CryptoStream(stream,
  99.                 aes.CreateEncryptor(bkey, biv), CryptoStreamMode.Write);
  100.  
  101.             encStream.Write(bdata, 0, bdata.Length);
  102.             encStream.FlushFinalBlock();
  103.             encStream.Close();
  104.  
  105.             return BytesToHex(stream.ToArray());
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Decrypts a memory string (i.e. variable).
  110.         /// </summary>
  111.         /// <param name="data">String to be decrypted.</param>
  112.         /// <param name="key">Original encryption key.</param>
  113.         /// <param name="iv">Original initialization vector.</param>
  114.         /// <returns>Decrypted string.</returns>
  115.         public static string Decrypt(string data, string key, string iv)
  116.         {
  117.             byte[] bdata = HexToBytes(data);
  118.             byte[] bkey = HexToBytes(key);
  119.             byte[] biv = HexToBytes(iv);
  120.  
  121.             MemoryStream stream = new MemoryStream();
  122.             CryptoStream encStream = new CryptoStream(stream,
  123.                 aes.CreateDecryptor(bkey, biv), CryptoStreamMode.Write);
  124.  
  125.             encStream.Write(bdata, 0, bdata.Length);
  126.             encStream.FlushFinalBlock();
  127.             encStream.Close();
  128.  
  129.             return Encoding.ASCII.GetString(stream.ToArray());
  130.         }
  131.  
  132.         /// <summary>
  133.         /// Standard encrypt method.
  134.         /// Uses the predefined key and iv.
  135.         /// </summary>
  136.         /// <param name="data">String to be encrypted.</param>
  137.         /// <returns>Encrypted string</returns>
  138.         public static string DefaultEncrypt(string data)
  139.         {
  140.             return Encrypt(data, defaultKey, defaultIv);
  141.         }
  142.  
  143.         /// <summary>
  144.         /// Standard decrypt method.
  145.         /// Uses the predefined key and iv.
  146.         /// </summary>
  147.         /// <param name="data">String to be decrypted.</param>
  148.         /// <returns>Decrypted string.</returns>
  149.         public static string DefaultDecrypt(string data)
  150.         {
  151.             return Decrypt(data, defaultKey, defaultIv);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement