一个常用加密算法的封装库,AES,DES,RC4,TripleDES,RSA,SM2,SM3,SM4,RIPEMD
$ dotnet add package EasilyNET.Security常用加密/哈希算法封装,降低使用复杂度,面向 .NET 高性能与易用性。
说明:本库不“重写算法”,而是对 .NET/BouncyCastle 做轻量封装。
若你使用了非 UTF-8 的旧编码(如 GBK),可能需要注册 CodePages 编码提供器:
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
// AES (注意:本库会对密钥做 hash 处理,密文仅与本库兼容)
var cipher = AesCrypt.EncryptToBase64("hello", "pwd", AesKeyModel.AES256);
var plain = AesCrypt.DecryptFromBase64(cipher, "pwd", AesKeyModel.AES256);
AesKeyModel)var cipher = Rc4Crypt.EncryptToBase64("hello", "pwd");
var plain = Rc4Crypt.DecryptFromBase64(cipher, "pwd");
RsaCrypt.GenerateKey(ERsaKeyLength)OaepSHA256(推荐)Encrypt/Decrypt 带 out 重载SHA256 + Pkcs1var key = RsaCrypt.GenerateKey(ERsaKeyLength.Bit2048);
RsaCrypt.Encrypt(key.PublicKey, "hello"u8.ToArray(), out var secret);
RsaCrypt.Decrypt(key.PrivateKey, secret, out var plainBytes);
var sign = RsaCrypt.Signature(key.PrivateKey, "hello"u8.ToArray());
var ok = RsaCrypt.Verification(key.PublicKey, "hello"u8.ToArray(), sign);
var pemPri = RsaCrypt.ExportPrivateKeyToPem(key.PrivateKey);
var pemPub = RsaCrypt.ExportPublicKeyToPem(key.PublicKey);
Sm2Crypt.GenerateKey(out var pub, out var pri);
var cipher = Sm2Crypt.Encrypt(pub, "hello"u8.ToArray());
var plain = Sm2Crypt.Decrypt(pri, cipher);
var sig = Sm2Crypt.Signature(pri, "hello"u8.ToArray());
var ok = Sm2Crypt.Verify(pub, "hello"u8.ToArray(), sig);
var hex = Sm3Signature.HashToHex("hello", upperCase: true);
var base64 = Sm3Signature.HashToBase64("hello");
hexString=true 表示 key/iv 为 16 进制字符串var cipherHex = Sm4Crypt.EncryptECBToHex("701d1cc0cfbe7ee11824df718855c0c6", true, "hello");
var plain = Sm4Crypt.DecryptECBFromHex("701d1cc0cfbe7ee11824df718855c0c6", true, cipherHex);
var cipherBase64 = Sm4Crypt.EncryptCBCToBase64("701d1cc0cfbe7ee11824df718855c0c6", true, "701d1cc0cfbe7ee11824df718855c0c5", "hello");
var plain2 = Sm4Crypt.DecryptCBCFromBase64("701d1cc0cfbe7ee11824df718855c0c6", true, "701d1cc0cfbe7ee11824df718855c0c5", cipherBase64);
支持 128/160/256/320 变体:RipeMD128/160/256/320,提供 Hash/HashToHex/HashToBase64。