Smart.Security
4.1.6
dotnet add package Smart.Security --version 4.1.6
NuGet\Install-Package Smart.Security -Version 4.1.6
<PackageReference Include="Smart.Security" Version="4.1.6" />
<PackageVersion Include="Smart.Security" Version="4.1.6" />
<PackageReference Include="Smart.Security" />
paket add Smart.Security --version 4.1.6
#r "nuget: Smart.Security, 4.1.6"
#:package Smart.Security@4.1.6
#addin nuget:?package=Smart.Security&version=4.1.6
#tool nuget:?package=Smart.Security&version=4.1.6
Smart.Security
<a name="english"></a>
English
Smart.Security is a .NET security helper library built on .NET cryptography APIs and BouncyCastle. It provides AES, RSA, SM2, SM3, SM4, hash, HMAC, password salt/hash, file hash, and secure random helpers. It supports .NET 8, 9, and 10.
Installation
dotnet add package Smart.Security
Examples
AES
using Smart.Security;
var key = SmartAESCipher.GenerateKey();
string cipherText = SmartAESCipher.Encrypt("Hello World", key.Key, key.IV);
string plainText = SmartAESCipher.Decrypt(cipherText, key.Key, key.IV);
RSA
using Smart.Security;
var keyPair = SmartRSACipher.GenerateRSAKeyPair(2048);
byte[] data = Encoding.UTF8.GetBytes("Secret Message");
byte[] cipherBytes = SmartRSACipher.Encrypt(keyPair.Public, data);
byte[] plainBytes = SmartRSACipher.Decrypt(keyPair.Private, cipherBytes);
byte[] signature = SmartRSACipher.Sign(keyPair.Private, data);
bool valid = SmartRSACipher.VerifySign(keyPair.Public, data, signature);
SM2 / SM3 / SM4
byte[] sm3 = SmartSM3Hash.SM3Hash(Encoding.UTF8.GetBytes("Data to hash"));
var sm4Key = SmartSM4Cipher.GenerateKey();
string sm4Cipher = SmartSM4Cipher.Encrypt("SM4 message", sm4Key.Base64Key, sm4Key.Base64IV);
string sm4Plain = SmartSM4Cipher.Decrypt(sm4Cipher, sm4Key.Base64Key, sm4Key.Base64IV);
byte[] sm2Cipher = SmartSM2Cipher.Encrypt(publicKeyBytes, plainBytes);
byte[] sm2Plain = SmartSM2Cipher.Decrypt(privateKeyBytes, sm2Cipher);
byte[] sm2Signature = SmartSM2Cipher.Sign(privateKeyBytes, plainBytes);
bool sm2Valid = SmartSM2Cipher.VerifySign(publicKeyBytes, plainBytes, sm2Signature);
Hash And HMAC
byte[] data = Encoding.UTF8.GetBytes("Data to hash");
string sha256 = SmartHash.SHA256Hash(data);
string sha512 = SmartHash.SHA512Hash(data);
string hmac = SmartHash.HmacSHA256(data, "secret-key");
File Hash
string md5 = SmartFileHash.MD5Hash(filePath);
string sha256 = await SmartFileHash.SHA256HashAsync(filePath);
Password Salt And Hash
SmartPassword generates a Base64 salt and stores a Base64 SHA256 hash of password + salt.
string salt = SmartPassword.GenerateSalt();
string hash = SmartPassword.HashPassword("user_password", salt);
bool valid = SmartPassword.VerifyPassword("user_password", salt, hash);
Secure Random
string base64Key = SmartRandom.GenerateKey(256);
byte[] bytes = SmartRandom.RandomArray(32);
string text = SmartRandom.RandomString(16);
string digits = SmartRandom.RandomNumber(6);
long number = SmartRandom.RandomLong();
Security Notes
- Prefer SHA256/384/512 over MD5/SHA1 for new scenarios.
- Use RSA keys of at least 2048 bits.
- Keep encryption keys and salts in secure storage.
- Use unique IV values for each encryption operation.
<a name="chinese"></a>
中文
Smart.Security 是基于 .NET 密码学 API 和 BouncyCastle 的安全工具库,提供 AES、RSA、SM2、SM3、SM4、哈希、HMAC、密码盐值/哈希、文件哈希和安全随机数等能力,兼容 .NET 8/9/10。
安装
dotnet add package Smart.Security
示例
AES
using Smart.Security;
var key = SmartAESCipher.GenerateKey();
string cipherText = SmartAESCipher.Encrypt("Hello World", key.Key, key.IV);
string plainText = SmartAESCipher.Decrypt(cipherText, key.Key, key.IV);
RSA
using Smart.Security;
var keyPair = SmartRSACipher.GenerateRSAKeyPair(2048);
byte[] data = Encoding.UTF8.GetBytes("Secret Message");
byte[] cipherBytes = SmartRSACipher.Encrypt(keyPair.Public, data);
byte[] plainBytes = SmartRSACipher.Decrypt(keyPair.Private, cipherBytes);
byte[] signature = SmartRSACipher.Sign(keyPair.Private, data);
bool valid = SmartRSACipher.VerifySign(keyPair.Public, data, signature);
SM2 / SM3 / SM4
byte[] sm3 = SmartSM3Hash.SM3Hash(Encoding.UTF8.GetBytes("待哈希数据"));
var sm4Key = SmartSM4Cipher.GenerateKey();
string sm4Cipher = SmartSM4Cipher.Encrypt("SM4 消息", sm4Key.Base64Key, sm4Key.Base64IV);
string sm4Plain = SmartSM4Cipher.Decrypt(sm4Cipher, sm4Key.Base64Key, sm4Key.Base64IV);
byte[] sm2Cipher = SmartSM2Cipher.Encrypt(publicKeyBytes, plainBytes);
byte[] sm2Plain = SmartSM2Cipher.Decrypt(privateKeyBytes, sm2Cipher);
byte[] sm2Signature = SmartSM2Cipher.Sign(privateKeyBytes, plainBytes);
bool sm2Valid = SmartSM2Cipher.VerifySign(publicKeyBytes, plainBytes, sm2Signature);
哈希与 HMAC
byte[] data = Encoding.UTF8.GetBytes("待哈希数据");
string sha256 = SmartHash.SHA256Hash(data);
string sha512 = SmartHash.SHA512Hash(data);
string hmac = SmartHash.HmacSHA256(data, "secret-key");
文件哈希
string md5 = SmartFileHash.MD5Hash(filePath);
string sha256 = await SmartFileHash.SHA256HashAsync(filePath);
密码盐值与哈希
SmartPassword 会生成 Base64 盐值,并保存 password + salt 的 Base64 SHA256 哈希。
string salt = SmartPassword.GenerateSalt();
string hash = SmartPassword.HashPassword("user_password", salt);
bool valid = SmartPassword.VerifyPassword("user_password", salt, hash);
安全随机数
string base64Key = SmartRandom.GenerateKey(256);
byte[] bytes = SmartRandom.RandomArray(32);
string text = SmartRandom.RandomString(16);
string digits = SmartRandom.RandomNumber(6);
long number = SmartRandom.RandomLong();
安全建议
- 新场景优先使用 SHA256/384/512,避免继续使用 MD5/SHA1。
- RSA 密钥长度建议不低于 2048 位。
- 加密密钥和盐值应存储在安全位置。
- 每次加密操作应使用唯一 IV。
Developed by zenglei
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net8.0
- BouncyCastle.Cryptography (>= 2.6.2)
-
net9.0
- BouncyCastle.Cryptography (>= 2.6.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Smart.Security:
| Package | Downloads |
|---|---|
|
BaseLibrary.ClassLibraryStand
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.1.6 | 45 | 5/16/2026 |
| 4.1.5 | 215 | 2/11/2026 |
| 4.1.4 | 113 | 2/8/2026 |
| 4.1.3 | 212 | 12/30/2025 |
| 4.1.2 | 247 | 10/15/2025 |
| 4.1.1 | 232 | 7/13/2025 |
| 4.1.0 | 209 | 4/5/2025 |
| 4.0.1 | 300 | 3/23/2025 |
| 4.0.0 | 264 | 3/20/2025 |
| 3.0.2 | 206 | 2/26/2025 |
| 3.0.1 | 224 | 2/15/2025 |
| 3.0.0 | 192 | 2/15/2025 |
| 2.0.2 | 242 | 2/9/2025 |
| 2.0.1 | 210 | 12/7/2024 |
| 1.1.1 | 206 | 12/7/2024 |
| 1.1.0.2 | 234 | 11/11/2024 |