EasilyNET.Security
3.24.1206.100
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 EasilyNET.Security --version 3.24.1206.100
NuGet\Install-Package EasilyNET.Security -Version 3.24.1206.100
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="EasilyNET.Security" Version="3.24.1206.100" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.Security --version 3.24.1206.100
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.Security, 3.24.1206.100"
#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 EasilyNET.Security as a Cake Addin #addin nuget:?package=EasilyNET.Security&version=3.24.1206.100 // Install EasilyNET.Security as a Cake Tool #tool nuget:?package=EasilyNET.Security&version=3.24.1206.100
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.Security
一个.Net 中常用的加密算法的封装.降低加密解密的使用复杂度.
目前有的算法:AES,DES,RC4,TripleDES,RSA,SM2,SM3,SM4
支持 RSA XML 结构的 SecurityKey 和 Base64 格式的互转.
本库不是去实现加密算法,而是基于.Net 提供的接口封装,为了方便使用
未经测试的预测,若是遇到了解密乱码,可能是需要引入一个包.
在主项目中添加 System.Text.Encoding.CodePages 库,并在程序入口处添加注册代码. Programe.cs
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
private const string PublicKey =
"<RSAKeyValue><Modulus>p0H+4fFMZFIQq6LAhXNf3Zml3imiOCU6mzMiMcqRu/x5x08X3pWF6m+v+1T7fOo4P/2GYluZiuQzKveJnRLxV0O6kRdVivApZ6C/Lb8vnsnocaSlmEOoswdOZvmhe3s4tAQ4KlZerWdRYx3dUgoH862IUv6FNiSyF/z3TP4M50E=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private const string PrivateKey =
"<RSAKeyValue><Modulus>p0H+4fFMZFIQq6LAhXNf3Zml3imiOCU6mzMiMcqRu/x5x08X3pWF6m+v+1T7fOo4P/2GYluZiuQzKveJnRLxV0O6kRdVivApZ6C/Lb8vnsnocaSlmEOoswdOZvmhe3s4tAQ4KlZerWdRYx3dUgoH862IUv6FNiSyF/z3TP4M50E=</Modulus><Exponent>AQAB</Exponent><P>2Nhp0/mM3qRkTqzUe5DeI5r5hmDS24XYsSteHKLNuD66sa0K1EFprTstw96+ZJdXW/bJ+R58j78YP1frkLFVpw==</P><Q>xXVq4nyP7UppDXXyKTjWindWmMTuD0v9el6EnyWiqaoaGRF6IAL1nKOcrh5O/BiFgBZm5cTn0knUNE8BzaFI1w==</Q><DP>T4ur3qe3pmC1ryq1U5LD+lm6WTIhh4flArD++BA82O6h+9WXmF9ajcKcujJ2s13VHRY95xXo6a5XDb2J221CFw==</DP><DQ>JpxaJSV0Q1MsjZxFmPfrQ7IoNcE0R7t1OktnJXOHhiOj7Mj1F3NcsZ9wkL+OdE8bM7utrTo+lmknXH8ifCIQiw==</DQ><InverseQ>VvMW94/iyBgKNH8N1Xdikn17BLC0a8tYsm/H/VDMXjaVngAXGqeN370Hd1Zukj0EGVw/vEl2YsAoJ6o9KmQgNA==</InverseQ><D>CkO8lrUuUQHQUBg+5HG+MmVZjpbTg8qVHC05LgEWjIjkGF08Q8a9XzPXgv8mJ/Zf2V1/v82LUMDMexiR83fUI8NxNuZjD+ldR1ZqmtE96+4laA0/WUH0fb1nBA6foVS+WB643nN01dJs0/3IV65k35GfkuqKDct6gld8/UFaN1k=</D></RSAKeyValue>";
private static readonly RsaSecretKey key = new(PrivateKey, PublicKey);
/// <summary>
/// 转换Key,Base64和XML格式互转. Java和其他语言里一般使用base64格式,.Net中一般为XML格式
/// </summary>
public void XmlToBase64Key()
{
// 私钥转换为Base64格式
var pri = RsaKeyConverter.ToBase64PrivateKey(PrivateKey);
// MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKdB/uHxTGRSEKuiwIVzX92Zpd4pojglOpszIjHKkbv8ecdPF96Vhepvr/tU+3zqOD/9hmJbmYrkMyr3iZ0S8VdDupEXVYrwKWegvy2/L57J6HGkpZhDqLMHTmb5oXt7OLQEOCpWXq1nUWMd3VIKB/OtiFL+hTYkshf890z+DOdBAgMBAAECgYAKQ7yWtS5RAdBQGD7kcb4yZVmOltODypUcLTkuARaMiOQYXTxDxr1fM9eC/yYn9l/ZXX+/zYtQwMx7GJHzd9Qjw3E25mMP6V1HVmqa0T3r7iVoDT9ZQfR9vWcEDp+hVL5YHrjec3TV0mzT/chXrmTfkZ+S6ooNy3qCV3z9QVo3WQJBANjYadP5jN6kZE6s1HuQ3iOa+YZg0tuF2LErXhyizbg+urGtCtRBaa07LcPevmSXV1v2yfkefI+/GD9X65CxVacCQQDFdWrifI/tSmkNdfIpONaKd1aYxO4PS/16XoSfJaKpqhoZEXogAvWco5yuHk78GIWAFmblxOfSSdQ0TwHNoUjXAkBPi6vep7emYLWvKrVTksP6WbpZMiGHh+UCsP74EDzY7qH71ZeYX1qNwpy6MnazXdUdFj3nFejprlcNvYnbbUIXAkAmnFolJXRDUyyNnEWY9+tDsig1wTRHu3U6S2clc4eGI6PsyPUXc1yxn3CQv450Txszu62tOj6WaSdcfyJ8IhCLAkBW8xb3j+LIGAo0fw3Vd2KSfXsEsLRry1iyb8f9UMxeNpWeABcap43fvQd3Vm6SPQQZXD+8SXZiwCgnqj0qZCA0
// 公钥转换为Base64格式
var pub = RsaKeyConverter.ToBase64PublicKey(PublicKey);
// MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnQf7h8UxkUhCrosCFc1/dmaXeKaI4JTqbMyIxypG7/HnHTxfelYXqb6/7VPt86jg//YZiW5mK5DMq94mdEvFXQ7qRF1WK8ClnoL8tvy+eyehxpKWYQ6izB05m+aF7ezi0BDgqVl6tZ1FjHd1SCgfzrYhS/oU2JLIX/PdM/gznQQIDAQAB
// 一样可以将Base64格式转换为XML格式
var xml_pri = RsaKeyConverter.ToXmlPrivateKey(pri);
// xml_pri == PrivateKey
var xml_pub = RsaKeyConverter.ToXmlPublicKey(pub);
// xml_pub == PublicKey
}
/// <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);
// result == data
}
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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- BouncyCastle.Cryptography (>= 2.5.0)
-
net9.0
- BouncyCastle.Cryptography (>= 2.5.0)
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 |
---|---|---|
3.24.1224.141 | 72 | 12/24/2024 |
3.24.1216.116 | 88 | 12/16/2024 |
3.24.1206.100 | 107 | 12/6/2024 |
3.24.1205.171 | 91 | 12/5/2024 |
3.24.1202.150 | 85 | 12/2/2024 |
3.24.1126.231 | 61 | 11/26/2024 |
3.24.1126.172 | 55 | 11/26/2024 |
3.24.1126.114 | 51 | 11/26/2024 |
3.24.1126.104 | 48 | 11/26/2024 |
3.24.1125.181 | 37 | 11/25/2024 |
3.24.1125.104 | 55 | 11/25/2024 |
3.24.1121.183 | 48 | 11/21/2024 |
3.24.1120.183 | 49 | 11/20/2024 |
3.24.1119.31 | 58 | 11/18/2024 |
3.24.1115.143 | 45 | 11/15/2024 |
3.24.1113.100 | 67 | 11/13/2024 |
3.24.1112.125 | 59 | 11/12/2024 |
3.24.1107.140 | 69 | 11/7/2024 |
3.24.1107.54 | 44 | 11/7/2024 |
3.24.1107.34 | 49 | 11/7/2024 |
3.24.1105.111 | 53 | 11/5/2024 |
3.24.1103.31 | 73 | 11/2/2024 |
3.24.1103 | 56 | 11/2/2024 |
3.24.1031.135 | 51 | 10/31/2024 |
3.24.1031.112 | 48 | 10/31/2024 |
3.24.1031.104 | 48 | 10/31/2024 |
3.24.1029.142 | 78 | 10/29/2024 |
3.24.1025.30 | 52 | 10/24/2024 |
3.24.1022.142 | 41 | 10/22/2024 |
3.24.1018.204 | 115 | 10/18/2024 |
3.24.1018.175 | 101 | 10/18/2024 |
3.24.1018.166 | 97 | 10/18/2024 |
3.24.1018.93 | 104 | 10/18/2024 |
3.24.1017.42 | 59 | 10/16/2024 |
3.24.1016.161 | 61 | 10/16/2024 |
3.24.1015.231 | 56 | 10/15/2024 |
3.24.1015.14 | 57 | 10/14/2024 |
3.24.1012.114 | 60 | 10/12/2024 |
3.24.1009.115 | 62 | 10/9/2024 |
3.24.1008.160 | 50 | 10/8/2024 |
3.24.1008.133 | 56 | 10/8/2024 |
3.24.1007.185 | 57 | 10/7/2024 |
3.24.1003.33 | 62 | 10/2/2024 |
3.24.1002.162 | 63 | 10/2/2024 |
3.24.929.143 | 65 | 9/29/2024 |
3.24.929.141 | 55 | 9/29/2024 |
3.24.929.131 | 55 | 9/29/2024 |
3.24.929.122 | 62 | 9/29/2024 |
3.24.926.184 | 67 | 9/26/2024 |
3.24.926.182 | 58 | 9/26/2024 |
3.24.926.175 | 63 | 9/26/2024 |
3.24.924.160 | 66 | 9/24/2024 |
3.24.924.133 | 66 | 9/24/2024 |
3.24.924.124 | 56 | 9/24/2024 |
3.24.924.10 | 65 | 9/23/2024 |
3.24.924.1 | 59 | 9/23/2024 |
3.24.923.234 | 57 | 9/23/2024 |
3.24.923.232 | 58 | 9/23/2024 |
3.24.923.155 | 58 | 9/23/2024 |
3.24.919.92 | 73 | 9/19/2024 |
3.24.914.125 | 80 | 9/14/2024 |
3.24.914.115 | 64 | 9/14/2024 |
3.24.914.111 | 62 | 9/14/2024 |
3.24.911.95 | 116 | 9/11/2024 |
3.24.908.215 | 121 | 9/8/2024 |
3.24.904.200 | 126 | 9/4/2024 |
3.24.828.163 | 113 | 8/28/2024 |
3.24.820.173 | 119 | 8/20/2024 |
3.24.814.92 | 112 | 8/14/2024 |
3.24.812.115 | 118 | 8/12/2024 |
3.24.802.100 | 88 | 8/2/2024 |
3.24.801.162 | 98 | 8/1/2024 |
3.24.801.160 | 110 | 8/1/2024 |
3.24.730.164 | 66 | 7/30/2024 |
3.24.730.91 | 76 | 7/30/2024 |
3.24.724.91 | 100 | 7/24/2024 |
3.24.718.105 | 116 | 7/18/2024 |
3.24.716.95 | 107 | 7/16/2024 |
3.24.712.94 | 101 | 7/12/2024 |
3.24.710.14 | 108 | 7/9/2024 |
3.24.709.105 | 110 | 7/9/2024 |
3.24.704.94 | 119 | 7/4/2024 |
3.24.701.90 | 119 | 7/1/2024 |
3.24.628.114 | 118 | 6/28/2024 |
3.24.627.145 | 101 | 6/27/2024 |
3.24.620.160 | 108 | 6/20/2024 |
3.24.613.115 | 103 | 6/13/2024 |
3.24.612.95 | 98 | 6/12/2024 |
3.24.528.90 | 114 | 5/28/2024 |
3.24.522.84 | 79 | 5/22/2024 |
3.24.512.213 | 84 | 5/12/2024 |
3.24.508.112 | 126 | 5/8/2024 |
2.2024.428.71 | 126 | 4/28/2024 |
2.2024.427.1128 | 111 | 4/27/2024 |
2.2.72 | 132 | 4/14/2024 |
2.2.71 | 115 | 4/12/2024 |
2.2.8 | 103 | 4/26/2024 |
2.2.6 | 112 | 4/10/2024 |
2.2.5 | 126 | 3/26/2024 |
2.2.4 | 255 | 3/25/2024 |
2.2.3 | 126 | 3/24/2024 |
2.2.2 | 113 | 3/21/2024 |
2.2.1 | 125 | 3/20/2024 |
2.2.0 | 128 | 3/13/2024 |
2.1.9 | 131 | 2/21/2024 |
2.1.8 | 128 | 2/18/2024 |
2.1.7 | 110 | 2/16/2024 |
2.1.6 | 120 | 2/14/2024 |
2.1.5 | 120 | 2/14/2024 |
2.1.4 | 126 | 2/9/2024 |
2.1.3 | 125 | 2/8/2024 |
2.1.2 | 86 | 2/5/2024 |
2.1.1.2 | 205 | 12/26/2023 |
2.1.1.1 | 124 | 12/26/2023 |
2.1.1 | 128 | 12/25/2023 |
2.1.0 | 134 | 12/17/2023 |
2.0.11 | 159 | 12/6/2023 |
2.0.1 | 143 | 11/15/2023 |
2.0.0 | 136 | 11/14/2023 |
1.9.1 | 137 | 11/1/2023 |
1.9.0 | 130 | 10/19/2023 |
1.9.0-preview2 | 110 | 10/12/2023 |
1.9.0-preview1 | 99 | 10/12/2023 |
1.8.9 | 150 | 10/11/2023 |
1.8.8 | 136 | 10/11/2023 |
1.8.7-rc2 | 105 | 9/21/2023 |
1.8.7-rc1 | 106 | 9/12/2023 |
1.8.6 | 332 | 8/31/2023 |
1.8.5 | 138 | 8/25/2023 |
1.8.4 | 145 | 8/24/2023 |
1.8.3 | 144 | 8/23/2023 |
1.8.2 | 146 | 8/22/2023 |
1.8.1 | 136 | 8/18/2023 |
1.8.0 | 172 | 8/15/2023 |
1.7.9 | 163 | 8/11/2023 |
1.7.8 | 162 | 8/11/2023 |
1.7.7 | 153 | 8/10/2023 |
1.7.6 | 157 | 8/9/2023 |
1.7.5 | 152 | 8/9/2023 |
1.7.4 | 168 | 8/3/2023 |
1.7.3 | 137 | 8/1/2023 |
1.7.2 | 144 | 7/31/2023 |
1.7.1 | 132 | 7/27/2023 |
1.7.0 | 164 | 7/25/2023 |
1.6.9 | 164 | 7/25/2023 |
1.6.8 | 159 | 7/24/2023 |
1.6.7 | 157 | 7/20/2023 |
1.6.6 | 147 | 7/19/2023 |
1.6.5 | 128 | 7/19/2023 |
1.6.4 | 155 | 7/17/2023 |
1.6.3 | 149 | 7/17/2023 |
1.6.2 | 148 | 7/12/2023 |
1.6.1 | 155 | 6/30/2023 |
1.6.0 | 150 | 6/26/2023 |
1.5.9 | 143 | 6/22/2023 |
1.5.8 | 158 | 6/15/2023 |
1.5.7.1 | 166 | 6/14/2023 |
1.5.7 | 154 | 6/14/2023 |
1.5.6.2 | 167 | 6/7/2023 |
1.5.6.1 | 172 | 6/7/2023 |
1.5.6 | 157 | 6/7/2023 |
1.5.5.2 | 144 | 5/26/2023 |
1.5.5.1 | 154 | 5/26/2023 |
1.5.5 | 160 | 5/26/2023 |
1.5.4.4 | 168 | 5/25/2023 |
1.5.4.3 | 159 | 5/23/2023 |
1.5.4.2 | 167 | 5/17/2023 |
1.5.4.1 | 159 | 5/16/2023 |
1.5.4 | 175 | 5/11/2023 |
1.5.3 | 172 | 5/11/2023 |
1.5.2 | 172 | 5/10/2023 |
1.5.1 | 165 | 5/10/2023 |
1.5.0 | 176 | 5/6/2023 |
1.4.0 | 157 | 5/5/2023 |
1.3.9 | 190 | 4/23/2023 |
1.3.8.6 | 190 | 4/23/2023 |
1.3.8.5 | 187 | 4/21/2023 |
1.3.8.1 | 197 | 4/12/2023 |
1.3.8 | 198 | 4/11/2023 |
1.3.7 | 200 | 4/9/2023 |
1.3.6.3 | 225 | 4/1/2023 |
1.3.6.2 | 213 | 3/31/2023 |
1.3.6.1 | 215 | 3/31/2023 |
1.3.6 | 216 | 3/31/2023 |
1.3.5 | 198 | 3/30/2023 |
1.3.4.1 | 239 | 3/29/2023 |
1.3.4 | 207 | 3/28/2023 |
1.3.3 | 223 | 3/28/2023 |
1.3.2 | 225 | 3/26/2023 |
1.3.1 | 237 | 3/22/2023 |
1.3.0 | 227 | 3/21/2023 |
1.2.0 | 240 | 3/21/2023 |
1.1.0 | 247 | 3/17/2023 |
1.0.9 | 242 | 3/16/2023 |