.NET Core 常用加密和Hash工具NETCore.Encrypt

前言

 在日常开发过程中,不可避免的涉及到数据加密解密(Hash)操作,所以就有想法开发通用工具,NETCore.Encrypt就诞生了。目前NETCore.Encrypt只支持.NET Core ,工具包含了AES,DES,RSA加密解密,MD5,SHA*,HMAC*等常用Hash操作。

项目地址

https://github.com/myloveCc/NETCore.Encrypt

通过Nuget安装

Install-Package NETCore.Encrypt -Version 2.0.8

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);

 

//对byte[] 进行aes加密(version 2.0.6)
var srcBytes = new bytes[]{xxxx};
//带加密向量
var encryptedBytes = EncryptProvider.AESEncrypt(srcBytes , key, iv);

  

AES 解密

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);

  

DES 操作

创建DES 秘钥

//秘钥长度为24
var desKey = EncryptProvider.CreateDesKey();

DES加密

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);

  

DES解密

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)

  

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加密 Sign 和 Verify 方法

string rawStr = "xxx";
string signStr = EncryptProvider.RSASign(rawStr, privateKey);
bool   result = EncryptProvider.RSAVerify(rawStr, signStr, publicKey); 

 RSA加密

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);

 RSA解密

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);

通过RSA秘钥创建RSA

var privateKey = rsaKey.PrivateKey;
RSA rsa = EncryptProvider.RSAFromString(privateKey);

RSA 和 PEM 相互转换

//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);

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,你也可以参与到项目中,让这个工具更加完善,希望对大家有帮助。

 

posted @ 2017-08-24 15:54  雨~桐  阅读(10099)  评论(8编辑  收藏  举报