微信数据解密C#
发布于 3 年前 作者 xiulan23 4455 次浏览 来自 分享

NetCore C# 微信数据解密

        /// <summary>
        /// 微信数据解密
        /// </summary>
        /// <param name="wXKey"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static string WechatDecrypt(WXKeyParam wXKey)
        {
            //base64解码为字节数组
            var encryptData = Convert.FromBase64String(wXKey.encryptedData);
            //创建aes对象
            Aes aes = Aes.Create();
            if (aes == null)
            {
                throw new InvalidOperationException("未能获取Aes算法实例");
            }
            aes.Mode = CipherMode.CBC;  //设置模式为CBC
            aes.KeySize = 128;  //设置Key大小
            aes.Padding = PaddingMode.PKCS7;    //设置填充
            aes.Key = Convert.FromBase64String(wXKey.sessionKey);
            aes.IV = Convert.FromBase64String(wXKey.encryptIv);
            var de = aes.CreateDecryptor();//创建解密器
            var decodeByteData = de.TransformFinalBlock(encryptData, 0, encryptData.Length);//解密数据
            var data = Encoding.UTF8.GetString(decodeByteData);//转换为字符串
            return data;
        }

//传入参数类

{
    public class WXKeyParam
    {
        /// <summary>
        /// 
        /// </summary>
        public string encryptedData { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string encryptIv { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string sessionKey { get; set; }
    }
回到顶部