Unity中添加AES加密

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

/// 
/// AES加密算法
/// 
public class AESEncryption : MonoBehaviour {

    /// 
    /// 默认密钥向量
    /// 
    private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// 
    /// AES加密算法
    /// 
    /// 明文字符串
    /// 密钥
    /// 返回加密后的密文字节数组
    public static byte[] AESEncrypt(string plainText, string strKey)
    {
        //分组加密算法
        SymmetricAlgorithm des = Rijndael.Create();
        des.Padding = PaddingMode.None;
        byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
        int covering = (inputByteArray.Length + 1) % 16;
        int coveringLength = 0;
        if (covering != 0)//手动补位
        {
            coveringLength = 16 - covering;
        }
        int dataLength = (inputByteArray.Length + 1) + coveringLength;
        byte[] dataArray = new byte[dataLength];
        Buffer.BlockCopy(inputByteArray, 0, dataArray, 0, inputByteArray.Length);
        dataArray[dataArray.Length - 1] = Convert.ToByte(coveringLength);
        //设置密钥及密钥向量
        des.Key = Encoding.UTF8.GetBytes(strKey);
        des.IV = _key1;
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(dataArray, 0, dataArray.Length);
        cs.FlushFinalBlock();
        byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
        cs.Close();
        ms.Close();
        return cipherBytes;

    }

    /// 
    /// AES加密算法
    /// 
    /// 明文
    /// 密钥
    /// 返回加密后的密文字节数组
    public static byte[] AESEncrypt(byte[] inputByteArray, string strKey)
    {
        //分组加密算法
        SymmetricAlgorithm des = Rijndael.Create();
        des.Padding = PaddingMode.None;
        int covering = (inputByteArray.Length + 1) % 16;
        int coveringLength = 0;
        if (covering != 0)//手动补位
        {
            coveringLength = 16 - covering;
        }
        int dataLength = (inputByteArray.Length + 1) + coveringLength;
        byte[] dataArray = new byte[dataLength];
        Buffer.BlockCopy(inputByteArray, 0, dataArray, 0, inputByteArray.Length);
        dataArray[dataArray.Length - 1] = Convert.ToByte(coveringLength);
        //设置密钥及密钥向量
        des.Key = Encoding.UTF8.GetBytes(strKey);
        des.IV = _key1;
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(dataArray, 0, dataArray.Length);
        cs.FlushFinalBlock();
        byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
        cs.Close();
        ms.Close();
        return cipherBytes;
    }
    /// 
    /// AES解密
    /// 
    /// 密文字节数组
    /// 密钥
    /// 返回解密后的字符串
    public static byte[] AESDecrypt(byte[] cipherText, string strKey)
    {
        SymmetricAlgorithm des = Rijndael.Create();
        des.Key = Encoding.UTF8.GetBytes(strKey);
        des.IV = _key1;
        des.Padding = PaddingMode.None;
        byte[] decryptBytes = new byte[cipherText.Length];
        MemoryStream ms = new MemoryStream(cipherText);
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
        cs.Read(decryptBytes, 0, decryptBytes.Length);
        int length = Convert.ToInt32(decryptBytes.Length - 1);
        byte[] databytes = new byte[decryptBytes.Length - 1 - length];
        Buffer.BlockCopy(decryptBytes, 0, databytes, 0, databytes.Length);
        cs.Close();
        ms.Close();
        return databytes;
    }

}
using UnityEngine;
using System.Collections;

public class StartGame : MonoBehaviour {

    /// 
    /// 密钥(系统自动随机的密钥)
    /// 
    string keys = "";
    /// 
    /// 注册码(玩家输入的注册码)
    /// 
    string inputLicense = "";

    string numChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    void Awake()
    {
        #region 获取随机秘钥
        for (int i = 0; i < 16; i++)
        {
            keys += numChar.Substring(Random.Range(0, 61), 1);
        }
        Debug.Log("keys: " + keys);
        #endregion
    }

	// Use this for initialization
	void Start ()
    {

        #region 系统根据密钥自动得出的注册码
        string outLicense = "";
        for (int i = 0; i < 16; i++)
        {
            outLicense += AESEncryption.AESEncrypt("AEROSPACE", keys)[i].ToString("X");
        }
        Debug.Log("outLicense: " + outLicense);
        #endregion

        #region 注册码进行对比
        if (inputLicense == outLicense)
        {
            Debug.Log("验证通过");            
        }
        else
        {
            Debug.Log("验证未通过");
        }
        #endregion

    }
	
	// Update is called once per frame
	void Update () {
	
	}
}


你可能感兴趣的:(unity)