15 changed files with 533 additions and 264 deletions
File diff suppressed because it is too large
@ -1,22 +1,20 @@ |
|||
using System.Text; |
|||
using LINGYUN.Abp.Encryption.SM4; |
|||
using System.Text; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Security; |
|||
using Volo.Abp.Security.Encryption; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.Console |
|||
namespace LINGYUN.Abp.Encryption.Console; |
|||
|
|||
[DependsOn(typeof(AbpEncryptionSM4Module))] |
|||
public class AbpEncryptionConsoleModule : AbpModule |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpSecurityModule))] |
|||
public class AbpEncryptionConsoleModule : AbpModule |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
Configure<AbpStringEncryptionOptions>(options => |
|||
{ |
|||
Configure<AbpStringEncryptionOptions>(options => |
|||
{ |
|||
options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; |
|||
options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); |
|||
options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); |
|||
}); |
|||
} |
|||
options.DefaultPassPhrase = "s46c5q55nxpeS8Ra"; |
|||
options.InitVectorBytes = Encoding.ASCII.GetBytes("s83ng0abvd02js84"); |
|||
options.DefaultSalt = Encoding.ASCII.GetBytes("sf&5)s3#"); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,3 @@ |
|||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> |
|||
<ConfigureAwait ContinueOnCapturedContext="false" /> |
|||
</Weavers> |
|||
@ -0,0 +1,30 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
|||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> |
|||
<xs:element name="Weavers"> |
|||
<xs:complexType> |
|||
<xs:all> |
|||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> |
|||
<xs:complexType> |
|||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:all> |
|||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> |
|||
<xs:annotation> |
|||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
<xs:attribute name="GenerateXsd" type="xs:boolean"> |
|||
<xs:annotation> |
|||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> |
|||
</xs:annotation> |
|||
</xs:attribute> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\..\configureawait.props" /> |
|||
<Import Project="..\..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks> |
|||
<AssemblyName>LINGYUN.Abp.Encryption.SM4</AssemblyName> |
|||
<PackageId>LINGYUN.Abp.Encryption.SM4</PackageId> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Volo.Abp.Security" /> |
|||
<PackageReference Include="BouncyCastle.Cryptography" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,10 @@ |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Security; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.SM4; |
|||
|
|||
[DependsOn(typeof(AbpSecurityModule))] |
|||
public class AbpEncryptionSM4Module : AbpModule |
|||
{ |
|||
|
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Org.BouncyCastle.Crypto.Engines; |
|||
using Org.BouncyCastle.Crypto.Modes; |
|||
using Org.BouncyCastle.Crypto.Paddings; |
|||
using Org.BouncyCastle.Crypto.Parameters; |
|||
using System; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Security.Encryption; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.SM4; |
|||
|
|||
[Dependency(ServiceLifetime.Transient, ReplaceServices = true)] |
|||
[ExposeServices(typeof(StringEncryptionService), typeof(IStringEncryptionService))] |
|||
public class SM4StringEncryptionService : StringEncryptionService |
|||
{ |
|||
public SM4StringEncryptionService( |
|||
IOptions<AbpStringEncryptionOptions> options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
public override string Decrypt(string cipherText, string passPhrase = null, byte[] salt = null) |
|||
{ |
|||
if (string.IsNullOrEmpty(cipherText)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
passPhrase ??= Options.DefaultPassPhrase; |
|||
salt ??= Options.DefaultSalt; |
|||
|
|||
var cipherTextBytes = Convert.FromBase64String(cipherText); |
|||
|
|||
using var password = new Rfc2898DeriveBytes(passPhrase, salt); |
|||
// 128-bit key
|
|||
var keyBytes = password.GetBytes(16); |
|||
var ivBytes = password.GetBytes(16); |
|||
|
|||
var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new SM4Engine()), new Pkcs7Padding()); |
|||
cipher.Init(false, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); |
|||
|
|||
var decryptTextBytes = cipher.DoFinal(cipherTextBytes); |
|||
|
|||
return Encoding.UTF8.GetString(decryptTextBytes); |
|||
} |
|||
|
|||
public override string Encrypt(string plainText, string passPhrase = null, byte[] salt = null) |
|||
{ |
|||
if (plainText == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
passPhrase ??= Options.DefaultPassPhrase; |
|||
salt ??= Options.DefaultSalt; |
|||
|
|||
var plainTextBytes = Encoding.UTF8.GetBytes(plainText); |
|||
using var password = new Rfc2898DeriveBytes(passPhrase, salt); |
|||
// 128-bit key
|
|||
var keyBytes = password.GetBytes(16); |
|||
var ivBytes = password.GetBytes(16); |
|||
|
|||
var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new SM4Engine()), new Pkcs7Padding()); |
|||
cipher.Init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes)); |
|||
|
|||
var decryptTextBytes = cipher.DoFinal(plainTextBytes); |
|||
|
|||
return Convert.ToBase64String(decryptTextBytes); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
# LINGYUN.Abp.Encryption.SM4 |
|||
|
|||
数据加密模块,采用国密SM4算法,使用 **AbpStringEncryptionOptions** 配置无缝切换(密钥长度固定为128位以符合算法要求) |
|||
|
|||
## 配置使用 |
|||
|
|||
|
|||
```csharp |
|||
[DependsOn(typeof(AbpEncryptionSM4Module))] |
|||
public class YouProjectModule : AbpModule |
|||
{ |
|||
// other |
|||
} |
|||
``` |
|||
@ -0,0 +1,25 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net8.0</TargetFramework> |
|||
<RootNamespace /> |
|||
<IsPackable>false</IsPackable> |
|||
<Configurations>Debug;Release</Configurations> |
|||
<Platforms>AnyCPU</Platforms> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> |
|||
<PackageReference Include="xunit" /> |
|||
<PackageReference Include="xunit.runner.visualstudio"> |
|||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
|||
<PrivateAssets>all</PrivateAssets> |
|||
</PackageReference> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\framework\security\LINGYUN.Abp.Encryption.SM4\LINGYUN.Abp.Encryption.SM4.csproj" /> |
|||
<ProjectReference Include="..\LINGYUN.Abp.TestBase\LINGYUN.Abp.TestsBase.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,8 @@ |
|||
using LINGYUN.Abp.Tests; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.SM4; |
|||
|
|||
public abstract class AbpEncryptionSM4TestBase : AbpTestsBase<AbpEncryptionSM4TestModule> |
|||
{ |
|||
|
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.SM4; |
|||
|
|||
[DependsOn(typeof(AbpEncryptionSM4Module))] |
|||
public class AbpEncryptionSM4TestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using Shouldly; |
|||
using Volo.Abp.Security.Encryption; |
|||
using Xunit; |
|||
|
|||
namespace LINGYUN.Abp.Encryption.SM4; |
|||
public class StringEncryptionService_Tests : AbpEncryptionSM4TestBase |
|||
{ |
|||
private readonly IStringEncryptionService _stringEncryptionService; |
|||
|
|||
public StringEncryptionService_Tests() |
|||
{ |
|||
_stringEncryptionService = GetRequiredService<IStringEncryptionService>(); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("")] |
|||
[InlineData("This is a plain text!")] |
|||
public void Should_Enrypt_And_Decrpyt_With_Default_Options(string plainText) |
|||
{ |
|||
var encryptedText = _stringEncryptionService.Encrypt(plainText); |
|||
|
|||
var decryptedText = _stringEncryptionService.Decrypt(encryptedText); |
|||
|
|||
decryptedText.ShouldBe(plainText); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue