https://www.cnblogs.com/piscesLoveCc/p/7423205.html
前言
在日常开发过程中,不可避免的涉及到数据加密解密(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.6
AES 操作
创建AES 秘钥
1
2
3
4
|
var
aseKey = EncryptProvider.CreateAesKey();
var
key = aesKey.Key;
var
iv = aesKey.IV;
|
AES 加密
1
2
3
|
var
srcString =
"aes encrypt"
;
//不带加密向量
var
encrypted = EncryptProvider.AESEncrypt(srcString, key);
|
1
2
3
|
var
srcString =
"aes encrypt"
;
//带加密向量
var
encrypted = EncryptProvider.AESEncrypt(srcString, key, iv);
|
1
2
3
4
|
//对byte[] 进行aes加密(version 2.0.6)
var
srcBytes =
new
bytes[]{xxxx};
//带加密向量
var
encryptedBytes = EncryptProvider.AESEncrypt(srcBytes , key, iv);
|
AES 解密
1
2
3
|
var
encryptedStr =
"xxxx"
;
//不带加密向量(与加密方式对应)
var
decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
|
1
2
3
|
var
encryptedStr =
"xxxx"
;
//带加密向量
var
decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
|
1
2
3
4
|
//对byte[] 进行aes解密(version 2.0.6)
var
encryptedBytes =
new
bytes[]{xxxx};
//带加密向量
var
encryptedBytes = EncryptProvider.AESDecrypt(encryptedBytes , key, iv);
|
DES 操作
创建DES 秘钥
1
2
|
//秘钥长度为24
var
desKey = EncryptProvider.CreateDesKey();
|
DES加密
1
2
|
var
srcString =
"des encrypt"
;
var
encrypted = EncryptProvider.DESEncrypt(srcString, key);
|
1
2
3
|
//对byte[] 进行des加密(version 2.0.6)
var
srcBytes =
new
bytes[]{xxxx};
var
encryptedBytes = EncryptProvider.DESEncrypt(srcBytes , key);
|
DES解密
1
2
|
var
encryptedStr =
"xxxx"
;
var
decrypted = EncryptProvider.DESDecrypt(encryptedStr, key);
|
1
2
3
|
//对byte[] 进行des解密(version 2.0.6)
var
encryptedBytes =
new
bytes[]{xxxx};
var
decryptedBytes = EncryptProvider.DESDecrypt(decryptedBytes , key)
|
RSA 操作
RSA秘钥长度枚举
1
2
3
4
5
6
|
public
enum
RsaSize
{
R2048=2048,
R3072=3072,
R4096=4096
}
|
创建RSA秘钥
1
2
3
4
5
6
7
8
|
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加密
1
2
3
4
5
6
7
|
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解密
1
2
3
4
5
6
|
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
1
2
|
var
privateKey = rsaKey.PrivateKey;
RSA rsa = EncryptProvider.RSAFromString(privateKey);
|
HASH 操作
MD5
1
2
|
var
srcString =
"Md5 hash"
;
var
hashed = EncryptProvider.Md5(srcString);
|
SHA
1
2
3
4
5
6
7
8
9
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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加密
1
2
3
4
5
|
var
srcString =
"base64 string"
;
var
hashed = EncryptProvider.Base64Encrypt(srcString);
//default encoding is UTF-8
//Encoding.ASCII
var
hashed = EncryptProvider.Base64Encrypt(srcString,Encoding.ASCII);
|
Base64解密
1
2
3
4
5
|
var
encryptedStr =
"xxxxx"
;
var
strValue = EncryptProvider.Base64Decrypt(encryptedStr);
//default encoding is UTF-8
//Encoding.ASCII(需要与加密编码对应)
var
strValue = EncryptProvider.Base64Decrypt(encryptedStr,Encoding.ASCII);
|
EncryptExtensions扩展
引用命名空间
1
|
using
NETCore.Encrypt.Extensions;
|
MD5
1
|
var
hashed=
"some string"
.MD5();
|
SHA
1
|
var
hashed=
"some string"
.SHA1();
|
1
|
var
hashed=
"some string"
.SHA256();
|
1
|
var
hashed=
"some string"
.SHA384();
|
1
|
var
hashed=
"some string"
.SHA512();
|
HMAC
1
|
var
key=
"xxx"
;
|
1
|
var
hashed=
"some string"
.HMACMD5(key);
|
1
|
var
hashed=
"some string"
.HMACSHA1(key);
|
1
|
var
hashed=
"some string"
.HMACSHA256(key);
|
1
|
var
hashed=
"some string"
.HMACSHA384(key);
|
1
|
var
hashed=
"some string"
.HMACSHA512(key);
|
Make .NET Great Again
很简单的一个工具,如果大家有新的需求,可以提issue,你也可以参与到项目中,让这个工具更加完善,希望对大家有帮助。