From 456e6f30353bcf022813ec5e6cd3fc9c17fbd0d1 Mon Sep 17 00:00:00 2001
From: hang <872297557@qq.com>
Date: Thu, 28 Nov 2024 16:18:45 +0800
Subject: [PATCH] =?UTF-8?q?IRC-HIR=20=E6=8E=88=E6=9D=83=E7=A0=81=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../IRaCIS.Core.Application.xml | 16 ++
.../MinimalApiService/HIRActivateService.cs | 160 ++++++++++++++++++
.../_IRaCIS/Encryption/AesEncryption.cs | 16 +-
3 files changed, 188 insertions(+), 4 deletions(-)
create mode 100644 IRaCIS.Core.Application/Service/MinimalApiService/HIRActivateService.cs
diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
index b93a6470b..bd0927b45 100644
--- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
+++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml
@@ -2349,6 +2349,22 @@
+
+
+ 获取授权码明文信息
+
+
+
+
+
+
+
+ 获取项目激活码
+
+
+
+
+
测试客户端凭证代码
diff --git a/IRaCIS.Core.Application/Service/MinimalApiService/HIRActivateService.cs b/IRaCIS.Core.Application/Service/MinimalApiService/HIRActivateService.cs
new file mode 100644
index 000000000..c16e5041d
--- /dev/null
+++ b/IRaCIS.Core.Application/Service/MinimalApiService/HIRActivateService.cs
@@ -0,0 +1,160 @@
+using IRaCIS.Application.Contracts;
+using IRaCIS.Core.Infrastructure.Encryption;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace IRaCIS.Core.Application.Service;
+
+public class HIRActivateService : ServiceBase
+{
+
+ ///
+ /// 获取授权码明文信息
+ ///
+ ///
+ ///
+ ///
+ [AllowAnonymous]
+ public async Task GetAuthorizationCodeInfo(string authorizationCode, [FromServices] IOptionsMonitor _hospitalOption)
+ {
+ // 解密 Base64 编码后的数据
+ byte[] base64DecodedBytes = Convert.FromBase64String(authorizationCode);
+ string decodedText = System.Text.Encoding.UTF8.GetString(base64DecodedBytes);
+
+
+ var authInfo = JsonConvert.DeserializeObject(decodedText);
+
+ if (authInfo == null)
+ {
+ return ResponseOutput.NotOk("不能解析该项目授权码");
+ }
+
+ return ResponseOutput.Ok(decodedText);
+ }
+
+ ///
+ /// 获取项目激活码
+ ///
+ ///
+ ///
+ ///
+ [AllowAnonymous]
+ [RoutePattern(HttpMethod = "get")]
+ public async Task GetTrialActivationCode(string decodedText, [FromServices] IOptionsMonitor _basicSystemConfigConfig)
+ {
+
+ var authorizationInfo = JsonConvert.DeserializeObject(decodedText);
+
+ if (authorizationInfo != null)
+ {
+ //一周内激活
+ authorizationInfo.ActiveDeadLineDate = DateTime.Now.Date.AddDays(8).AddSeconds(-1);
+ var info = Cryptography.EncryptString($"{JsonConvert.SerializeObject(authorizationInfo)}", "HIR_System_AES_Key_Info", "Trial_AuthorizationEncrypt");
+
+ Console.WriteLine("HIR_System_AES_Key_Info".PadRight(32, '0').Substring(0, 32)+" "+ "Trial_AuthorizationEncrypt".PadRight(16, '0').Substring(0, 16));
+ var enCrept = AesEncryption.Encrypt($"{JsonConvert.SerializeObject(authorizationInfo)}", "HIR_System_AES_Key_Info", "Trial_AuthorizationEncrypt");
+
+ var dd= AesEncryption.Decrypt(enCrept, "HIR_System_AES_Key_Info", "Trial_AuthorizationEncrypt");
+
+ return ResponseOutput.Ok(info);
+ }
+ else
+ {
+ return ResponseOutput.NotOk("激活码解密文本信息有误");
+ }
+
+
+
+ }
+}
+
+public class TrialAuthorizationInfo
+{
+ public Guid TrialId { get; set; }
+ public Guid CreateUserId { get; set; }
+ public string TrialCode { get; set; }
+
+ public string HospitalName { get; set; }
+
+ public string HospitalCode { get; set; }
+
+ public int PurchaseDuration { get; set; }
+
+
+
+ public List CriterionTypeList { get; set; }
+
+ public DateTime? AuthorizationDeadLineDate { get; set; }
+
+ public DateTime? ActiveDeadLineDate { get; set; }
+
+ public DateTime? ActiveTime { get; set; }
+
+}
+
+public class Cryptography
+{
+ public static string EncryptString(string plainText, string key, string iv)
+ {
+ using (Aes aesAlg = Aes.Create())
+ {
+ aesAlg.Key = GetKeyBytes(key, aesAlg.KeySize / 8);
+ aesAlg.IV = GetKeyBytes(iv, 16);
+
+ ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
+
+ using (MemoryStream msEncrypt = new MemoryStream())
+ {
+ using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
+ {
+ byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
+ csEncrypt.Write(plainBytes, 0, plainBytes.Length);
+ csEncrypt.FlushFinalBlock();
+ }
+ return Convert.ToBase64String(msEncrypt.ToArray());
+ }
+ }
+ }
+
+ public static string DecryptString(string cipherText, string key, string iv)
+ {
+ byte[] cipherBytes = Convert.FromBase64String(cipherText);
+ using (Aes aesAlg = Aes.Create())
+ {
+ aesAlg.Key = GetKeyBytes(key, aesAlg.KeySize / 8);
+ aesAlg.IV = GetKeyBytes(iv, 16);
+
+ ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
+
+ using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
+ {
+ using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
+ {
+ using (StreamReader srDecrypt = new StreamReader(csDecrypt))
+ {
+ return srDecrypt.ReadToEnd();
+ }
+ }
+ }
+ }
+ }
+
+ private static byte[] GetKeyBytes(string key, int keySize)
+ {
+
+
+ using (var deriveBytes = new PasswordDeriveBytes(key, null))
+ {
+ return deriveBytes.GetBytes(keySize);
+ }
+ }
+}
diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Encryption/AesEncryption.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Encryption/AesEncryption.cs
index 884e8b29b..ed71d8b32 100644
--- a/IRaCIS.Core.Infrastructure/_IRaCIS/Encryption/AesEncryption.cs
+++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Encryption/AesEncryption.cs
@@ -44,10 +44,18 @@ public class AesEncryption
}
// AES 加密(带 IV)
+ ///
+ /// AES 密钥的长度必须是以下之一:128 位(16 字节)192 位(24 字节)256 位(32 字节)
+ /// IV must be 16 bytes
+ ///
+ ///
+ ///
+ ///
+ ///
public static string Encrypt(string plainText, string key, string iv)
{
- var keyBytes = Encoding.UTF8.GetBytes(key);
- var ivBytes = Encoding.UTF8.GetBytes(iv);
+ var keyBytes = Encoding.UTF8.GetBytes(key.PadRight(32, '0').Substring(0, 32));
+ var ivBytes = Encoding.UTF8.GetBytes(iv.PadRight(16, '0').Substring(0, 16));
// 使用 AES 引擎 + PKCS7 填充 + CBC 模式
var engine = new AesEngine();
@@ -64,8 +72,8 @@ public class AesEncryption
// AES 解密(带 IV)
public static string Decrypt(string encryptedText, string key, string iv)
{
- var keyBytes = Encoding.UTF8.GetBytes(key);
- var ivBytes = Encoding.UTF8.GetBytes(iv);
+ var keyBytes = Encoding.UTF8.GetBytes(key.PadRight(32, '0').Substring(0, 32));
+ var ivBytes = Encoding.UTF8.GetBytes(iv.PadRight(16, '0').Substring(0, 16));
var cipherBytes = Convert.FromBase64String(encryptedText);
// 使用 AES 引擎 + PKCS7 填充 + CBC 模式