Unity——AES-256-CBC加密解密

Posted 格拉格拉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity——AES-256-CBC加密解密相关的知识,希望对你有一定的参考价值。

RijndaelManaged rm = new RijndaelManaged();

 private void Awake()
    {
        rm.BlockSize = 128;
        rm.KeySize = 256;
        rm.FeedbackSize = 128;
        rm.Padding = PaddingMode.PKCS7;
        rm.Mode = CipherMode.CBC;

        rm.Key = (new SHA256Managed()).ComputeHash(Encoding.ASCII.GetBytes("2b58592aab597b16c0e04477"));
        rm.IV = Encoding.ASCII.GetBytes("8d3affe547824a710817ccd46f");
    }

    


    //加密
    public string AESEncrypt(string msg)
    {
        ICryptoTransform encryptor = rm.CreateEncryptor(rm.Key, rm.IV);
        byte[] _bytes;
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(csEncrypt))
                {
                    sw.Write(msg);
                }
                _bytes = ms.ToArray();
            }
        }
        return Convert.ToBase64String(_bytes);
    }

    //解密
    public string AESDecrtpt(byte[] msg)
    {
        ICryptoTransform ict = rm.CreateDecryptor(rm.Key, rm.IV);

        string backString;
        using (MemoryStream ms = new MemoryStream(msg))
        {
            using (CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read))
            {
                using (StreamReader sr = new StreamReader(cs))
                {
                    backString = sr.ReadToEnd();
                }
            }
        }
        return backString;
    }

以上是关于Unity——AES-256-CBC加密解密的主要内容,如果未能解决你的问题,请参考以下文章

错误的加密(QT c++ OpenSSL AES 256 CBC)

使用OpenSSL进行AES-256 / CBC加密并使用C#进行解密

php openssl aes-256-cbc key长度自动匹配了128的长度,为啥

无法使用来自 AES-256-CBC 的 pgcrypto 解密,但 AES-128-CBC 可以

AES-256-CBC用PHP加密并用Java解密

前端加密-aes-256-cbc(服务端是php,项目是用的vue)