using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Text.Json.Serialization; using Furion.JsonSerialization; namespace GDZZ.Core; /// /// 小程序工具类 /// public class MiniProgramUtil { /// /// 解密数据获取小程序用户信息 /// /// 包括敏感数据在内的完整用户信息的加密数据 /// auth.code2Session获取的session_key /// 加密算法的初始向量 /// 如遇异常返回 "" public static TelUserInfoModel AESDecrypt(string encryptedDataStr, string session_key, string iv) { var rlt = BasicAESDecrypt(encryptedDataStr, session_key, iv); return !rlt.IsEmpty() ? JSON.Deserialize(rlt) : null; } /// /// 基础解析 /// /// 包括敏感数据在内的完整用户信息的加密数据 /// auth.code2Session获取的session_key /// 加密算法的初始向量 /// private static string BasicAESDecrypt(string encryptedDataStr, string session_key, string iv) { var databyte = System.Text.Encoding.Default.GetBytes(encryptedDataStr); // 16进制转换成byte var encryptedData = Convert.FromBase64String(encryptedDataStr.Replace(" ","")); var managed = new RijndaelManaged { Key = Convert.FromBase64String(session_key), IV = Convert.FromBase64String(iv), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }; var transform = managed.CreateDecryptor(); var finalBlock = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); var result = Encoding.Default.GetString(finalBlock); return result; } public static byte[] GetByteArray(string shex) { string[] ssArray = shex.Split(' '); List bytList = new List(); foreach (var s in ssArray) { //将十六进制的字符串转换成数值 bytList.Add(Convert.ToByte(s, 16)); } //返回字节数组 return bytList.ToArray(); } } /// /// 小程序登录解密后的用户信息Model /// public class TelUserInfoModel { /// /// 手机号 /// [JsonPropertyName("phoneNumber")] public string PhoneNumber { get; set; } /// /// 纯手机号 /// [JsonPropertyName("purePhoneNumber")] public string PurePhoneNumber { get; set; } /// /// 区号 /// [JsonPropertyName("countryCode")] public string CountryCode { get; set; } }