Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace PartFinder.Data
- {
- /// <summary>
- /// Static class that utilizes a basic AES encryption for conversion on strings
- /// </summary>
- internal static class Encryptor
- {
- #region Fields
- // Change these keys
- private static readonly byte[] Key = { 123, 217, 19, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 112, 222, 209, 241, 24, 175, 144, 173, 53, 196, 29, 24, 26, 17, 218, 131, 236, 53, 209 };
- private static readonly byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };
- private static ICryptoTransform encryptorTransform, decryptorTransform;
- private static UTF8Encoding utfEncoder;
- #endregion
- #region Properties
- private static ICryptoTransform EncryptorTransform
- {
- get
- {
- if (encryptorTransform == null)
- Initialize();
- return encryptorTransform;
- }
- }
- private static ICryptoTransform DecryptorTransform
- {
- get
- {
- if (decryptorTransform == null)
- Initialize();
- return decryptorTransform;
- }
- }
- private static UTF8Encoding UTFEncoder
- {
- get
- {
- if (utfEncoder == null)
- Initialize();
- return utfEncoder;
- }
- }
- #endregion
- /// <summary>
- /// Initializes the private fields used in the class
- /// </summary>
- private static void Initialize()
- {
- RijndaelManaged rm = new RijndaelManaged();
- encryptorTransform = rm.CreateEncryptor(Key, Vector);
- decryptorTransform = rm.CreateDecryptor(Key, Vector);
- utfEncoder = new UTF8Encoding();
- }
- /// <summary>
- /// Generates an encryption key
- /// </summary>
- /// <returns>The key as an array of bytes</returns>
- public static byte[] GenerateEncryptionKey()
- {
- RijndaelManaged rm = new RijndaelManaged();
- rm.GenerateKey();
- return rm.Key;
- }
- /// <summary>
- /// Generates an encryption vector
- /// </summary>
- /// <returns>The vector as an array of bytes</returns>
- static public byte[] GenerateEncryptionVector()
- {
- RijndaelManaged rm = new RijndaelManaged();
- rm.GenerateIV();
- return rm.IV;
- }
- /// <summary>
- /// Encrypts a string and returns it
- /// </summary>
- /// <param name="text">The text to encrypt</param>
- /// <returns>A string that is suitable for using in a URL</returns>
- public static string EncryptToString(string text)
- {
- return ByteArrToString(Encrypt(text));
- }
- /// <summary>
- /// Encrypts a string and returns it as an array of bytes
- /// </summary>
- /// <param name="text">The string to encrypt</param>
- /// <returns>A byte array representing the encrypted string</returns>
- public static byte[] Encrypt(string text)
- {
- byte[] bytes = UTFEncoder.GetBytes(text);
- byte[] encrypted;
- using (MemoryStream memoryStream = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write))
- {
- cs.Write(bytes, 0, bytes.Length);
- cs.FlushFinalBlock();
- memoryStream.Position = 0;
- encrypted = new byte[memoryStream.Length];
- memoryStream.Read(encrypted, 0, encrypted.Length);
- }
- }
- return encrypted;
- }
- /// <summary>
- /// Decrypts a string and returns it
- /// </summary>
- /// <param name="text">The string to decrypt</param>
- /// <returns>The decrypted string</returns>
- public static string DecryptString(string text)
- {
- return Decrypt(StrToByteArray(text));
- }
- /// <summary>
- /// Decrypts an array if bytes and returns it as a string
- /// </summary>
- /// <param name="byteArray">The array if bytes to decrypt</param>
- /// <returns>The decrypted string</returns>
- public static string Decrypt(byte[] byteArray)
- {
- byte[] decryptedBytes;
- using (MemoryStream encryptedStream = new MemoryStream())
- {
- using (CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write))
- {
- decryptStream.Write(byteArray, 0, byteArray.Length);
- decryptStream.FlushFinalBlock();
- encryptedStream.Position = 0;
- decryptedBytes = new Byte[encryptedStream.Length];
- encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
- }
- }
- return UTFEncoder.GetString(decryptedBytes);
- }
- /// <summary>
- /// Converts a string to an array of bytes.
- /// </summary>
- /// <param name="str">The string to convert</param>
- /// <returns>A string representation of the array. All values less than 100 are padded with 0's</returns>
- public static byte[] StrToByteArray(string str)
- {
- if (str.Length == 0)
- throw new Exception("Invalid string value in StrToByteArray");
- byte val;
- byte[] byteArr = new byte[str.Length / 3];
- int i = 0;
- int j = 0;
- do
- {
- val = byte.Parse(str.Substring(i, 3));
- byteArr[j++] = val;
- i += 3;
- }
- while (i < str.Length);
- return byteArr;
- }
- /// <summary>
- /// Converts an array of bytes to a string
- /// </summary>
- /// <param name="byteArr">The array of bytes to convert</param>
- /// <returns>The converted string</returns>
- public static string ByteArrToString(byte[] byteArr)
- {
- byte val;
- string tempStr = "";
- for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
- {
- val = byteArr[i];
- if (val < (byte)10)
- tempStr += "00" + val.ToString();
- else if (val < (byte)100)
- tempStr += "0" + val.ToString();
- else
- tempStr += val.ToString();
- }
- return tempStr;
- }
- /// <summary>
- /// Compares a string with an encrypted one and returns true if they are the same
- /// </summary>
- /// <param name="text">The string passed to compare in human-readable format</param>
- /// <param name="encrypted">The encrypted string to compare it to</param>
- /// <returns>The result of the comparison</returns>
- public static bool Validate(string text, string encrypted)
- {
- return DecryptString(encrypted) == text;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement