From 99ef28be99f72b36eea32a31b5f07f7df6161271 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 5 Aug 2024 14:41:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=96=B9=E6=B3=95=E5=87=86?= =?UTF-8?q?=E5=A4=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Application/Helper/RSAHelper.cs | 97 ++++++++++++++++ .../Service/Allocation/VisitTaskService.cs | 1 - .../Service/QC/QCListService.cs | 1 - .../Visit/DTO/ClinicalStudySubjects.cs | 1 - IRaCIS.Core.Application/TestService.cs | 104 ++++-------------- 5 files changed, 119 insertions(+), 85 deletions(-) create mode 100644 IRaCIS.Core.Application/Helper/RSAHelper.cs diff --git a/IRaCIS.Core.Application/Helper/RSAHelper.cs b/IRaCIS.Core.Application/Helper/RSAHelper.cs new file mode 100644 index 000000000..fb5309691 --- /dev/null +++ b/IRaCIS.Core.Application/Helper/RSAHelper.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Encodings; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.OpenSsl; +using Org.BouncyCastle.Security; + +namespace IRaCIS.Core.Application.Helper +{ + /// + /// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html + /// + public class RSAHelper + { + + public static AsymmetricCipherKeyPair GenerateRSAKeyPair(int keySize) + { + var keyGenerationParameters = new KeyGenerationParameters(new SecureRandom(), keySize); + var keyPairGenerator = new RsaKeyPairGenerator(); + keyPairGenerator.Init(keyGenerationParameters); + return keyPairGenerator.GenerateKeyPair(); + } + + public static string ExportPublicKey(AsymmetricKeyParameter publicKey) + { + using (StringWriter sw = new StringWriter()) + { + PemWriter pw = new PemWriter(sw); + pw.WriteObject(publicKey); + pw.Writer.Flush(); + return sw.ToString(); + } + } + + public static string ExportPrivateKey(AsymmetricKeyParameter privateKey) + { + using (StringWriter sw = new StringWriter()) + { + PemWriter pw = new PemWriter(sw); + pw.WriteObject(privateKey); + pw.Writer.Flush(); + return sw.ToString(); + } + } + + /// + /// RSA解密 + /// + /// 私钥 + /// 待解密的字符串(Base64) + /// 解密后的字符串 + public static string Decrypt(string privateKey, string decryptstring) + { + using (TextReader reader = new StringReader(privateKey)) + { + dynamic key = new PemReader(reader).ReadObject(); + var rsaDecrypt = new Pkcs1Encoding(new RsaEngine()); + if (key is AsymmetricKeyParameter) + { + key = (AsymmetricKeyParameter)key; + } + else if (key is AsymmetricCipherKeyPair) + { + key = ((AsymmetricCipherKeyPair)key).Private; + } + rsaDecrypt.Init(false, key); //这里加密是true;解密是false + + byte[] entData = Convert.FromBase64String(decryptstring); + entData = rsaDecrypt.ProcessBlock(entData, 0, entData.Length); + return Encoding.UTF8.GetString(entData); + } + }/// + + /// 加密 + /// + /// 公钥 + /// 待加密的字符串 + /// 加密后的Base64 + public static string Encrypt(string publicKey, string encryptstring) + { + using (TextReader reader = new StringReader(publicKey)) + { + AsymmetricKeyParameter key = new PemReader(reader).ReadObject() as AsymmetricKeyParameter; + Pkcs1Encoding pkcs1 = new Pkcs1Encoding(new RsaEngine()); + pkcs1.Init(true, key);//加密是true;解密是false; + byte[] entData = Encoding.UTF8.GetBytes(encryptstring); + entData = pkcs1.ProcessBlock(entData, 0, entData.Length); + return Convert.ToBase64String(entData); + } + } + } +} diff --git a/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs b/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs index b030d49ec..efdd7ff84 100644 --- a/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs +++ b/IRaCIS.Core.Application/Service/Allocation/VisitTaskService.cs @@ -19,7 +19,6 @@ using DocumentFormat.OpenXml.Office2010.Word; using System.Linq.Dynamic.Core; using System.Linq; using DocumentFormat.OpenXml.Bibliography; -using Org.BouncyCastle.Crypto; using IRaCIS.Core.Domain.Share.Reading; using MassTransit; using System.Reactive.Subjects; diff --git a/IRaCIS.Core.Application/Service/QC/QCListService.cs b/IRaCIS.Core.Application/Service/QC/QCListService.cs index dd1e1d662..bd065b33e 100644 --- a/IRaCIS.Core.Application/Service/QC/QCListService.cs +++ b/IRaCIS.Core.Application/Service/QC/QCListService.cs @@ -8,7 +8,6 @@ using IRaCIS.Application.Interfaces; using IRaCIS.Application.Contracts; using IRaCIS.Core.Application.ViewModel; using IRaCIS.Core.Application.Service.Reading.Dto; -using static Org.BouncyCastle.Math.EC.ECCurve; using IRaCIS.Core.Infrastructure; namespace IRaCIS.Core.Application.Image.QA diff --git a/IRaCIS.Core.Application/Service/Visit/DTO/ClinicalStudySubjects.cs b/IRaCIS.Core.Application/Service/Visit/DTO/ClinicalStudySubjects.cs index fc1155c32..bd0178751 100644 --- a/IRaCIS.Core.Application/Service/Visit/DTO/ClinicalStudySubjects.cs +++ b/IRaCIS.Core.Application/Service/Visit/DTO/ClinicalStudySubjects.cs @@ -2,7 +2,6 @@ using IRaCIS.Core.Infrastructure.Extention; using IRaCIS.Core.Domain.Share; using System.ComponentModel.DataAnnotations; -using Org.BouncyCastle.Bcpg.OpenPgp; namespace IRaCIS.Application.Contracts { diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 08fbc3a60..b0dd183e8 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -1,7 +1,4 @@ using Aliyun.OSS; -using BeetleX; -using BeetleX.BNR; -using Castle.DynamicProxy.Generators.Emitters.SimpleAST; using IP2Region.Net.XDB; using IRaCIS.Application.Contracts; using IRaCIS.Core.Application.Contracts; @@ -21,14 +18,10 @@ using Microsoft.Extensions.Options; using MiniExcelLibs; using Minio; using Minio.DataModel.Args; -using NPOI.HPSF; -using NPOI.POIFS.Crypt; using SharpCompress.Common; using Spire.Doc; using System.Linq.Expressions; using System.Reflection.Metadata; -using System.Security.AccessControl; -using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; @@ -177,83 +170,30 @@ namespace IRaCIS.Application.Services [UnitOfWork] public async Task Get() { + // Generate RSA keys + var keyPair = RSAHelper.GenerateRSAKeyPair(2048); - //Expression> visitTaskLambda = x => x.TrialId == Guid.Empty && x.SubjectId == Guid.Empty && x.TrialReadingCriterionId == Guid.Empty; + // Export the public and private keys to PEM format + string publicKey = RSAHelper.ExportPublicKey(keyPair.Public); + string privateKey = RSAHelper.ExportPrivateKey(keyPair.Private); - //var visitTaskIdQueryable = _visitTaskRepositoryy.Where(visitTaskLambda).Where(t => t.Subject.SubjectVisitTaskList.AsQueryable().Where(visitTaskLambda).Any(c => c.IsNeedClinicalDataSign == true && c.IsClinicalDataSign == false && c.VisitTaskNum < t.VisitTaskNum)).Select(t => t.Id); + Console.WriteLine("Public Key:"); + Console.WriteLine(publicKey); + Console.WriteLine("\nPrivate Key:"); + Console.WriteLine(privateKey); - //await _visitTaskRepositoryy.BatchUpdateNoTrackingAsync(t => visitTaskIdQueryable.Contains(t.Id), u => new VisitTask() - //{ - // IsFrontTaskNeedSignButNotSign = true - //}); + // Data to encrypt + string dataToEncrypt = "Hello, RSA!"; + Console.WriteLine("\nOriginal Data: " + dataToEncrypt); + // Encrypt the data + var encryptedData = RSAHelper.Encrypt( publicKey, dataToEncrypt); + Console.WriteLine("\nEncrypted Data: " + encryptedData); - //var a = ((Decimal)1.00).ToString().TrimEnd(new char[] { '.', '0' }); - //var b = ((Decimal)1.01).ToString().TrimEnd(new char[] { '.', '0' }); - //var c = ((Decimal)100).ToString().TrimEnd(new char[] { '.', '0' }); - //var subject1 = Guid.Parse("431D0C58-ABC5-4166-B9BC-08DA0E391693"); - //var subject2 = Guid.Parse("431D0C58-ABC5-4166-B9BC-08DA0E391694"); + // Decrypt the data + string decryptedData = RSAHelper.Decrypt( privateKey, encryptedData); + Console.WriteLine("\nDecrypted Data: " + decryptedData); - // var subjectList = new List() { Guid.Parse("431D0C58-ABC5-4166-B9BC-08DA0E391693") , - // Guid.Parse("431D0C58-ABC5-4166-B9BC-08DA0E391694") , - // Guid.Parse("431D0C58-ABC5-4166-B9BC-08DA0E391695") - // }; - - //string[] citys = new string[] { "广州", "深圳", "上海", "北京" }; - //foreach (var item in subjectList) - //{ - // Console.WriteLine(await BNRFactory.Default.Create($"[CN:{item}][N:[CN:{item}]/0000000]")); - //} - //foreach (var item in subjectList) - //{ - // Console.WriteLine(await BNRFactory.Default.Create($"[N:[CN:{item}]/0000000]")); - //} - - //foreach (var item in subjectList) - //{ - // Console.WriteLine(await BNRFactory.Default.Create($"[CN:{item}][redis:city/0000000]")); - //} - - //var needAddVisitList = await _repository.Where(t => t.TrialId == Guid.Empty).DistinctBy(t => t.VisitTaskNum).ToListAsync(); - - - //await _repository.BatchUpdateAsync(t => t.Id == Guid.Empty, u => new VisitTask() - //{ - // SuggesteFinishedTime = u.IsUrgent ? DateTime.Now.AddDays(2) : DateTime.Now.AddDays(7), - - // Code = u.Code + 1 - //}); - - //var query = from item1 in _repository.Where() - // join item2 in _repository.Where() on item1.ValueType equals item2.ValueType - // select new - // { - // item1.ValueType, - // dd = item2.ValueType - // }; - - //var list2 = query.ToList(); - - //await Task.CompletedTask; - - //var list = await _repository.Where(t => t.TrialId == Guid.Parse("40400000-3e2c-0016-239b-08da581f0e74")).ToListAsync(); - - ////await _repository.BatchDeleteAsync(t => t.TrialId == Guid.Parse("40400000-3e2c-0016-239b-08da581f0e74")); - - //await _repository.AddRangeAsync(list, true); - - //await _repository.SaveChangesAsync(); - - //await _repository.BatchUpdateAsync(t => t.TrialId == Guid.Parse("40400000-3e2c-0016-239b-08da581f0e74") && t.EntityName== "ClinicalDataTrialSet", t => new DataInspection() { CreateTime= DateTime.Now.AddMonths(-2) } ); - - //await _visitTaskRepositoryy.UpdatePartialFromQueryAsync( Guid.Parse("78360000-3E2C-0016-9B53-08DA6A002040"), c => new VisitTask() { UpdateTime = DateTime.Now }); - - //await _visitTaskRepositoryy.UpdatePartialFromQueryAsync( Guid.Parse("78360000-3E2C-0016-9B53-08DA6A002040"), c => new VisitTask() { UpdateTime = DateTime.Now.AddMinutes(1) }); - - //var a = _userInfo.IsTestUser; - - //var list1 = await _repository.Where().Select(t => t.TranslateValue(t.Value, t.ValueCN, true)).ToListAsync(); - //var list2 = await _repository.Where().Select(t => t.TranslateValue(t.Value, t.ValueCN, false)).ToListAsync(); return "测试自动发布--再次提交"; } @@ -273,15 +213,15 @@ namespace IRaCIS.Application.Services [AllowAnonymous] public async Task GetEnvironmentName([FromServices] IWebHostEnvironment env) { - var a = IdentifierHelper.CreateGuid("123456"); + //var a = IdentifierHelper.CreateGuid("123456"); - var k = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes("123456")); + //var k = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes("123456")); - var c = MD5Helper.Md5("123456"); + //var c = MD5Helper.Md5("123456"); - return new { env.EnvironmentName, EMailConfig = _systemEmailConfig.CurrentValue, BasicConfig = _basicConfig.CurrentValue }; + //return new { env.EnvironmentName, EMailConfig = _systemEmailConfig.CurrentValue, BasicConfig = _basicConfig.CurrentValue }; // Load a document.