Yoko.Tool.Security
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Yoko.Tool.Security --version 1.0.0
NuGet\Install-Package Yoko.Tool.Security -Version 1.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="Yoko.Tool.Security" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Yoko.Tool.Security --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Yoko.Tool.Security, 1.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.
// Install Yoko.Tool.Security as a Cake Addin #addin nuget:?package=Yoko.Tool.Security&version=1.0.0 // Install Yoko.Tool.Security as a Cake Tool #tool nuget:?package=Yoko.Tool.Security&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
常用的加密算法的封装,降低加密解密的使用复杂度
目前有的算法:AES,DES,RC4,RSA,SM2,SM3,SM4
支持 RSA XML 结构的 SecurityKey 和 Base64 格式的互转
本库不是去实现加密算法,而是基于.Net 提供的接口封装,为了方便使用
若是遇到了解密乱码,就在主项目中添加 System.Text.Encoding.CodePages 库,然后注册
var builder = WebApplication.CreateBuilder(args); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
RC4
/// <summary>
/// RC4
/// </summary>
public void Rc4()
{
const string data = "Microsoft";
var key = "123456"u8.ToArray();
var byte_data = Encoding.UTF8.GetBytes(data);
var secret = Rc4Crypt.Encrypt(byte_data, key);
var base64 = secret.ToBase64();
// TZEdFUtAevoL
var data_result = Rc4Crypt.Decrypt(secret, key);
var result = Encoding.UTF8.GetString(data_result);
// result == data
}
RSA
// XML私钥转换为Base64格式
var pri = RsaKeyConverter.ToBase64PrivateKey(PrivateKey);
// XML公钥转换为Base64格式
var pub = RsaKeyConverter.ToBase64PublicKey(PublicKey);
// 一样可以将Base64格式转换为XML格式
var xml_pri = RsaKeyConverter.ToXmlPrivateKey(pri);
var xml_pub = RsaKeyConverter.ToXmlPublicKey(pub);
//生成密钥对
var data = RsaCrypt.GenerateKey(520);
Console.WriteLine($"公钥:{data.PublicKey}");
Console.WriteLine($"私钥:{data.PrivateKey}");
/// <summary>
/// RSA加密解密测试
/// </summary>
public void RsaEncryptAndDecrypt()
{
const string data = "Microsoft";
// 将原文解析到二进制数组格式
var byte_data = Encoding.UTF8.GetBytes(data);
RsaCrypt.Encrypt(key.PublicKey, byte_data, out var secret_data);
var secret_str = secret_data.ToBase64();
Console.WriteLine(secret_str);
RsaCrypt.Decrypt(key.PrivateKey, secret_str.FromBase64(), out var data_byte);
var result = Encoding.UTF8.GetString(data_byte);
}
SM2
// 1. 生成密钥对
Sm2Crypt.GenerateKey(out byte[] publicKey, out byte[] privateKey);
Console.WriteLine("生成的公钥: " + Convert.ToBase64String(publicKey));
Console.WriteLine("生成的私钥: " + Convert.ToBase64String(privateKey));
// 设置待加密和签名的数据
string message = "Hello, SM2!";
byte[] data = Encoding.UTF8.GetBytes(message);
// 2. 使用公钥进行加密
byte[] encryptedData = Sm2Crypt.Encrypt(publicKey, data);
Console.WriteLine("加密后的数据: " + Convert.ToBase64String(encryptedData));
// 3. 使用私钥进行解密
byte[] decryptedData = Sm2Crypt.Decrypt(privateKey, encryptedData);
Console.WriteLine("解密后的数据: " + Encoding.UTF8.GetString(decryptedData));
// 4. 使用私钥签名
byte[] signature = Sm2Crypt.Signature(privateKey, data);
Console.WriteLine("签名: " + Convert.ToBase64String(signature));
// 5. 使用公钥验签
bool isVerified = Sm2Crypt.Verify(publicKey, data, signature);
Console.WriteLine("验签结果: " + (isVerified ? "成功" : "失败"));
SM3
private const string data = "Microsoft";
/// <summary>
/// SM3测试16进制字符串格式
/// </summary>
public void SM3HexString()
{
var byte_data = Sm3Crypt.Crypt(data);
var hex = byte_data.ToHex(); hex.ToUpper().Should().Be("1749CE3E4EF7622F1EBABB52078EC86309CABD5A6073C8A0711BF35E19BA51B8");
}
/// <summary>
/// SM3测试Base64字符串格式
/// </summary>
public void SM3Base64()
{
var byte_data = Sm3Crypt.Crypt(data);
var base64 = byte_data.ToBase64();
base64.ToUpper().Should().Be("F0NOPK73YI8EURTSB47IYWNKVVPGC8IGCRVZXHM6UBG=");
}
SM4
/// <summary>
/// SM4ECB模式加密到Base64格式
/// </summary>
public void Sm4EncryptECBToBase64()
{
const string data = "Microsoft";
// 将原文解析到二进制数组格式
var byte_data = Encoding.UTF8.GetBytes(data);
// 进制格式密钥加密数据
var result = Sm4Crypt.EncryptECB("701d1cc0cfbe7ee11824df718855c0c6", true, byte_data);
// 获取Base64格式的字符串结果
var base64 = result.ToBase64();
// ThRruxZZm1GrHE5KkP4UmQ==
}
/// <summary>
/// SM4ECB模式解密Base64格式到字符串
/// </summary>
public void Sm4DecryptECBTest()
{
// Base64格式的
const string data = "ThRruxZZm1GrHE5KkP4UmQ==";
// 将Base64格式字符串转为 byte[]
var byte_data = Convert.FromBase64String(data);
// 通过16进制格式密钥解密数据
var result = Sm4Crypt.DecryptECB("701d1cc0cfbe7ee11824df718855c0c6", true, byte_data);
// 解析结果获取字符串
var str = Encoding.UTF8.GetString(result);
// Microsoft
}
/// <summary>
/// SM4ECB模式加密到16进制字符串
/// </summary>
public void Sm4EncryptECBToHex16()
{
const string data = "Microsoft";
// 将原文解析到二进制数组格式
var byte_data = Encoding.UTF8.GetBytes(data);
// 使用16位长度的密钥加密
var result = Sm4Crypt.EncryptECB("1cc0cfbe7ee11824", false, byte_data);
// 将结果转为16进制字符串
var hex = result.ToHexString();
// D265DF0510C05FE836D3113B3ACEC714
}
/// <summary>
/// SM4ECB模式解密16进制字符串格式密文
/// </summary>
public void Sm4DecryptECBTest2()
{
const string data = "D265DF0510C05FE836D3113B3ACEC714";
var byte_data = data.FromHex();
var result = Sm4Crypt.DecryptECB("1cc0cfbe7ee11824", false, byte_data);
// 解析结果获取字符串
var str = Encoding.UTF8.GetString(result);
// Microsoft
}
/// <summary>
/// SM4CBC模式加密到Base64格式
/// </summary>
public void Sm4EncryptCBCTest()
{
const string data = "Microsoft";
var byte_data = Encoding.UTF8.GetBytes(data);
var result = Sm4Crypt.EncryptCBC("701d1cc0cfbe7ee11824df718855c0c6", true, "701d1cc0cfbe7ee11824df718855c0c5", byte_data);
var base64 = result.ToBase64();
// Q2iUaMuSHjLvq6GhUQnGTg==
}
/// <summary>
/// SM4CBC模式从Base64解密
/// </summary>
public void Sm4DecryptCBCTest()
{
const string data = "Q2iUaMuSHjLvq6GhUQnGTg==";
var byte_data = Convert.FromBase64String(data);
var result = Sm4Crypt.DecryptCBC("701d1cc0cfbe7ee11824df718855c0c6", true, "701d1cc0cfbe7ee11824df718855c0c5", byte_data);
var str = Encoding.UTF8.GetString(result);
// Microsoft
}
/// <summary>
/// SM4CBC模式加密到16进制字符串
/// </summary>
public void Sm4EncryptCBCTest2()
{
const string data = "Microsoft";
var byte_data = Encoding.UTF8.GetBytes(data);
var result = Sm4Crypt.EncryptCBC("1cc0cfbe7ee11824", false, "1cc0cfbe7ee12824", byte_data);
var hex = result.ToHexString();
// 1BD7A32E49B60B17698AAC9D1E4FEE4A
}
/// <summary>
/// SM4CBC模式从Hex16解密到字符串
/// </summary>
public void Sm4DecryptCBCTest2()
{
const string data = "1BD7A32E49B60B17698AAC9D1E4FEE4A";
var byte_data = data.FromHex();
var result = Sm4Crypt.DecryptCBC("1cc0cfbe7ee11824", false, "1cc0cfbe7ee12824", byte_data);
var str = Encoding.UTF8.GetString(result);
// Microsoft
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- BouncyCastle.Cryptography (>= 2.4.0)
-
net8.0
- BouncyCastle.Cryptography (>= 2.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.