Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- using System.Globalization;
- namespace Gerador.Core.Security
- {
- /// <summary>
- /// Cryptographic class for encryption and decryption of string values.
- /// </summary>
- public static class Crypto
- {
- // Arbitrary key and iv vector.
- // You will want to generate (and protect) your own when using encryption.
- private static string defaultKey = "6C8A39995727CA63782A55D283A43CCEC901956342AF62A1C03D92C1B1E7106D";
- private static string defaultIv = "4321E3FB5CC54D0765CCA825BE761237";
- private static AesCryptoServiceProvider aes;
- /// <summary>
- /// Default constructor. Initializes the DES3 encryption provider.
- /// </summary>
- static Crypto()
- {
- aes = new AesCryptoServiceProvider();
- aes.Mode = CipherMode.CBC;
- }
- /// <summary>
- /// Generates a 24 byte Hex key.
- /// </summary>
- /// <returns>Generated Hex key.</returns>
- public static string GenerateKey()
- {
- // Length is 24
- aes.GenerateKey();
- return BytesToHex(aes.Key);
- }
- /// <summary>
- /// Generates an 8 byte Hex IV (Initialization Vector).
- /// </summary>
- /// <returns>Initialization vector.</returns>
- public static string GenerateIV()
- {
- // Length = 8
- aes.GenerateIV();
- return BytesToHex(aes.IV);
- }
- /// <summary>
- /// Converts a hex string to a byte array.
- /// </summary>
- /// <param name="hex">Hex string.</param>
- /// <returns>Byte array.</returns>
- private static byte[] HexToBytes(string hex)
- {
- byte[] bytes = new byte[hex.Length / 2];
- for (int i = 0; i < hex.Length / 2; i++)
- {
- string code = hex.Substring(i * 2, 2);
- bytes[i] = byte.Parse(code, NumberStyles.HexNumber
- , CultureInfo.CurrentCulture);
- }
- return bytes;
- }
- /// <summary>
- /// Converts a byte array to a hex string.
- /// </summary>
- /// <param name="bytes">Byte array.</param>
- /// <returns>Converted hex string</returns>
- private static string BytesToHex(byte[] bytes)
- {
- StringBuilder hex = new StringBuilder();
- for (int i = 0; i < bytes.Length; i++)
- hex.AppendFormat("{0:X2}", bytes[i]);
- return hex.ToString();
- }
- /// <summary>
- /// Encrypts a memory string (i.e. variable).
- /// </summary>
- /// <param name="data">String to be encrypted.</param>
- /// <param name="key">Encryption key.</param>
- /// <param name="iv">Encryption initialization vector.</param>
- /// <returns>Encrypted string.</returns>
- public static string Encrypt(string data, string key, string iv)
- {
- byte[] bdata = Encoding.ASCII.GetBytes(data);
- byte[] bkey = HexToBytes(key);
- byte[] biv = HexToBytes(iv);
- MemoryStream stream = new MemoryStream();
- CryptoStream encStream = new CryptoStream(stream,
- aes.CreateEncryptor(bkey, biv), CryptoStreamMode.Write);
- encStream.Write(bdata, 0, bdata.Length);
- encStream.FlushFinalBlock();
- encStream.Close();
- return BytesToHex(stream.ToArray());
- }
- /// <summary>
- /// Decrypts a memory string (i.e. variable).
- /// </summary>
- /// <param name="data">String to be decrypted.</param>
- /// <param name="key">Original encryption key.</param>
- /// <param name="iv">Original initialization vector.</param>
- /// <returns>Decrypted string.</returns>
- public static string Decrypt(string data, string key, string iv)
- {
- byte[] bdata = HexToBytes(data);
- byte[] bkey = HexToBytes(key);
- byte[] biv = HexToBytes(iv);
- MemoryStream stream = new MemoryStream();
- CryptoStream encStream = new CryptoStream(stream,
- aes.CreateDecryptor(bkey, biv), CryptoStreamMode.Write);
- encStream.Write(bdata, 0, bdata.Length);
- encStream.FlushFinalBlock();
- encStream.Close();
- return Encoding.ASCII.GetString(stream.ToArray());
- }
- /// <summary>
- /// Standard encrypt method.
- /// Uses the predefined key and iv.
- /// </summary>
- /// <param name="data">String to be encrypted.</param>
- /// <returns>Encrypted string</returns>
- public static string DefaultEncrypt(string data)
- {
- return Encrypt(data, defaultKey, defaultIv);
- }
- /// <summary>
- /// Standard decrypt method.
- /// Uses the predefined key and iv.
- /// </summary>
- /// <param name="data">String to be decrypted.</param>
- /// <returns>Decrypted string.</returns>
- public static string DefaultDecrypt(string data)
- {
- return Decrypt(data, defaultKey, defaultIv);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement