在日常開發過程中,不可避免的涉及到數據加密解密(Hash)操作,所以就有想法開發通用工具,NETCore.Encrypt就誕生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,RSA加密解密,MD5,SHA*,HMAC*等常用Hash操作。 ...
前言
在日常開發過程中,不可避免的涉及到數據加密解密(Hash)操作,所以就有想法開發通用工具,NETCore.Encrypt就誕生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,RSA加密解密,MD5,SHA*,HMAC*等常用Hash操作。
項目地址
https://github.com/myloveCc/NETCore.Encrypt
通過Nuget安裝
Install-Package NETCore.Encrypt -Version 2.0.1
AES 操作
創建AES 秘鑰
var aseKey = EncryptProvider.CreateAesKey(); var key = aesKey.Key; var iv = aesKey.IV;
AES 加密
var srcString = "aes encrypt"; //不帶加密向量 var encrypted = EncryptProvider.AESEncrypt(srcString, key);
var srcString = "aes encrypt"; //帶加密向量 var encrypted = EncryptProvider.AESEncrypt(srcString, key, iv);
AES 解密
var encryptedStr = "xxxx"; //不帶加密向量(與加密方式對應) var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
var encryptedStr = "xxxx"; //帶加密向量 var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
RSA 操作
RSA秘鑰長度枚舉
public enum RsaSize { R2048=2048, R3072=3072, R4096=4096 }
創建RSA秘鑰
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;
RSA加密
var publicKey = rsaKey.PublicKey; var srcString = "rsa encrypt"; var encrypted = EncryptProvider.RSAEncrypt(publicKey, srcString);
RSA解密
var privateKey = rsaKey.PrivateKey; var encryptedStr = "xxxx"; var decrypted = EncryptProvider.RSADecrypt(privateKey, encryptedStr);
通過RSA秘鑰創建RSA
var privateKey = rsaKey.PrivateKey; RSA rsa = EncryptProvider.RSAFromString(privateKey);
HASH 操作
MD5
var srcString = "Md5 hash"; var hashed = EncryptProvider.Md5(srcString);
SHA
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);
HMAC
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);
BASE64 操作
Base64加密
var srcString = "base64 string"; var hashed = EncryptProvider.Base64Encrypt(srcString); //default encoding is UTF-8 //Encoding.ASCII var hashed = EncryptProvider.Base64Encrypt(srcString,Encoding.ASCII);
Base64解密
var encryptedStr = "xxxxx"; var strValue = EncryptProvider.Base64Decrypt(encryptedStr); //default encoding is UTF-8 //Encoding.ASCII(需要與加密編碼對應) var strValue = EncryptProvider.Base64Decrypt(encryptedStr,Encoding.ASCII);
EncryptExtensions擴展
引用命名空間
using NETCore.Encrypt.Extensions;
MD5
var hashed="some string".MD5();
SHA
var hashed="some string".SHA1();
var hashed="some string".SHA256();
var hashed="some string".SHA384();
var hashed="some string".SHA512();
HMAC
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);
Make .NET Great Again
很簡單的一個工具,如果大家有新的需求,可以提issue,你也可以參與到項目中,讓這個工具更加完善,希望對大家有幫助。