在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//微信也不给个c#调用的例子 只好自己造咯;ps:大佬勿喷 #region 微信小程序用户数据解密 public static string AesKey; public static string AesIV; /// <summary> /// AES解密 /// </summary> /// <param name="inputdata">输入的数据encryptedData</param> /// <param name="AesKey">key</param> /// <param name="AesIV">向量128</param> /// <returns name="result">解密后的字符串</returns> public string AESDecrypt(string inputdata) { try { AesIV = AesIV.Replace(" ", "+"); AesKey = AesKey.Replace(" ", "+"); inputdata = inputdata.Replace(" ", "+"); byte[] encryptedData = Convert.FromBase64String(inputdata); 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.UTF8.GetString(plainText); return result; } catch (Exception) { return null; } } /// <summary> /// 根据微信小程序平台提供的解密算法解密数据 /// </summary> /// <param name="encryptedData">加密数据</param> /// <param name="iv">初始向量</param> /// <param name="sessionKey">从服务端获取的SessionKey</param> /// <returns></returns> public userInfo Decrypt(string encryptedData, string iv, string sessionKey) { userInfo userInfo; //创建解密器生成工具实例 AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); //设置解密器参数 aes.Mode = CipherMode.CBC; aes.BlockSize = 128; aes.Padding = PaddingMode.PKCS7; //格式化待处理字符串 byte[] byte_encryptedData = Convert.FromBase64String(encryptedData); byte[] byte_iv = Convert.FromBase64String(iv); byte[] byte_sessionKey = Convert.FromBase64String(sessionKey); aes.IV = byte_iv; aes.Key = byte_sessionKey; //根据设置好的数据生成解密器实例 ICryptoTransform transform = aes.CreateDecryptor(); //解密 byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length); //生成结果 string result = Encoding.UTF8.GetString(final); //反序列化结果,生成用户信息实例 userInfo = JsonConvert.DeserializeObject<userInfo>(result); return userInfo; } #endregion } public class wechat { } #region 实体类 /// <summary> /// 微信用户类 /// </summary> public class userInfo { public string openId { get; set; } public string nickName { get; set; } public string gender { get; set; } public string city { get; set; } public string province { get; set; } public string country { get; set; } public string avatarUrl { get; set; } public string unionId { get; set; } public data watermark { get; set; } } /// <summary> /// 微信用户数据水印 /// </summary> public class data { public string appid { get; set; } public string timestamp { get; set; } } /// <summary> /// 微信小程序验证返回结果 /// </summary> public class result { public string openid { get; set; } public string session_key { get; set; } public string errcode { get; set; } public string errmsg { get; set; } } #endregion
|
请发表评论