thieumao

AES.cs Unity

Jul 6th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Diagnostics;
  8.  
  9. public class AES : MonoBehaviour {
  10.    
  11.     private const string _SALT = "g46dzQ80";
  12.     private const string _INITVECTOR = "OFRna74m*aze01xY";
  13.    
  14.     private static byte[] _saltBytes;
  15.     private static byte[] _initVectorBytes;
  16.    
  17.     static AES()
  18.     {
  19.         _saltBytes = Encoding.UTF8.GetBytes(_SALT);
  20.         _initVectorBytes = Encoding.UTF8.GetBytes(_INITVECTOR);
  21.     }
  22.  
  23.     void Start(){
  24.         //string enc = Encrypt ("thieumao","12345678");
  25.         //Debug.Log ("ket qua: " + enc);
  26.         //Debug.Log ("");
  27.     }
  28.  
  29.  
  30.    
  31.     /// <summary>
  32.     /// Encrypts a string with AES
  33.     /// </summary>
  34.     /// <param name="plainText">Text to be encrypted</param>
  35.     /// <param name="password">Password to encrypt with</param>  
  36.     /// <param name="salt">Salt to encrypt with</param>    
  37.     /// <param name="initialVector">Needs to be 16 ASCII characters long</param>    
  38.     /// <returns>An encrypted string</returns>        
  39.     public static string Encrypt(string plainText, string password, string salt = null, string initialVector = null)
  40.     {
  41.         return Convert.ToBase64String(EncryptToBytes(plainText, password, salt, initialVector));
  42.     }
  43.    
  44.     /// <summary>
  45.     /// Encrypts a string with AES
  46.     /// </summary>
  47.     /// <param name="plainText">Text to be encrypted</param>
  48.     /// <param name="password">Password to encrypt with</param>  
  49.     /// <param name="salt">Salt to encrypt with</param>    
  50.     /// <param name="initialVector">Needs to be 16 ASCII characters long</param>    
  51.     /// <returns>An encrypted string</returns>        
  52.     public static byte[] EncryptToBytes(string plainText, string password, string salt = null, string initialVector = null)
  53.     {
  54.         byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  55.         return EncryptToBytes(plainTextBytes, password, salt, initialVector);
  56.     }
  57.    
  58.     /// <summary>
  59.     /// Encrypts a string with AES
  60.     /// </summary>
  61.     /// <param name="plainTextBytes">Bytes to be encrypted</param>
  62.     /// <param name="password">Password to encrypt with</param>  
  63.     /// <param name="salt">Salt to encrypt with</param>    
  64.     /// <param name="initialVector">Needs to be 16 ASCII characters long</param>    
  65.     /// <returns>An encrypted string</returns>        
  66.     public static byte[] EncryptToBytes(byte[] plainTextBytes, string password, string salt = null, string initialVector = null)
  67.     {
  68.         int keySize = 256;
  69.        
  70.         byte[] initialVectorBytes = string.IsNullOrEmpty(initialVector) ? _initVectorBytes : Encoding.UTF8.GetBytes(initialVector);
  71.         byte[] saltValueBytes = string.IsNullOrEmpty(salt) ? _saltBytes : Encoding.UTF8.GetBytes(salt);
  72.         byte[] keyBytes = new Rfc2898DeriveBytes(password, saltValueBytes).GetBytes(keySize / 8);
  73.        
  74.         using (RijndaelManaged symmetricKey = new RijndaelManaged())
  75.         {
  76.             symmetricKey.Mode = CipherMode.CBC;
  77.            
  78.             using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes))
  79.             {
  80.                 using (MemoryStream memStream = new MemoryStream())
  81.                 {
  82.                     using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
  83.                     {
  84.                         cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  85.                         cryptoStream.FlushFinalBlock();
  86.                        
  87.                         return memStream.ToArray();
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.    
  94.     /// <summary>  
  95.     /// Decrypts an AES-encrypted string.
  96.     /// </summary>  
  97.     /// <param name="cipherText">Text to be decrypted</param>
  98.     /// <param name="password">Password to decrypt with</param>
  99.     /// <param name="salt">Salt to decrypt with</param>
  100.     /// <param name="initialVector">Needs to be 16 ASCII characters long</param>
  101.     /// <returns>A decrypted string</returns>
  102.     public static string Decrypt(string cipherText, string password, string salt = null, string initialVector = null)
  103.     {
  104.         byte[] cipherTextBytes = Convert.FromBase64String(cipherText.Replace(' ','+'));
  105.         return Decrypt(cipherTextBytes, password, salt, initialVector).TrimEnd('\0');
  106.     }
  107.    
  108.     /// <summary>  
  109.     /// Decrypts an AES-encrypted string.
  110.     /// </summary>  
  111.     /// <param name="cipherText">Text to be decrypted</param>
  112.     /// <param name="password">Password to decrypt with</param>
  113.     /// <param name="salt">Salt to decrypt with</param>
  114.     /// <param name="initialVector">Needs to be 16 ASCII characters long</param>
  115.     /// <returns>A decrypted string</returns>
  116.     public static string Decrypt(byte[] cipherTextBytes, string password, string salt = null, string initialVector = null)
  117.     {
  118.         int keySize = 256;
  119.        
  120.         byte[] initialVectorBytes = string.IsNullOrEmpty(initialVector) ? _initVectorBytes : Encoding.UTF8.GetBytes(initialVector);
  121.         byte[] saltValueBytes = string.IsNullOrEmpty(salt) ? _saltBytes : Encoding.UTF8.GetBytes(salt);
  122.         byte[] keyBytes = new Rfc2898DeriveBytes(password, saltValueBytes).GetBytes(keySize / 8);
  123.         byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  124.        
  125.         using (RijndaelManaged symmetricKey = new RijndaelManaged())
  126.         {
  127.             symmetricKey.Mode = CipherMode.CBC;
  128.            
  129.             using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
  130.             {
  131.                 using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
  132.                 {
  133.                     using (CryptoStream cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
  134.                     {
  135.                         int byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  136.                        
  137.                         return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment