在平常開發過程當中,不可避免的涉及到數據加密解密(Hash)操做,因此就有想法開發通用工具,NETCore.Encrypt就誕生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,DES,RSA加密解密,MD5,SHA*,HMAC*等經常使用Hash操做。linux
https://github.com/myloveCc/NETCore.Encryptgit
Install-Package NETCore.Encrypt -Version 2.0.8
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);
//對byte[] 進行aes加密(version 2.0.6) var srcBytes = new bytes[]{xxxx}; //帶加密向量 var encryptedBytes = EncryptProvider.AESEncrypt(srcBytes , key, iv);
var encryptedStr = "xxxx"; //不帶加密向量(與加密方式對應) var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
var encryptedStr = "xxxx"; //帶加密向量 var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
//對byte[] 進行aes解密(version 2.0.6) var encryptedBytes = new bytes[]{xxxx}; //帶加密向量 var encryptedBytes = EncryptProvider.AESDecrypt(encryptedBytes , key, iv);
//祕鑰長度爲24 var desKey = EncryptProvider.CreateDesKey();
var srcString = "des encrypt"; var encrypted = EncryptProvider.DESEncrypt(srcString, key);
//對byte[] 進行des加密(version 2.0.6) var srcBytes = new bytes[]{xxxx}; var encryptedBytes = EncryptProvider.DESEncrypt(srcBytes , key);
var encryptedStr = "xxxx"; var decrypted = EncryptProvider.DESDecrypt(encryptedStr, key);
//對byte[] 進行des解密(version 2.0.6) var encryptedBytes = new bytes[]{xxxx}; var decryptedBytes = EncryptProvider.DESDecrypt(decryptedBytes , key)
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;
string rawStr = "xxx"; string signStr = EncryptProvider.RSASign(rawStr, privateKey); bool result = EncryptProvider.RSAVerify(rawStr, signStr, publicKey);
var publicKey = rsaKey.PublicKey; var srcString = "rsa encrypt"; var encrypted = EncryptProvider.RSAEncrypt(publicKey, srcString); //on mac/linux at version 2.0.5 var encrypted = EncryptProvider.RSAEncrypt(publicKey, srcString, RSAEncryptionPadding.Pkcs1);
var privateKey = rsaKey.PrivateKey; var encryptedStr = "xxxx"; var decrypted = EncryptProvider.RSADecrypt(privateKey, encryptedStr); //on mac/linux at version 2.0.5 var decrypted = EncryptProvider.RSADecrypt(privateKey, encryptedStr, RSAEncryptionPadding.Pkcs1);
var privateKey = rsaKey.PrivateKey; RSA rsa = EncryptProvider.RSAFromString(privateKey);
//Rsa to pem format key //PKCS1 var pkcs1KeyTuple = EncryptProvider.RSAToPem(false); var publicPem = pkcs1KeyTuple.publicPem; var privatePem = pkcs1KeyTuple.privatePem; //PKCS8 var pkcs8KeyTuple = EncryptProvider.RSAToPem(true); publicPem = pkcs8KeyTuple.publicPem; privatePem = pkcs8KeyTuple.privatePem; //Rsa from pem key var rsa = EncryptProvider.RSAFromPem(pemPublicKey); rsa = EncryptProvider.RSAFromPem(pemPrivateKey); //Rsa encrypt and decrypt with pem key var rawStr = "xxx"; var enctypedStr = EncryptProvider.RSAEncryptWithPem(pemPublicKey, rawStr); var decryptedStr = EncryptProvider.RSADecryptWithPem(pemPrivateKey, enctypedStr);
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,你也能夠參與到項目中,讓這個工具更加完善,但願對你們有幫助。github