CloudYxt.Crypto
1.0.5
See the version list below for details.
dotnet add package CloudYxt.Crypto --version 1.0.5
NuGet\Install-Package CloudYxt.Crypto -Version 1.0.5
<PackageReference Include="CloudYxt.Crypto" Version="1.0.5" />
paket add CloudYxt.Crypto --version 1.0.5
#r "nuget: CloudYxt.Crypto, 1.0.5"
// Install CloudYxt.Crypto as a Cake Addin #addin nuget:?package=CloudYxt.Crypto&version=1.0.5 // Install CloudYxt.Crypto as a Cake Tool #tool nuget:?package=CloudYxt.Crypto&version=1.0.5
云享通.Net Core针对常用加解密常规操作库
云享通.Net Core针对常用加解密常规操作库,如:RSA,PKCS1,PKCS8,SHA1WITHRSA,MD5,SM2,SM3,SM4,SHA256,HMAC-SHA256,同时支持RSA的私钥格式转换PrivateKeyPkcs1ToPkcs8和PrivateKeyPkcs8ToPkcs1
典型示例
国密2加解密及签名验签示例
// 生成密钥对
CryptoSM2.GenerateKey(out var publicKey, out var privateKey);
Console.WriteLine($"Public Key: {publicKey}");
Console.WriteLine($"Private Key: {privateKey}");
// 测试加密:Base64与Hex
string message = "Hello, SM2! 你好";
string encryptedBase64 = CryptoSM2.EncryptStringToBase64(message, publicKey, true); // true表示返回Base64
string encryptedHex = CryptoSM2.EncryptToHex(Encoding.UTF8.GetBytes(message), publicKey);
Console.WriteLine($"Encrypted Base64: {encryptedBase64}");
Console.WriteLine($"Encrypted Hex: {encryptedHex}");
// 测试解密
string decrypted = CryptoSM2.DecryptFromBase64(encryptedBase64, privateKey);
Console.WriteLine($"Decrypted: {decrypted}");
// 测试签名
string signature = CryptoSM2.Sign(message, privateKey);
Console.WriteLine($"Signature: {signature}");
// 测试验证签名
bool isVerified = CryptoSM2.VerifySign(message, signature, publicKey);
Console.WriteLine($"Signature Verified: {isVerified}");
// 加密解密的完整流程
Console.WriteLine("\n--- Complete Encryption/Decryption Test ---");
string originalMessage = "Encrypt and Decrypt with SM2";
string encryptedMessage = CryptoSM2.EncryptStringToBase64(originalMessage, publicKey, true);
string decryptedMessage = CryptoSM2.DecryptFromBase64(encryptedMessage, privateKey);
Console.WriteLine($"Original Message: {originalMessage}");
Console.WriteLine($"Encrypted Message (Base64): {encryptedMessage}");
Console.WriteLine($"Decrypted Message: {decryptedMessage}");
// 签名与验证的完整流程
Console.WriteLine("\n--- Complete Signing/Verification Test ---");
string originalMessageForSign = "SM2 Signing Example";
string generatedSignature = CryptoSM2.Sign(originalMessageForSign, privateKey);
bool verificationResult = CryptoSM2.VerifySign(originalMessageForSign, generatedSignature, publicKey);
Console.WriteLine($"Original Message: {originalMessageForSign}");
Console.WriteLine($"Generated Signature: {generatedSignature}");
Console.WriteLine($"Signature Verification Result: {verificationResult}");
国密3加密示例
string text = "你好, SM3!";
string charset = "utf-8";
// 加密为字节数组
byte[] hashBytes = CryptoSM3.Encrypt(text, charset);
Console.WriteLine($"Encrypt: {BitConverter.ToString(hashBytes).Replace("-", "")}");
// 加密为 HEX 字符串
string hashHex = CryptoSM3.EncryptToHex(text, charset);
Console.WriteLine($"Encrypt To Hex: {hashHex}");
// 加密为 Base64 字符串
string hashBase64 = CryptoSM3.EncryptToBase64(text, charset);
Console.WriteLine($"Encrypt To Base64: {hashBase64}");
// 测试不同字符集
string textGBK = "你好, SM3! (GBK)";
string charsetGBK = "gbk";
string hashHexGBK = CryptoSM3.EncryptToHex(textGBK, charsetGBK);
Console.WriteLine($"Encrypt To Hex (GBK): {hashHexGBK}");
string hashBase64GBK = CryptoSM3.EncryptToBase64(textGBK, charsetGBK);
Console.WriteLine($"Encrypt To Base64 (GBK): {hashBase64GBK}");
// 测试空字符串
string emptyText = string.Empty;
string emptyHex = CryptoSM3.EncryptToHex(emptyText);
Console.WriteLine($"Encrypt To Hex (Empty): {emptyHex}");
string emptyBase64 = CryptoSM3.EncryptToBase64(emptyText);
Console.WriteLine($"Encrypt To Base64 (Empty): {emptyBase64}");
国密4加/解密示例
string key = "1234567890abcdef";
string iv = "abcdef1234567890";
string text = "你好, SM4!";
// ECB 模式
string ecbHex = CryptoSM4.EncryptECBToHex(key, text);
string ecbBase64 = CryptoSM4.EncryptECBToBase64(key, text);
Console.WriteLine($"ECB Hex: {ecbHex}");
Console.WriteLine($"ECB Base64: {ecbBase64}");
Console.WriteLine($"ECB Decrypt Hex: {CryptoSM4.DecryptECBFromHex(key, ecbHex)}");
Console.WriteLine($"ECB Decrypt Base64: {CryptoSM4.DecryptECBFromBase64(key, ecbBase64)}");
// CBC 模式
string cbcHex = CryptoSM4.EncryptCBCToHex(key, iv, text);
string cbcBase64 = CryptoSM4.EncryptCBCToBase64(key, iv, text);
Console.WriteLine($"CBC Hex: {cbcHex}");
Console.WriteLine($"CBC Base64: {cbcBase64}");
Console.WriteLine($"CBC Decrypt Hex: {CryptoSM4.DecryptCBCFromHex(key, iv, cbcHex)}");
Console.WriteLine($"CBC Decrypt Base64: {CryptoSM4.DecryptCBCFromBase64(key, iv, cbcBase64)}");
RSA加密及验签
// 生成RSA密钥对
var rsa = new CryptoRSA(2048, CryptoRSA_PKCSType.PKCS8, true);
Console.WriteLine($"Public Key:\n{rsa.PublicKey}");
Console.WriteLine($"Private Key:\n{rsa.PrivateKey}");
// 从私钥中提取公钥
string extractedPublicKey = CryptoRSA.GetPublicKeyFromPrivateKey(rsa.PrivateKey, CryptoRSA_PKCSType.PKCS8);
Console.WriteLine($"Extracted Public Key:\n{extractedPublicKey}");
// 私钥格式转换:PKCS8转PKCS1
string pkcs1PrivateKey = CryptoRSA.PrivateKeyPkcs8ToPkcs1(rsa.PrivateKey, true);
Console.WriteLine($"PKCS8 to PKCS1 (PEM):\n{pkcs1PrivateKey}");
// 私钥格式转换:PKCS1转PKCS8
string pkcs8PrivateKey = CryptoRSA.PrivateKeyPkcs1ToPkcs8(pkcs1PrivateKey, true);
Console.WriteLine($"PKCS1 to PKCS8 (PEM):\n{pkcs8PrivateKey}");
// 数据签名与验签
string data = "Hello, RSA!";
string signSHA1 = CryptoRSA.SHA1WithRSASign(data, rsa.PrivateKey);
Console.WriteLine($"SHA1 Sign:\n{signSHA1}");
bool isValidSHA1 = CryptoRSA.SHA1WithRSAValid(data, rsa.PublicKey, signSHA1);
Console.WriteLine($"SHA1 Signature Valid: {isValidSHA1}");
string signSHA256 = CryptoRSA.SHA256WithRSASign(data, rsa.PrivateKey);
Console.WriteLine($"SHA256 Sign:\n{signSHA256}");
bool isValidSHA256 = CryptoRSA.SHA256WithRSAValid(data, rsa.PublicKey, signSHA256);
Console.WriteLine($"SHA256 Signature Valid: {isValidSHA256}");
其他MD5、SHA256、HmacSHA256
CryptoMD5.Encrypt
CryptoSHA256.Encrypt
CryptoHmacSHA256.Encrypt
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Portable.BouncyCastle (>= 1.9.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CloudYxt.Crypto:
Package | Downloads |
---|---|
CloudYxt.TenpayV3
云享通.Net Core针对微信支付V3扩展操作库 |
GitHub repositories
This package is not used by any popular GitHub repositories.
云享通.Net Core针对常用加解密常规操作库。
1.0.5
增加基于PKCS7填充的ECB、CBC的SM4算法以及SM2加解密,签名、验签(同时支持C1|C2|C3和C1|C3|C2)
1.0.4
新增CryptoX509RSA类,继承自X509Certificate2,可初始化P12证书,并能直接获取Base64格式的X509v3公钥、BouncyCastle公钥、PKCS1私钥、PKCS8私钥
1.0.3
增加AesGcm解密方法(常用于微信支付V3)
1.0.2
增加PEM格式文本简化为BASE64字符串的简单方法PemToBase64String
1.0.1
新增SHA256的RSA签名和验签(常用于微信支付V3)
1.0.0
实现RSA基于PKCS8的常规操作方法,如:PKCS1与PKCS8转换,PKCS1、PKCS8签名,验证签名等
实现基于BouncyCastle的常规算法,如:国密3、SHA256、HMAC-SHA256等