From 57b3866d2b06472fa47da7367ec41002d063b6fa Mon Sep 17 00:00:00 2001 From: wtp <1813748440@qq.com> Date: Fri, 4 Aug 2023 10:31:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E6=89=8B?= =?UTF-8?q?=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/Services/UserService.cs | 48 +++++----- src/Znyc.Dispatching.Core/Util/Extensions.cs | 47 ++++++++++ .../Util/MiniProgramUtil.cs | 93 +++++++++++++++++++ .../Znyc.Dispatching.Core.xml | 69 ++++++++++++++ 4 files changed, 234 insertions(+), 23 deletions(-) create mode 100644 src/Znyc.Dispatching.Core/Util/Extensions.cs create mode 100644 src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs diff --git a/src/Znyc.Dispatching.Application/User/Services/UserService.cs b/src/Znyc.Dispatching.Application/User/Services/UserService.cs index 87a11d2..d00b0bb 100644 --- a/src/Znyc.Dispatching.Application/User/Services/UserService.cs +++ b/src/Znyc.Dispatching.Application/User/Services/UserService.cs @@ -20,6 +20,7 @@ using Znyc.Dispatching.Application.Dto.Input; using Znyc.Dispatching.Core; using Znyc.Dispatching.Core.Entitys; using Znyc.Dispatching.Core.Extension; +using Znyc.Dispatching.Core.Util; using Znyc.Dispatching.WeChat.Core.CommonService.TemplateMessage.WxOpen; namespace Znyc.Dispatching.Application @@ -195,30 +196,31 @@ namespace Znyc.Dispatching.Application var key = Convert.FromBase64String(jsCode2JsonResult.session_key);//第一步获取到的session_key var iv = Convert.FromBase64String(input.DecryptPhoneAddInput.Iv); - Console.WriteLine("encryptedData:" + encryptedData); - Console.WriteLine("key:" + key); - Console.WriteLine("iv:" + iv); - - var decryptor = rijndael.CreateDecryptor(key, iv); - Console.WriteLine("decryptor:" + decryptor); - using (var msDecrypt = new MemoryStream(encryptedData)) - { - using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) - { - Console.WriteLine("csDecrypt:"+ csDecrypt); - using (var srDecrypt = new StreamReader(csDecrypt)) - { - var plaintext = srDecrypt.ReadToEnd(); - var json = Newtonsoft.Json.JsonConvert.DeserializeObject(plaintext); - Console.WriteLine("json:"+json); - var phoneNumber = json.phoneNumber; - var purePhoneNumber = json.purePhoneNumber; + var phoneInfo = MiniProgramUtil.AESDecrypt(input.DecryptPhoneAddInput.EncryptedData, jsCode2JsonResult.session_key, input.DecryptPhoneAddInput.Iv); + phone = phoneInfo.PurePhoneNumber; + Console.WriteLine(phoneInfo.PurePhoneNumber); + + //var decryptor = rijndael.CreateDecryptor(key, iv); + + //using (var msDecrypt = new MemoryStream(encryptedData)) + //{ + // using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) + // { + // Console.WriteLine("csDecrypt:"+ csDecrypt); + // using (var srDecrypt = new StreamReader(csDecrypt)) + // { + // var plaintext = srDecrypt.ReadToEnd(); + // Console.WriteLine("plaintext:"+ plaintext); + // var json = Newtonsoft.Json.JsonConvert.DeserializeObject(plaintext); + // Console.WriteLine("json:"+json); + // var phoneNumber = json.phoneNumber; + // var purePhoneNumber = json.purePhoneNumber; - //至此,成功获取到手机号 - phone = Convert.ToString(purePhoneNumber); - } - } - } + // //至此,成功获取到手机号 + // phone = Convert.ToString(purePhoneNumber); + // } + // } + //} } catch (Exception ex) { diff --git a/src/Znyc.Dispatching.Core/Util/Extensions.cs b/src/Znyc.Dispatching.Core/Util/Extensions.cs new file mode 100644 index 0000000..8bdfe0d --- /dev/null +++ b/src/Znyc.Dispatching.Core/Util/Extensions.cs @@ -0,0 +1,47 @@ + +using System; +using Microsoft.AspNetCore.Http; +namespace Znyc.Dispatching.Core.Util +{ + /// + /// 验证扩展类 + /// + public static partial class Extensions + { + /// + /// 检查 Object 是否为 NULL + /// + /// + /// + public static bool IsEmpty(this object value) + { + return value == null || string.IsNullOrEmpty(value.ToString()); + } + + /// + /// 检查 Object 是否为 NULL 或者 0 + /// + /// + /// + public static bool IsNullOrZero(this object value) + { + return value == null || value.ToString().Trim() == "0"; + } + + /// + /// 检查是否为 AJAX 请求 + /// + /// + /// + public static bool IsAjaxRequest(this HttpRequest request) + { + if (request == null) + throw new ArgumentNullException(nameof(request)); + + if (request.Headers != null) + return request.Headers["X-Requested-With"] == "XMLHttpRequest"; + return false; + } + } + +} diff --git a/src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs b/src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs new file mode 100644 index 0000000..a566e85 --- /dev/null +++ b/src/Znyc.Dispatching.Core/Util/MiniProgramUtil.cs @@ -0,0 +1,93 @@ +using Furion.JsonSerialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Znyc.Dispatching.Core.Util +{ + /// + /// 小程序工具类 + /// + 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; } + } +} diff --git a/src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml b/src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml index f610f8f..d35c249 100644 --- a/src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml +++ b/src/Znyc.Dispatching.Core/Znyc.Dispatching.Core.xml @@ -5343,6 +5343,75 @@ 保存地址 + + + 验证扩展类 + + + + + 检查 Object 是否为 NULL + + + + + + + 检查 Object 是否为 NULL 或者 0 + + + + + + + 检查是否为 AJAX 请求 + + + + + + + 小程序工具类 + + + + + 解密数据获取小程序用户信息 + + 包括敏感数据在内的完整用户信息的加密数据 + auth.code2Session获取的session_key + 加密算法的初始向量 + 如遇异常返回 "" + + + + 基础解析 + + 包括敏感数据在内的完整用户信息的加密数据 + auth.code2Session获取的session_key + 加密算法的初始向量 + + + + + 小程序登录解密后的用户信息Model + + + + + 手机号 + + + + + 纯手机号 + + + + + 区号 + + 阿里云oss文件上传工具类