Unity||CSharp 加密解密

  public class Aes
    {
        private string _key;

        public Aes(string key)
        {
            if (_key.Length != 32)
                throw new Exception("please check key, current length is: " + _key.Length);
            _key = key;
        }

        ///
        ///加密
        ///
        ///需要被加密的数据
        ///
        public byte[] Encrypt(byte[] toEncrypt)
        {
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(_key);
            System.Security.Cryptography.RijndaelManaged aes = new System.Security.Cryptography.RijndaelManaged();
            aes.Key = keyArray;
            aes.Mode = System.Security.Cryptography.CipherMode.ECB;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.Security.Cryptography.ICryptoTransform transform = aes.CreateEncryptor();
            Byte[] resultArray = transform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
            return resultArray;
        }

        ///
        ///解密
        ///
        ///需要被解密的数据
        ///
        public byte[] Decrypt(byte[] toDecrypt)
        {
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(_key);
            System.Security.Cryptography.RijndaelManaged aes = new System.Security.Cryptography.RijndaelManaged();
            aes.Key = keyArray;
            aes.Mode = System.Security.Cryptography.CipherMode.ECB;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.Security.Cryptography.ICryptoTransform transform = aes.CreateDecryptor();
            Byte[] resultArray = transform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length);
            return resultArray;
        }
    }

 

你可能感兴趣的:(C#,Unity,3D)