using System; using System.Security.Cryptography; using System.Text; using System.Text.Json.Serialization; using Furion.JsonSerialization; namespace Myshipping.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) { // 16进制转换成byte var encryptedData = Convert.FromBase64String(encryptedDataStr); 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; } } /// /// 小程序登录解密后的用户信息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; } }