c# 解密微信encryptedData字段

2018-06-17 18:59:57来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

参考链接:https://www.cnblogs.com/jetz/p/6384809.html

我写了一个工具方法,直接照搬链接中的方法,还有一个工具类。

    public class Encrypt
    {
        #region SHA1解密

        /// <summary>
        /// 解密小程序的encryptedData
        /// </summary>
        /// <param name="encryptedData">加密的信息</param>
        /// <param name="sessionKey">key</param>
        /// <param name="iv">加密算法的初始向量</param>
        public static WxPhoneModel DescodeWxSHA1(string encryptedData, string sessionKey, string iv)
        {
            WxPhoneModel model = null;
            var res = AESDecrypt(encryptedData, sessionKey, iv);// {"phoneNumber":"152XXXX9583","purePhoneNumber":"1525XXXX3","countryCode":"86","watermark":{"timestamp":1525829586,"appid":"wx38XXXXXXXX43"}} 
            if (!string.IsNullOrEmpty(res))
            {
                model = JObject.Parse(res).ToObject<WxPhoneModel>();
            }
            return model;
        }

        public static string AESDecrypt(string encryptedData, string sessionKey, string iv)
        {
            try
            {
                //16进制数据转换成byte
                var encryptedDataByte = Convert.FromBase64String(encryptedData);  // strToToHexByte(text);
                var rijndaelCipher = new RijndaelManaged
                {
                    Key = Convert.FromBase64String(sessionKey),
                    IV = Convert.FromBase64String(iv),
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.PKCS7
                };
                
                var transform = rijndaelCipher.CreateDecryptor();
                var plainText = transform.TransformFinalBlock(encryptedDataByte, 0, encryptedDataByte.Length);
                var result = Encoding.Default.GetString(plainText);
                
                return result;
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        #endregion
    }
public class WxPhoneModel { /// <summary> /// 用户绑定的手机号(国外手机号会有区号) /// </summary> public string PhoneNumber { set; get; } /// <summary> /// 没有区号的手机号 /// </summary> public string PurePhoneNumber { set; get; } /// <summary> /// 区号 /// </summary> public string CountryCode { set; get; } /// <summary> /// 水印 /// </summary> public WaterMarkModel WaterMark { set; get; } } public class WaterMarkModel { /// <summary> /// appid /// </summary> public string AppId { set; get; } /// <summary> /// 时间戳 /// </summary> public string TimeStamp { set; get; } }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JSON C# Class Generator ---由json字符串生成C#实体类的工具

下一篇:C# Json反序列化