﻿using UnityEngine;
using System.Collections;

public static class AESTool {	
	/// <summary>
	/// AES加密
	/// </summary>
	/// <param name="strAesKey">加密Key</param>
	/// <param name="toEncryptArray">要加密的byte[]</param>
	/// <returns>返回加密后byte[]</returns>
	public static byte[] Encrypt_AES(string strAesKey,byte[] toEncryptArray)
	{
		if(toEncryptArray.Length<1)
			return toEncryptArray;
		byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
		System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
		rDel.Key = keyArray;
		rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
		rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;		
		System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);		
		cTransform.Dispose();
		return resultArray;
	}

	/// <summary>
	/// AES加密
	/// </summary>
	/// <param name="strAesKey">加密Key</param>
	/// <param name="str">要加密字符串</param>
	/// <returns>返回加密后的Base64字符串</returns>
	public static string Encrypt_AES(string strAesKey,string str)
	{
		if(string.IsNullOrEmpty(str))
			return str;
		byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
		byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);		
		System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
		rDel.Key = keyArray;
		rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
		rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;		
		System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
		cTransform.Dispose();
		return System.Convert.ToBase64String(resultArray, 0, resultArray.Length);

	}	
	/// <summary>
	/// AES解密
	/// </summary>
	/// <param name="strAesKey">加密Key</param>
	/// <param name="toEncryptArray">要解密byte[]</param>
	/// <returns>返回解密后byte[]</returns>
	public static byte[] Decrypt_AES(string strAesKey,byte[] toEncryptArray)
	{
		if(toEncryptArray.Length<1)
			return toEncryptArray;
		byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
		System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
		rDel.Key = keyArray;
		rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
		rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;		
		System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
		cTransform.Dispose();
		return resultArray;
	}
	/// <summary>
	/// AES解密
	/// </summary>
	/// <param name="strAesKey">加密Key</param>
	/// <param name="str">要解密Base64字符串</param>
	/// <returns>返回解密后字符串</returns>
	public static string Decrypt_AES(string strAesKey,string str)
	{
		if(string.IsNullOrEmpty(str))
			return str;
		byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey);
		byte[] toEncryptArray = System.Convert.FromBase64String(str);
		
		System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
		rDel.Key = keyArray;
		rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
		rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
		
		System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
		byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
		cTransform.Dispose();
		return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
	}

}
