C#实现微信AES-128-CBC加密数据的解密

1.微信小程序登录获取用户的openid信息

需要使用AES-128-CBC解密处理

    /// <summary>
    /// ASE加解密
    /// </summary>
    public class AESHelper
    {
        /// <summary>
        /// 秘钥
        /// </summary>
        public static string AesKey;
        /// <summary>
        /// 16位初始向量
        /// </summary>
        public static string AesIV;
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="password"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AESDecrypt(string text)
        {
            try
            {
                //16进制数据转换成byte
                byte[] encryptedData = Convert.FromBase64String(text);  // strToToHexByte(text);
                RijndaelManaged rijndaelCipher = new RijndaelManaged();
                rijndaelCipher.Key = Convert.FromBase64String(AesKey); // Encoding.UTF8.GetBytes(AesKey);
                rijndaelCipher.IV = Convert.FromBase64String(AesIV);// Encoding.UTF8.GetBytes(AesIV);
                rijndaelCipher.Mode = CipherMode.CBC;
                rijndaelCipher.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
                byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
                string result = Encoding.Default.GetString(plainText);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }

测试代码:

AESHelper.AesIV = "r7BXXKkLb8qrSNn05n0qiA==";
AESHelper.AesKey = "tiihtNczf5v6AKRyjwEUhQ==";
string text =
    "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM" +
    "QmRzooG2xrDcvSnxIMXFufNstNGTyaGS" +
    "9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+" +
    "3hVbJSRgv+4lGOETKUQz6OYStslQ142d" +
    "NCuabNPGBzlooOmB231qMM85d2/fV6Ch" +
    "evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6" +
    "/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw" +
    "u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn" +
    "/Hz7saL8xz+W//FRAUid1OksQaQx4CMs" +
    "8LOddcQhULW4ucetDf96JcR3g0gfRK4P" +
    "C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB" +
    "6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns" +
    "/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd" +
    "lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV" +
    "oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG" +
    "20f0a04COwfneQAGGwd5oa+T8yO5hzuy" +
    "Db/XcxxmK01EpqOyuxINew==";
string s = AESHelper.AESDecrypt(text);
Console.WriteLine(s);

原文参考:https://www.cnblogs.com/jetz/p/6384809.html

 

更多:

 

C# 调用微信公众号接口获取会员信息示例

C# 调用微信公众号接口发送客服消息示例

C# 调用微信公众号接口生成带参数二维码、下载、合并

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值