您现在的位置: 微信小程序 > 微信小程序开发 > 教程 >

【C#】小程序解密,官方Demo竟然没有C#,贴这里备用

来源:微信小程序 编辑:Yiyongtong.com 发布时间:2018-02-10 12:07热度:
[C#] 
public string AES_decrypt(string encryptedDataStr, string key, string iv)


        {

            RijndaelManaged rijalg = new RijndaelManaged();

            //-----------------    

            //设置 cipher 格式 AES-128-CBC    

            rijalg.KeySize = 128;

            rijalg.Padding = PaddingMode.PKCS7;

            rijalg.Mode = CipherMode.CBC;

            rijalg.Key = Convert.FromBase64String(key);

            rijalg.IV = Convert.FromBase64String(iv);

            byte[] encryptedData = Convert.FromBase64String(encryptedDataStr);

            //解密    

            ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);

            string result;

            using (MemoryStream msDecrypt = new MemoryStream(encryptedData))

            {

                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))

                {

                    using (StreamReader srDecrypt = new StreamReader(csDecrypt)

                    {

                        result= srDecrypt.ReadToEnd();

                    }

                }

            }

            return result;

        }