移动加密方法,测试值转换器ok

This commit is contained in:
2024-09-23 18:01:46 +08:00
parent 91fa8e75e1
commit d7a76bc110
9 changed files with 124 additions and 19 deletions
@@ -1,87 +0,0 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using System.Text;
namespace IRaCIS.Core.Application.BusinessFilter;
public class AesEncryption
{
// AES 加密(不带 IV
public static string Encrypt(string plainText, string key)
{
var keyBytes = Encoding.UTF8.GetBytes(key);
// 使用 AES 引擎 + PKCS7 填充
var engine = new AesEngine();
var blockCipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding());
blockCipher.Init(true, new KeyParameter(keyBytes)); // true 表示加密
var inputBytes = Encoding.UTF8.GetBytes(plainText);
var encryptedBytes = ProcessCipher(blockCipher, inputBytes);
// 返回 Base64 编码的加密字符串
return Convert.ToBase64String(encryptedBytes);
}
// AES 解密(不带 IV
public static string Decrypt(string encryptedText, string key)
{
var keyBytes = Encoding.UTF8.GetBytes(key);
var cipherBytes = Convert.FromBase64String(encryptedText);
// 使用 AES 引擎 + PKCS7 填充
var engine = new AesEngine();
var blockCipher = new PaddedBufferedBlockCipher(engine, new Pkcs7Padding());
blockCipher.Init(false, new KeyParameter(keyBytes)); // false 表示解密
var decryptedBytes = ProcessCipher(blockCipher, cipherBytes);
return Encoding.UTF8.GetString(decryptedBytes);
}
// AES 加密(带 IV
public static string Encrypt(string plainText, string key, string iv)
{
var keyBytes = Encoding.UTF8.GetBytes(key);
var ivBytes = Encoding.UTF8.GetBytes(iv);
// 使用 AES 引擎 + PKCS7 填充 + CBC 模式
var engine = new AesEngine();
var blockCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding());
blockCipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); // true 表示加密
var inputBytes = Encoding.UTF8.GetBytes(plainText);
var encryptedBytes = ProcessCipher(blockCipher, inputBytes);
// 返回 Base64 编码的加密字符串
return Convert.ToBase64String(encryptedBytes);
}
// 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 cipherBytes = Convert.FromBase64String(encryptedText);
// 使用 AES 引擎 + PKCS7 填充 + CBC 模式
var engine = new AesEngine();
var blockCipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding());
blockCipher.Init(false, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); // false 表示解密
var decryptedBytes = ProcessCipher(blockCipher, cipherBytes);
return Encoding.UTF8.GetString(decryptedBytes);
}
// 处理加密/解密数据
private static byte[] ProcessCipher(IBufferedCipher cipher, byte[] input)
{
var output = new byte[cipher.GetOutputSize(input.Length)];
int length = cipher.ProcessBytes(input, 0, input.Length, output, 0);
length += cipher.DoFinal(output, length);
Array.Resize(ref output, length); // 调整输出数组大小以适应实际数据长度
return output;
}
}
@@ -1,4 +1,5 @@
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Encryption;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
@@ -1,92 +0,0 @@
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;
using System.Text;
namespace IRaCIS.Core.Application.BusinessFilter;
/// <summary>
/// https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
/// </summary>
public class RSAEncryption
{
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();
}
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="privateKey">私钥</param>
/// <param name="decryptstring">待解密的字符串(Base64)</param>
/// <returns>解密后的字符串</returns>
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);
}
}/// <summary>
/// 加密
/// </summary>
/// <param name="publicKey">公钥</param>
/// <param name="encryptstring">待加密的字符串</param>
/// <returns>加密后的Base64</returns>
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);
}
}
}
@@ -47,7 +47,6 @@
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.400.16" />
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.14.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.402.7" />
<PackageReference Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageReference Include="DocX" Version="3.0.1" />
<PackageReference Include="FreeSpire.Doc" Version="12.2.0" />
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
@@ -12406,20 +12406,6 @@
测试加密API 返回的结果
</summary>
</member>
<member name="T:IRaCIS.Core.Application.BusinessFilter.RSAEncryption">
<summary>
https://www.cnblogs.com/NBDWDYS2214143926/p/13329231.html
</summary>
</member>
<member name="M:IRaCIS.Core.Application.BusinessFilter.RSAEncryption.Decrypt(System.String,System.String)">
<summary>
RSA解密
</summary>
<param name="privateKey">私钥</param>
<param name="decryptstring">待解密的字符串(Base64)</param>
<returns>解密后的字符串</returns>
</member>
<!-- Badly formed XML comment ignored for member "M:IRaCIS.Core.Application.BusinessFilter.RSAEncryption.Encrypt(System.String,System.String)" -->
<member name="T:IRaCIS.Core.Application.BusinessFilter.GlobalExceptionHandler">
<summary>
不生效,不知道为啥
+15 -1
View File
@@ -6,6 +6,7 @@ using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure;
using IRaCIS.Core.Infrastructure.Encryption;
using IRaCIS.Core.Infrastructure.NewtonsoftJson;
using MassTransit;
using Medallion.Threading;
@@ -104,6 +105,19 @@ namespace IRaCIS.Core.Application.Service
public string TestName { get; set; }
}
public async Task<IResponseOutput> TestAutoEncretpt([FromServices] IRepository<TestLength> _testLengthRepository)
{
await _testLengthRepository.AddAsync(new TestLength() { Name = "zhouhang1" });
await _testLengthRepository.AddAsync(new TestLength() { Name = "hewentao" });
await _testLengthRepository.SaveChangesAsync();
var list = _testLengthRepository.Where().ToList();
var exist = await _testLengthRepository.AnyAsync(t => t.Name == "zhouhang1");
return ResponseOutput.Ok(list, exist);
}
public async Task<IResponseOutput> TestJson()
{
@@ -280,7 +294,7 @@ namespace IRaCIS.Core.Application.Service
var encreptMd5 = AesEncryption.Encrypt(MD5Helper.Md5("123456"), key);
Console.WriteLine(encreptMd5);
var decrept= AesEncryption.Decrypt(encreptMd5, key);
var decrept = AesEncryption.Decrypt(encreptMd5, key);
Console.WriteLine();