Azrng.Security
2.0.0
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
dotnet add package Azrng.Security --version 2.0.0
NuGet\Install-Package Azrng.Security -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Azrng.Security" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Azrng.Security" Version="2.0.0" />
<PackageReference Include="Azrng.Security" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Azrng.Security --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Azrng.Security, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Azrng.Security@2.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Azrng.Security&version=2.0.0
#tool nuget:?package=Azrng.Security&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Azrng.Security
Azrng.Security 是一个常用加密工具库,覆盖国际通用算法与国密算法,提供统一的编码输入输出(Base64/Hex)。
- 对称加密:AES、AES-GCM、DES、3DES、SM4
- 非对称加密:RSA、SM2
- 哈希与消息认证:MD5、SHA1/SHA256/SHA512、SM3、HMAC
- 目标框架:
netstandard2.1; net6.0; net7.0; net8.0; net9.0; net10.0
安装
Install-Package Azrng.Security
dotnet add package Azrng.Security
快速开始
1) AES-GCM(推荐,具备完整性校验)
using Azrng.Security;
using Azrng.Security.Enums;
var plain = "hello aes-gcm";
var (key, _) = AesHelper.ExportSecretAndIv(256, OutType.Base64);
// 推荐使用命名参数,明确调用 GCM 重载
var cipher = AesGcmHelper.Encrypt(
plainText: plain,
secretKey: key,
secretType: SecretType.Base64,
outType: OutType.Base64);
var restored = AesGcmHelper.Decrypt(
cipherCombined: cipher,
secretKey: key,
secretType: SecretType.Base64,
cipherTextType: OutType.Base64);
2) AES-CBC(兼容场景)
using Azrng.Security;
using Azrng.Security.Enums;
var plain = "hello aes-cbc";
var (key, iv) = AesHelper.ExportSecretAndIv(256, OutType.Base64);
// 安全默认包装:CBC + PKCS7
var cipher = AesHelper.EncryptCbcPkcs7(plain, key, iv);
var restored = AesHelper.DecryptCbcPkcs7(cipher, key, iv);
3) RSA(推荐 OAEP-SHA256 + PSS)
using Azrng.Security;
using Azrng.Security.Enums;
using System.Security.Cryptography;
var source = "hello rsa";
var (publicKey, privateKey) = RsaHelper.ExportBase64RsaKey();
// 加解密(推荐 OAEP-SHA256)
var cipher = RsaHelper.EncryptOaepSha256(source, publicKey, keyType: RSAKeyType.PEM);
var restored = RsaHelper.DecryptOaepSha256(cipher, privateKey, keyType: RSAKeyType.PEM, privateKeyFormat: RsaKeyFormat.PKCS8);
// 签名验签(推荐 PSS)
var sign = RsaHelper.SignDataPss(source, privateKey, HashAlgorithmName.SHA256, privateKeyType: OutType.Base64);
var ok = RsaHelper.VerifyDataPss(source, sign, publicKey, HashAlgorithmName.SHA256, publicKeyType: OutType.Base64);
详细用法
哈希与 HMAC
using Azrng.Security;
using Azrng.Security.Enums;
var md5 = Md5Helper.GetMd5Hash("abc");
var md5_16 = Md5Helper.GetMd5Hash("abc", is16: true);
var fileMd5 = Md5Helper.GetFileMd5Hash("appsettings.json");
var sha256 = ShaHelper.GetSha256Hash("abc");
var sha512 = ShaHelper.GetSha512Hash("abc");
var fileSha256 = ShaHelper.GetFileSha256Hash("appsettings.json", OutType.Hex);
var fileSha512 = ShaHelper.GetFileSha512Hash("appsettings.json", OutType.Hex);
var hmac = ShaHelper.GetHmacSha256Hash("payload", "secret", OutType.Base64);
var verified = ShaHelper.VerifyHmacSha256Hash("payload", "secret", hmac, OutType.Base64);
AES-GCM 分段输出
适合将 Cipher/Nonce/Tag 分开存储或跨系统传输。
using Azrng.Security;
using Azrng.Security.Enums;
var (key, _) = AesHelper.ExportSecretAndIv(256, OutType.Base64);
var (cipher, nonce, tag) = AesGcmHelper.EncryptToParts("hello", key, outType: OutType.Base64);
var plain = AesGcmHelper.DecryptFromParts(cipher, nonce, tag, key, cipherTextType: OutType.Base64);
RSA 兼容接口(历史系统)
using Azrng.Security;
using Azrng.Security.Enums;
using System.Security.Cryptography;
var (pub, pri) = RsaHelper.ExportPemRsaKey(RsaKeyFormat.PKCS1);
// 兼容模式:PKCS#1 v1.5
var cipher = RsaHelper.Encrypt("legacy-data", pub);
var plain = RsaHelper.Decrypt(cipher, pri, privateKeyFormat: RsaKeyFormat.PKCS1);
var sign = RsaHelper.SignData("legacy-data", pri, HashAlgorithmName.SHA256);
var ok = RsaHelper.VerifyData("legacy-data", sign, pub, HashAlgorithmName.SHA256);
SM 系列
using Azrng.Security;
using Azrng.Security.Enums;
// SM3
var sm3 = Sm3Helper.GetSm3Hash("hello");
// SM4
var key = "1234567890123456";
var iv = "1234567890123456";
var cipherSm4 = Sm4Helper.Encrypt("hello", key, Sm4CryptoEnum.CBC, outType: OutType.Base64, iv: iv);
var plainSm4 = Sm4Helper.Decrypt(cipherSm4, key, Sm4CryptoEnum.CBC, inputType: OutType.Base64, iv: iv);
// SM2
var (publicKey, privateKey) = Sm2Helper.ExportKey(OutType.Hex);
var cipherSm2 = Sm2Helper.Encrypt("hello", publicKey, publicKeyType: OutType.Hex, outType: OutType.Hex);
var plainSm2 = Sm2Helper.Decrypt(cipherSm2, privateKey, privateKeyType: OutType.Hex, inputType: OutType.Hex);
常用枚举说明
OutTypeBase64:Base64 编码字符串Hex:十六进制字符串
SecretTypeText:原始文本密钥(UTF8)Base64:Base64 编码密钥Hex:十六进制密钥
安全建议
- 新项目优先:
- 对称加密:
AesGcmHelper(GCM) - 非对称加密:
EncryptOaepSha256/DecryptOaepSha256 - 数字签名:
SignDataPss/VerifyDataPss
- 对称加密:
- DES/3DES 仅建议兼容历史系统,不建议新系统使用。
- 固定密钥、固定 IV、明文落库都会显著降低安全性。
兼容与迁移
AesGcmHelper中历史非 GCM 重载(Encrypt/Decrypt带CipherMode/PaddingMode)已标记[Obsolete]。- 迁移建议:
- 历史 AES 调用迁移到
AesHelper.Encrypt/Decrypt或AesHelper.EncryptCbcPkcs7/DecryptCbcPkcs7 - 新业务优先使用
AesGcmHelper的 GCM 重载
- 历史 AES 调用迁移到
主要 API 一览
Md5HelperGetMd5HashGetHmacMd5HashGetFileMd5Hash
ShaHelperGetSha1HashGetSha256HashGetSha512HashGetHmacSha1HashGetHmacSha256HashGetHmacSha512HashVerifyHmacSha1HashVerifyHmacSha256HashVerifyHmacSha512HashGetFileSha256HashGetFileSha512Hash
AesHelperExportSecretAndIvEncryptCbcPkcs7DecryptCbcPkcs7EncryptDecrypt
AesGcmHelperEncryptDecryptEncryptToPartsDecryptFromParts
RsaHelperExportBase64RsaKeyExportPemRsaKeyExportXmlRsaKeyEncryptOaepSha256DecryptOaepSha256SignDataPssVerifyDataPssQuickSignQuickVerifyQuickSignPssQuickVerifyPss
Sm2HelperSm3HelperSm4HelperDesHelperDes3HelperMurmurHashHelper
版本记录
更新记录
- 2.0.0
- 合并 Common.SecurityCrypto,包与命名空间统一为 Azrng.Security(破坏性变更:命名空间由 Common.Security 变更为 Azrng.Security)
- 1.2.1
- 更新加密方法
- 1.2.0
- 适配.Net10
- 1.1.2
- 更新与十六进制互转操作
- 1.1.1
- 将扩展方法改为静态方法
- 1.1.0
- 升级依赖包
- 补充文档
- 1.0.0
- 升级依赖包
- 0.0.1-beta7
- 对AES加密算法优化
- 0.0.1-beta6
- 替换依赖包BouncyCastle.NetCore为BouncyCastle.Cryptography,且将里面的一些源码操作改为使用包的方法,性能更好
- 0.0.1-beta5
- 增加SHA的HMAC算法
- 0.0.1-beta4
- 增加sm3、rsa等示例,增加单元测试
- 0.0.1-beta3
- 支持MD5、SHA等、DES、AES、RSA等
- 0.0.1-beta2
- fix处理md5加密将16位和32位弄混问题
- 0.0.1-beta1
- 从common里面移出来一些方法
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- BouncyCastle.Cryptography (>= 2.6.2)
- System.Text.Json (>= 6.0.0)
-
net10.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net6.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net7.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net8.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net9.0
- BouncyCastle.Cryptography (>= 2.6.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0 | 78 | 6/20/2026 |