Yoko.Tool.Security 1.0.1

dotnet add package Yoko.Tool.Security --version 1.0.1                
NuGet\Install-Package Yoko.Tool.Security -Version 1.0.1                
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.1" />                
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.1                
#r "nuget: Yoko.Tool.Security, 1.0.1"                
#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.1

// Install Yoko.Tool.Security as a Cake Tool
#tool nuget:?package=Yoko.Tool.Security&version=1.0.1                

常用的加密算法的封装,降低加密解密的使用复杂度

  • 目前支持:AES,DES,RC4,RSA,SM2,SM4,MD5

  • 支持 RSA XML 结构的 SecurityKey 和 Base64 格式的互转

  • 本库不是去实现加密算法,而是基于.Net 提供的接口封装,为了方便使用

  • 若是遇到了解密乱码,就在主项目中添加 System.Text.Encoding.CodePages 库,然后注册

    var builder = WebApplication.CreateBuilder(args);
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    

使用示例

AES
// 原始文本数据
string originalText = "Hello, AES Encryption!";
Console.WriteLine("原始数据: " + originalText);

// 将文本数据转换为字节数组
byte[] content = Encoding.UTF8.GetBytes(originalText);

// 设置密码
string password = "mySecretPassword";

// 选择AES加密模式
AesKeyModel keyModel = AesKeyModel.AES256;

// 加密数据
byte[] encryptedData = AesCrypt.Encrypt(content, password, keyModel);
Console.WriteLine("加密后的数据 (Base64): " + Convert.ToBase64String(encryptedData));

// 解密数据
byte[] decryptedData = AesCrypt.Decrypt(encryptedData, password, keyModel);
string decryptedText = Encoding.UTF8.GetString(decryptedData);
Console.WriteLine("解密后的数据: " + decryptedText);
DES
// 原始文本数据
string originalText = "Hello, DES and TripleDES Encryption!";
Console.WriteLine("原始数据: " + originalText);

// 将文本数据转换为字节数组
byte[] content = Encoding.UTF8.GetBytes(originalText);

// 设置密码
string password = "securePassword123";

// DES 加密和解密示例
Console.WriteLine("\n--- DES 加密与解密 ---");
byte[] encryptedDesData = DesCrypt.Encrypt(content, password);
Console.WriteLine("DES 加密后的数据 (Base64): " + Convert.ToBase64String(encryptedDesData));

byte[] decryptedDesData = DesCrypt.Decrypt(encryptedDesData, password);
string decryptedDesText = Encoding.UTF8.GetString(decryptedDesData);
Console.WriteLine("DES 解密后的数据: " + decryptedDesText);

// TripleDES 加密和解密示例
Console.WriteLine("\n--- TripleDES 加密与解密 ---");
byte[] encryptedTripleDesData = TripleDes.Encrypt(content, password);
Console.WriteLine("TripleDES 加密后的数据 (Base64): " + Convert.ToBase64String(encryptedTripleDesData));

byte[] decryptedTripleDesData = TripleDes.Decrypt(encryptedTripleDesData, password);
string decryptedTripleDesText = Encoding.UTF8.GetString(decryptedTripleDesData);
Console.WriteLine("TripleDES 解密后的数据: " + decryptedTripleDesText);
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 ? "成功" : "失败"));
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
}
MD5
// 输入字符串
string input = "Hello, MD5 Encryption!";
Console.WriteLine("原始数据: " + input);

// 获取32位长度的MD5大写字符串
string md5Hash32 = input.To32MD5();
Console.WriteLine("32位MD5哈希: " + md5Hash32);

// 获取16位长度的MD5大写字符串
string md5Hash16 = input.To16MD5();
Console.WriteLine("16位MD5哈希: " + md5Hash16);

更新日志

1.0.1

【新增】Net6的支持

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.1 82 11/8/2024
1.0.0 74 11/5/2024