Advertisement
ForeverZer0

Basic String Encryption

Apr 13th, 2012
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5.  
  6. namespace PartFinder.Data
  7. {
  8.     /// <summary>
  9.     /// Static class that utilizes a basic AES encryption for conversion on strings
  10.     /// </summary>
  11.     internal static class Encryptor
  12.     {
  13.         #region Fields
  14.         // Change these keys
  15.         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 };
  16.         private static readonly byte[] Vector = { 146, 64, 191, 111, 23, 3, 113, 119, 231, 121, 252, 112, 79, 32, 114, 156 };
  17.         private static ICryptoTransform encryptorTransform, decryptorTransform;
  18.         private static UTF8Encoding utfEncoder;
  19.         #endregion
  20.  
  21.         #region Properties
  22.         private static ICryptoTransform EncryptorTransform
  23.         {
  24.             get
  25.             {
  26.                 if (encryptorTransform == null)
  27.                     Initialize();
  28.                 return encryptorTransform;
  29.             }
  30.         }
  31.         private static ICryptoTransform DecryptorTransform
  32.         {
  33.             get
  34.             {
  35.                 if (decryptorTransform == null)
  36.                     Initialize();
  37.                 return decryptorTransform;
  38.             }
  39.         }
  40.         private static UTF8Encoding UTFEncoder
  41.         {
  42.             get
  43.             {
  44.                 if (utfEncoder == null)
  45.                     Initialize();
  46.                 return utfEncoder;
  47.             }
  48.         }
  49.         #endregion
  50.  
  51.         /// <summary>
  52.         /// Initializes the private fields used in the class
  53.         /// </summary>
  54.         private static void Initialize()
  55.         {
  56.             RijndaelManaged rm = new RijndaelManaged();
  57.             encryptorTransform = rm.CreateEncryptor(Key, Vector);
  58.             decryptorTransform = rm.CreateDecryptor(Key, Vector);
  59.             utfEncoder = new UTF8Encoding();
  60.         }
  61.        
  62.         /// <summary>
  63.         /// Generates an encryption key
  64.         /// </summary>
  65.         /// <returns>The key as an array of bytes</returns>
  66.         public static byte[] GenerateEncryptionKey()
  67.         {
  68.             RijndaelManaged rm = new RijndaelManaged();
  69.             rm.GenerateKey();
  70.             return rm.Key;
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Generates an encryption vector
  75.         /// </summary>
  76.         /// <returns>The vector as an array of bytes</returns>
  77.         static public byte[] GenerateEncryptionVector()
  78.         {
  79.             RijndaelManaged rm = new RijndaelManaged();
  80.             rm.GenerateIV();
  81.             return rm.IV;
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Encrypts a string and returns it
  86.         /// </summary>
  87.         /// <param name="text">The text to encrypt</param>
  88.         /// <returns>A string that is suitable for using in a URL</returns>
  89.         public static string EncryptToString(string text)
  90.         {
  91.             return ByteArrToString(Encrypt(text));
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Encrypts a string and returns it as an array of bytes
  96.         /// </summary>
  97.         /// <param name="text">The string to encrypt</param>
  98.         /// <returns>A byte array representing the encrypted string</returns>
  99.         public static byte[] Encrypt(string text)
  100.         {
  101.             byte[] bytes = UTFEncoder.GetBytes(text);
  102.             byte[] encrypted;
  103.             using (MemoryStream memoryStream = new MemoryStream())
  104.             {
  105.                 using (CryptoStream cs = new CryptoStream(memoryStream, EncryptorTransform, CryptoStreamMode.Write))
  106.                 {
  107.                     cs.Write(bytes, 0, bytes.Length);
  108.                     cs.FlushFinalBlock();
  109.                     memoryStream.Position = 0;
  110.                     encrypted = new byte[memoryStream.Length];
  111.                     memoryStream.Read(encrypted, 0, encrypted.Length);
  112.                 }
  113.             }
  114.  
  115.             return encrypted;
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Decrypts a string and returns it
  120.         /// </summary>
  121.         /// <param name="text">The string to decrypt</param>
  122.         /// <returns>The decrypted string</returns>
  123.         public static string DecryptString(string text)
  124.         {
  125.             return Decrypt(StrToByteArray(text));
  126.         }
  127.  
  128.         /// <summary>
  129.         /// Decrypts an array if bytes and returns it as a string
  130.         /// </summary>
  131.         /// <param name="byteArray">The array if bytes to decrypt</param>
  132.         /// <returns>The decrypted string</returns>
  133.         public static string Decrypt(byte[] byteArray)
  134.         {
  135.             byte[] decryptedBytes;
  136.             using (MemoryStream encryptedStream = new MemoryStream())
  137.             {
  138.                 using (CryptoStream decryptStream = new CryptoStream(encryptedStream, DecryptorTransform, CryptoStreamMode.Write))
  139.                 {
  140.                     decryptStream.Write(byteArray, 0, byteArray.Length);
  141.                     decryptStream.FlushFinalBlock();
  142.                     encryptedStream.Position = 0;
  143.                     decryptedBytes = new Byte[encryptedStream.Length];
  144.                     encryptedStream.Read(decryptedBytes, 0, decryptedBytes.Length);
  145.                 }
  146.             }
  147.             return UTFEncoder.GetString(decryptedBytes);
  148.         }
  149.  
  150.         /// <summary>
  151.         /// Converts a string to an array of bytes.
  152.         /// </summary>
  153.         /// <param name="str">The string to convert</param>
  154.         /// <returns>A string representation of the array. All values less than 100 are padded with 0's</returns>
  155.         public static byte[] StrToByteArray(string str)
  156.         {
  157.             if (str.Length == 0)
  158.                 throw new Exception("Invalid string value in StrToByteArray");
  159.  
  160.             byte val;
  161.             byte[] byteArr = new byte[str.Length / 3];
  162.             int i = 0;
  163.             int j = 0;
  164.             do
  165.             {
  166.                 val = byte.Parse(str.Substring(i, 3));
  167.                 byteArr[j++] = val;
  168.                 i += 3;
  169.             }
  170.             while (i < str.Length);
  171.             return byteArr;
  172.         }
  173.  
  174.         /// <summary>
  175.         /// Converts an array of bytes to a string
  176.         /// </summary>
  177.         /// <param name="byteArr">The array of bytes to convert</param>
  178.         /// <returns>The converted string</returns>
  179.         public static string ByteArrToString(byte[] byteArr)
  180.         {
  181.             byte val;
  182.             string tempStr = "";
  183.             for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
  184.             {
  185.                 val = byteArr[i];
  186.                 if (val < (byte)10)
  187.                     tempStr += "00" + val.ToString();
  188.                 else if (val < (byte)100)
  189.                     tempStr += "0" + val.ToString();
  190.                 else
  191.                     tempStr += val.ToString();
  192.             }
  193.             return tempStr;
  194.         }
  195.  
  196.         /// <summary>
  197.         /// Compares a string with an encrypted one and returns true if they are the same
  198.         /// </summary>
  199.         /// <param name="text">The string passed to compare in human-readable format</param>
  200.         /// <param name="encrypted">The encrypted string to compare it to</param>
  201.         /// <returns>The result of the comparison</returns>
  202.         public static bool Validate(string text, string encrypted)
  203.         {
  204.             return DecryptString(encrypted) == text;
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement