Unity C# 脚本实现AES加密解密

   /// 
    /// AES加密
    /// 
    /// 明文
    /// 密钥
    /// 
    public static byte[] Encrypt(byte[] toEncryptArray, string key)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);        
        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rDel.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        return resultArray;
    }

    /// 
    /// AES解密
    /// 
    /// 密文
    /// 密钥
    /// 
    public static byte[] Decrypt(byte[] toEncryptArray, string key)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);        
        RijndaelManaged rDel = new RijndaelManaged();
        rDel.Key = keyArray;
        rDel.Mode = CipherMode.ECB;
        rDel.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rDel.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        return resultArray;
    }

 

你可能感兴趣的:(Unity3D)