【代码】C#解密微信小程序encryptedData

2020-10-19 16:08:25  阅读 2443 次 评论 0 条

C#解密encryptedData

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.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("\\",""));
            rijalg.IV = Convert.FromBase64String(iv.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("\\", ""));


            byte[] encryptedData = Convert.FromBase64String(encryptedDataStr.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+").Replace("\\", ""));
            //解密    
            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;
        }



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2107
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?