newdcm.Cryptor
1.0.6
dotnet add package newdcm.Cryptor --version 1.0.6
NuGet\Install-Package newdcm.Cryptor -Version 1.0.6
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="newdcm.Cryptor" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="newdcm.Cryptor" Version="1.0.6" />
<PackageReference Include="newdcm.Cryptor" />
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 newdcm.Cryptor --version 1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: newdcm.Cryptor, 1.0.6"
#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 newdcm.Cryptor@1.0.6
#: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=newdcm.Cryptor&version=1.0.6
#tool nuget:?package=newdcm.Cryptor&version=1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
1. 常用的加解密方法及hash值计算方法
1.1 AESCryptHelper类
AES对称加密与解密的工具类
string sourceStr = "要加密的串";
string key = "encryptKey密钥";
string iv = "iv偏移量";
string cryptBase64Str = AESCryptHelper.Encrypt(sourceStr,key,iv);
string sourceStr = AESCryptHelper.Decrypt(cryptBase64Str,key,iv);
1.2 HashHelper类
常用的Hash值计算方法,包括计算字符串,本地文件,流的Md5,SHA1,SHA256等类型的Hash值
var algorithm = MD5.Create();
// var algorithm = SHA1.Create();
// var algorithm = SHA256.Create();
// 计算字符串md5值
string md5Str = await HashHelper.Instance().ComputeStringHash("原串",algorithm);
// 获取文件md5值(此方法进行了效率优化,50M以下的直接获取hash,50M以上的采用流式计算)
string filePath = "D:\\1.txt";
string fileMd5 = await HashHelper.Instance().ComputeFileHash(filePath,algorithm);
// 同时获取文件的md5,sha1与sha256值(高效流式分块计算)
var fileHashAll = await HashHelper.Instance().ComputeFileAllHash(filePath);
string md5OfFile = fileHashAll.Item1;
string sha1OfFile = fileHashAll.Item2;
string sha256OfFile = fileHashAll.Item3;
//另外就是针对stream类型的hash计算
//HashHelper.Instance().ComputeStreamHash(stream,algorithm); //直接计算
//HashHelper.Instance().ComputeStreamHash2(stream,algorithm); //高效流式分块计算
//HashHelper.Instance().ComputeAllHash(stream);//高效流式分块计算三种hash返回
1.3 SM2Helper类
SM2国密非对称加密算法辅助类(无状态版本)
支持:密钥对生成、公钥加密、私钥解密、私钥签名、公钥验签
特点:
- 无状态静态类,所有方法均为静态方法,支持依赖注入
- 适用于客户端用公钥加密,服务端用私钥解密的场景
- 支持SM2签名与验签功能
- 支持PEM格式密钥导出
- 可用于加密SM4对称密钥或其他敏感信息
// 1. 生成SM2密钥对
var (publicKey, privateKey) = SM2Helper.GenerateKeys();
Console.WriteLine($"生成的公钥:{publicKey}");
Console.WriteLine($"生成的私钥:{privateKey}");
// 2. 公钥加密(客户端使用)
string plainText = "今天我,寒夜里看雨飘过,转着冷却了的心窝漂远方";
string cipherText = SM2Helper.EncryptWithPublicKey(publicKey, plainText);
Console.WriteLine($"SM2密文:{cipherText}");
// 3. 私钥解密(服务端使用)
string decryptedText = SM2Helper.DecryptWithPrivateKey(privateKey, cipherText);
Console.WriteLine($"解密后:{decryptedText}");
// 4. 私钥签名(服务端使用)
string dataToSign = "精诚所至,金石也不开!";
string signature = SM2Helper.SignWithPrivateKey(privateKey, dataToSign);
Console.WriteLine($"对[{dataToSign}]进行SM2签名,签名值:{signature}");
// 5. 公钥验签(客户端使用)
bool isValid = SM2Helper.VerifyWithPublicKey(publicKey, dataToSign, signature);
Console.WriteLine($"验签结果:{isValid}");
// 6. 导出PEM格式密钥
string publicKeyPem = SM2Helper.ExportPublicKeyToPem(publicKey);
string privateKeyPem = SM2Helper.ExportPrivateKeyToPem(privateKey);
Console.WriteLine($"公钥PEM格式:\n{publicKeyPem}");
Console.WriteLine($"私钥PEM格式:\n{privateKeyPem}");
1.4 SM3Helper类
SM3国密哈希算法辅助类(纯静态版本)
支持:字符串哈希、文件哈希、流哈希、带盐哈希、HMAC-SM3
特点:
- 无状态静态类,所有方法均为静态方法
- 支持可选盐值,增强安全性
- 支持多种输出格式:十六进制(64位)、Base64、字节数组
- 支持文件异步哈希计算,避免阻塞
- 支持HMAC-SM3,可用于API签名
- 支持快速数据指纹生成
// 1. 字符串哈希
string sourceStr = "今天是一个非常好的天气,万里无云,乌云密布";
// 十六进制格式(64位)
string hexHash = SM3Helper.ComputeHashHex(sourceStr);
Console.WriteLine($"十六进制哈希:{hexHash}");
// Base64格式
string base64Hash = SM3Helper.ComputeHashBase64(sourceStr);
Console.WriteLine($"Base64哈希:{base64Hash}");
// 字节数组格式
byte[] hashBytes = SM3Helper.ComputeHash(sourceStr);
// 2. 带盐哈希
string salt = "随机盐值";
string hexHashWithSalt = SM3Helper.ComputeHashHex(sourceStr, salt);
string base64HashWithSalt = SM3Helper.ComputeHashBase64(sourceStr, salt);
// 3. 文件哈希
string filePath = "z:\\1demo.rar";
// 十六进制格式(异步)
string fileHexHash = await SM3Helper.ComputeFileHashHexAsync(filePath);
Console.WriteLine($"文件十六进制哈希:{fileHexHash}");
// Base64格式(异步)
string fileBase64Hash = await SM3Helper.ComputeFileHashBase64Async(filePath);
Console.WriteLine($"文件Base64哈希:{fileBase64Hash}");
// 带盐的文件哈希
string fileHexHashWithSalt = await SM3Helper.ComputeFileHashHexAsync(filePath, salt);
// 同步方法
byte[] fileHashBytes = SM3Helper.ComputeFileHash(filePath);
// 4. Stream流哈希
using (var stream = File.OpenRead(filePath))
{
string streamHexHash = await SM3Helper.ComputeStreamHashHexAsync(stream);
string streamBase64Hash = await SM3Helper.ComputeStreamHashBase64Async(stream);
// 流式哈希完成后需要重新定位流位置
stream.Seek(0, SeekOrigin.Begin);
byte[] streamHashBytes = await SM3Helper.ComputeStreamHashAsync(stream);
}
// 同步方法
using (var stream = File.OpenRead(filePath))
{
byte[] streamHash = SM3Helper.ComputeStreamHash(stream);
}
// 5. HMAC-SM3(用于API签名)
string message = "要签名的消息";
byte[] hmacKey = Encoding.UTF8.GetBytes("HMAC密钥");
string hmacHex = SM3Helper.HmacSm3Hex(message, hmacKey);
string hmacBase64 = SM3Helper.HmacSm3Base64(message, hmacKey);
Console.WriteLine($"HMAC-SM3十六进制:{hmacHex}");
Console.WriteLine($"HMAC-SM3 Base64:{hmacBase64}");
// 6. 验证哈希值
string original = "原始数据";
string computedHash = SM3Helper.ComputeHashHex(original);
// 验证字符串哈希
bool isValid = SM3Helper.VerifyHash(original, computedHash);
Console.WriteLine($"哈希验证结果:{isValid}");
// 验证文件哈希
string fileHash = await SM3Helper.ComputeFileHashHexAsync(filePath);
bool isFileValid = await SM3Helper.VerifyFileHashAsync(filePath, fileHash);
Console.WriteLine($"文件哈希验证结果:{isFileValid}");
// 7. 快速数据指纹(前16字节,Base64格式)
string data = "需要生成指纹的数据";
string fingerprint = SM3Helper.GetFingerprint(data);
Console.WriteLine($"数据指纹:{fingerprint}");
// 带盐的指纹
string fingerprintWithSalt = SM3Helper.GetFingerprint(data, salt);
1.5 SM4Helper类
SM4国密对称加密算法辅助类(纯静态版本)
支持:密钥生成、IV生成、CBC/ECB/CTR/GCM模式加解密
特点:
- 无状态静态类,所有方法均为静态方法,支持依赖注入
- CBC模式:自动管理IV,密文中包含IV(推荐用于普通数据)
- GCM模式:认证加密模式,Nonce错误或密文被篡改时会抛出异常(推荐用于高安全场景)
- CTR模式:流式加密,适合大文件,支持自动或手动管理IV
- ECB模式:不推荐用于普通数据加密(仅用于密钥材料加密)
- 支持文件流式加解密,避免内存溢出
// 1. 生成密钥和IV
string sm4Key = SM4Helper.GenerateKeyBase64(); // 生成16字节随机密钥
string sm4Iv = SM4Helper.GenerateIVBase64(); // 生成16字节随机IV
string nonce = SM4Helper.GenerateGCMNonceBase64(); // 生成12字节GCM Nonce
// 2. CBC模式(自动管理IV,推荐)
string plainText = "长亭外 古道边 芳草碧连天";
string cbcCipher = SM4Helper.EncryptCBC(sm4Key, plainText); // 加密(IV自动拼接)
string cbcPlain = SM4Helper.DecryptCBC(sm4Key, cbcCipher); // 解密(自动提取IV)
// 3. GCM模式(认证加密,推荐高安全场景)
string gcmCipher = SM4Helper.EncryptGCMWithNonce(sm4Key, nonce, plainText); // 加密
string gcmPlain = SM4Helper.DecryptGCMWithNonce(sm4Key, nonce, gcmCipher); // 解密
// 使用错误的Nonce会抛出异常
// string wrongNonce = SM4Helper.GenerateGCMNonceBase64();
// string wrongPlain = SM4Helper.DecryptGCMWithNonce(sm4Key, wrongNonce, gcmCipher); // 异常!
// 4. CTR模式(自动管理IV,适合大文件/流式加密)
string ctrCipher = SM4Helper.EncryptCTR(sm4Key, plainText); // 加密(IV自动拼接)
string ctrPlain = SM4Helper.DecryptCTR(sm4Key, ctrCipher); // 解密(自动提取IV)
// 5. CTR模式(手动指定IV,IV需要自行管理)
string ctrWithIvCipher = SM4Helper.EncryptCTRWithIV(sm4Key, sm4Iv, plainText); // 加密
string ctrWithIvPlain = SM4Helper.DecryptCTRWithIV(sm4Key, sm4Iv, ctrWithIvCipher); // 解密
// 6. ECB模式(不推荐,仅用于密钥材料加密)
string ecbCipher = SM4Helper.EncryptECB(sm4Key, plainText); // ECB加密
string ecbPlain = SM4Helper.DecryptECB(sm4Key, ecbCipher); // ECB解密
// 7. 文件加密(CTR模式,自动管理IV)
string inputFile = "z:\\1demo.rar";
string encryptedFile = "z:\\encrypted.bin";
string decryptedFile = "z:\\decrypted.rar";
await SM4Helper.EncryptFileAsync(sm4Key, inputFile, encryptedFile); // 加密文件
await SM4Helper.DecryptFileAsync(sm4Key, encryptedFile, decryptedFile); // 解密文件
// 8. 文件加密(手动指定IV)
await SM4Helper.EncryptFileAsync(sm4Key, inputFile, encryptedFile, sm4Iv); // 加密(指定IV)
await SM4Helper.DecryptFileAsync(sm4Key, encryptedFile, decryptedFile, sm4Iv); // 解密(指定相同IV)
// 9. 流式加解密(适用于超大文件或网络流)
using (var inputStream = File.OpenRead(inputFile))
using (var outputStream = File.Create(encryptedFile))
{
SM4Helper.EncryptStream(sm4Key, inputStream, outputStream); // 加密流
}
using (var inputStream = File.OpenRead(encryptedFile))
using (var outputStream = File.Create(decryptedFile))
{
SM4Helper.DecryptStream(sm4Key, inputStream, outputStream); // 解密流
}
| 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 was computed. 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 was computed. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.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.