在日常开发过程中,不可避免的涉及到数据加密解密(Hash)操作,所以就有想法开发通用工具,NETCore.Encrypt就诞生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,RSA加密解密,MD5,SHA*,HMAC*等常用Hash操作。
https://github.com/myloveCc/NETCore.Encrypt
Install-Package NETCore.Encrypt -Version 2.0.1
class="brush:csharp;gutter:true;">var aseKey = EncryptProvider.CreateAesKey(); var key = aesKey.Key; var iv = aesKey.IV;
var srcString = "aes encrypt"; //不带加密向量 var encrypted = EncryptProvider.AESEncrypt(srcString, key);
var srcString = "aes encrypt"; //带加密向量 var encrypted = EncryptProvider.AESEncrypt(srcString, key, iv);
var encryptedStr = "xxxx"; //不带加密向量(与加密方式对应) var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
var encryptedStr = "xxxx"; //带加密向量 var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
public enum RsaSize { R2048=2048, R3072=3072, R4096=4096 }
var rsaKey = EncryptProvider.CreateRsaKey(); //default is 2048 // var rsaKey = EncryptProvider.CreateRsaKey(RsaSize.R3072); var publicKey = rsaKey.PublicKey; var privateKey = rsaKey.PrivateKey; var exponent = rsaKey.Exponent; var modulus = rsaKey.Modulus;
var publicKey = rsaKey.PublicKey; var srcString = "rsa encrypt"; var encrypted = EncryptProvider.RSAEncrypt(publicKey, srcString);
var privateKey = rsaKey.PrivateKey; var encryptedStr = "xxxx"; var decrypted = EncryptProvider.RSADecrypt(privateKey, encryptedStr);
var privateKey = rsaKey.PrivateKey; RSA rsa = EncryptProvider.RSAFromString(privateKey);
var srcString = "Md5 hash"; var hashed = EncryptProvider.Md5(srcString);
var srcString = "sha hash"; //SHA1 var hashed = EncryptProvider.Sha1(srcString); //SHA256 var hashed = EncryptProvider.Sha256(srcString); //SHA384 var hashed = EncryptProvider.Sha384(srcString); //SHA512 var hashed = EncryptProvider.Sha512(srcString);
var key="xxx"; var srcString = "hmac hash"; //MD5 var hashed = EncryptProvider.HMACMD5(srcString,key); //HMACSHA1 var hashed = EncryptProvider.HMACSHA1(srcString,key); //HMACSHA256 var hashed = EncryptProvider.HMACSHA256(srcString,key); //HMACSHA384 var hashed = EncryptProvider.HMACSHA384(srcString,key); //HMACSHA512 var hashed = EncryptProvider.HMACSHA512(srcString,key);
var srcString = "base64 string"; var hashed = EncryptProvider.Base64Encrypt(srcString); //default encoding is UTF-8 //Encoding.ASCII var hashed = EncryptProvider.Base64Encrypt(srcString,Encoding.ASCII);
var encryptedStr = "xxxxx"; var strValue = EncryptProvider.Base64Decrypt(encryptedStr); //default encoding is UTF-8 //Encoding.ASCII(需要与加密编码对应) var strValue = EncryptProvider.Base64Decrypt(encryptedStr,Encoding.ASCII);
using NETCore.Encrypt.Extensions;
var hashed="some string".MD5();
var hashed="some string".SHA1();
var hashed="some string".SHA256();
var hashed="some string".SHA384();
var hashed="some string".SHA512();
var key="xxx";
var hashed="some string".HMACMD5(key);
var hashed="some string".HMACSHA1(key);
var hashed="some string".HMACSHA256(key);
var hashed="some string".HMACSHA384(key);
var hashed="some string".HMACSHA512(key);
很简单的一个工具,如果大家有新的需求,可以提issue,你也可以参与到项目中,让这个工具更加完善,希望对大家有帮助。