NETCore encrypt and decrypt tool,Include AES,RSA,MD5,SAH1,SAH256,SHA384,SHA512 and more
To install NETCore.Encrypt, run the following command in the Package Manager Console
Install-Package NETCore.Encrypt -Version 2.1.1
dotnet add package NETCore.Encrypt --version 2.1.1
<PackageReference Include="NETCore.Encrypt" Version="2.1.1" />
EncryptProvider
var aesKey = EncryptProvider.CreateAesKey();
var key = aesKey.Key;
var iv = aesKey.IV;
AES encrypt without iv (ECB mode)
var srcString = "aes encrypt";
var encrypted = EncryptProvider.AESEncrypt(srcString, key);
AES encrypt with iv (CBC mode)
var srcString = "aes encrypt";
var encrypted = EncryptProvider.AESEncrypt(srcString, key, iv);
AES encrypt bytes with iv (CBC mode)
var srcBytes = new byte[]{xxx};
var encryptedBytes = EncryptProvider.AESEncrypt(srcBytes, key, iv);
AES decrypt without iv (ECB mode)
var encryptedStr = "xxxx";
var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
AES decrypt with iv (CBC mode)
var encryptedStr = "xxxx";
var decrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
AES decrypt bytes with iv (CBC mode)
var encryptedBytes = new byte[]{xxx};
var decryptedBytes = EncryptProvider.AESDecrypt(encryptedBytes, key, iv);
//des key length is 24 bit
var desKey = EncryptProvider.CreateDesKey();
//des iv length is 8 bit
var desIv = EncryptProvider.CreateDesIv();
var srcString = "des encrypt";
var encrypted = EncryptProvider.DESEncrypt(srcString, key);
var srcBytes = new byte[]{xxx};
var decryptedBytes = EncryptProvider.DESEncrypt(srcBytes, key);
var encryptedStr = "xxxx";
var decrypted = EncryptProvider.DESDecrypt(encryptedStr, key);
var encryptedBytes = new byte[]{xxx};
var decryptedBytes = EncryptProvider.DESDecrypt(encryptedBytes, key);
var srcBytes = new byte[]{xxx};
var encrypted = EncryptProvider.DESEncrypt(srcBytes, key, iv);
var encryptedBytes = new byte[]{xxx};
var encrypted = EncryptProvider.DESDecrypt(encryptedBytes, 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;
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 pem
var pkcs1KeyTuple = EncryptProvider.RSAToPem(false);
var publicPem = pkcs1KeyTuple.publicPem;
var privatePem = pkcs1KeyTuple.privatePem;
//PKCS8 pem
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);
//Rsa to pkcs1 format key
//PKCS1
var pkcs1KeyTuple = EncryptProvider.RsaToPkcs1();
var publicPkcs1 = pkcs1KeyTuple.publicPkcs1;
var privatePkcs1 = pkcs1KeyTuple.privatePkcs1;
//Rsa to pkcs8 format key
//PKCS8
var pkcs8KeyTuple = EncryptProvider.RsaToPkcs8();
var publicPkcs8 = pkcs1KeyTuple.publicPkcs8;
var privatePkcs8 = pkcs1KeyTuple.privatePkcs8;
//Rsa from pkcs public key
var rsa = EncryptProvider.RSAFromPublicPkcs(pkcsPublicKey); // Pkcs #1 | Pkcs #8
rsa = EncryptProvider.RSAFromPrivatePkcs1(privatePkcs1);
rsa = EncryptProvider.RSAFromPrivatePkcs8(privatePkcs8);
//Rsa encrypt and decrypt with pkcs key
var srcString = "Md5 hash";
var hashed = EncryptProvider.Md5(srcString);
var srcString = "Md5 hash";
var hashed = EncryptProvider.Md5(srcString, MD5Length.L16);
var srcString = "sha hash";
var hashed = EncryptProvider.Sha1(srcString);
var srcString = "sha hash";
var hashed = EncryptProvider.Sha256(srcString);
var srcString = "sha hash";
var hashed = EncryptProvider.Sha384(srcString);
var srcString = "sha hash";
var hashed = EncryptProvider.Sha512(srcString);
var key="xxx";
var srcString = "hmac md5 hash";
var hashed = EncryptProvider.HMACMD5(srcString,key);
var key="xxx";
var srcString = "hmac sha hash";
var hashed = EncryptProvider.HMACSHA1(srcString,key);
var key="xxx";
var srcString = "hmac sha hash";
var hashed = EncryptProvider.HMACSHA256(srcString,key);
var key="xxx";
var srcString = "hmac sha hash";
var hashed = EncryptProvider.HMACSHA384(srcString,key);
var key="xxx";
var srcString = "hmac sha hash";
var hashed = EncryptProvider.HMACSHA512(srcString,key);
var srcString = "base64 string";
var hashed = EncryptProvider.Base64Encrypt(srcString); //default encoding is UTF-8
var srcString = "base64 string";
var hashed = EncryptProvider.Base64Encrypt(srcString,Encoding.ASCII);
var encryptedStr = "xxxxx";
var strValue = EncryptProvider.Base64Decrypt(encryptedStr); //default encoding is UTF-8
var encryptedStr = "xxxxx";
var strValue = EncryptProvider.Base64Decrypt(encryptedStr,Encoding.ASCII);
EncryptExtensions
var hashed="some string".MD5();
var hashed="some string".SHA1();
Tips:SHA256,SHA384,SHA512 the same usage like SHA1
var key="xxx";
var hashed="some string".HMACSHA1(key);
Tips:HMACSHA256,HMACSHA384,HMACSHA512 the same usage like HMACSHA1