Skip to content
NETCore encrypt and decrpty tool,Include aes,des,rsa,md5,sha1,sha256,sha384,sha512
C#
Branch: master
Clone or download
Latest commit 7d2db6c Aug 30, 2019

README.md

NETCore.Encrypt 中文文档

NuGet NETCore CLR NetStandard 2.0.3 license

NETCore encrypt and decrpty tool,Include AES,RSA,MD5,SAH1,SAH256,SHA384,SHA512 and more

To install NETCore.Encrypt, run the following command in the Package Manager Console

Package Manager

Install-Package NETCore.Encrypt -Version 2.0.8

.NET CLI

dotnet add package NETCore.Encrypt --version 2.0.8

PackageReference

<PackageReference Include="NETCore.Encrypt" Version="2.0.8" />

Easy to use with EncryptProvider

AES

Create AES Key

var aesKey = EncryptProvider.CreateAesKey();

var key = aesKey.Key;
var iv = aesKey.IV;

AES encrypt

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

ASE decrypt

  • 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

  • Create DES Key

    //des key length is 24 bit
    var desKey = EncryptProvider.CreateDesKey();
    
  • Create DES Iv 【NEW】【PRE】

    //des iv length is 8 bit
    var desIv = EncryptProvider.CreateDesIv();
    
  • DES encrypt (ECB mode)

    var srcString = "des encrypt";
    var encrypted = EncryptProvider.DESEncrypt(srcString, key);
  • DES encrypt bytes (ECB mode)

    var srcBytes =  new byte[]{xxx};
    var decryptedBytes = EncryptProvider.DESEncrypt(srcBytes, key);
  • DES decrypt (ECB mode)

    var encryptedStr = "xxxx";
    var decrypted = EncryptProvider.DESDecrypt(encryptedStr, key);
  • DES decrypt bytes (ECB mode)

    var encryptedBytes =  new byte[]{xxx};
    var decryptedBytes = EncryptProvider.DESDecrypt(encryptedBytes, key);
  • DES encrypt bytes with iv (CBC mode)【NEW】【PRE】

    var srcBytes =  new byte[]{xxx};
    var encrypted = EncryptProvider.DESEncrypt(srcBytes, key, iv);
  • DES decrypt bytes with iv (CBC mode)【NEW】【PRE】

    var encryptedBytes =  new byte[]{xxx};
    var encrypted = EncryptProvider.DESDecrypt(encryptedBytes, key, iv);

RSA

  • Enum RsaSize

    public enum RsaSize
    {
        R2048=2048,
        R3072=3072,
        R4096=4096
    }
  • Create RSA Key with RsaSize

    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 and Verify method 【NEW】

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

    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 decrypt

    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 from string

    var privateKey = rsaKey.PrivateKey;
    RSA rsa = EncryptProvider.RSAFromString(privateKey);
  • RSA with PEM 【NEW】

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

MD5

var srcString = "Md5 hash";
var hashed = EncryptProvider.Md5(srcString);
var srcString = "Md5 hash";
var hashed = EncryptProvider.Md5(srcString, MD5Length.L16);

SHA

  • SHA1

    var srcString = "sha hash";    
    var hashed = EncryptProvider.Sha1(srcString); 
  • SHA256

    var srcString = "sha hash";    
    var hashed = EncryptProvider.Sha256(srcString); 
  • SHA384

    var srcString = "sha hash";    
    var hashed = EncryptProvider.Sha384(srcString); 
  • SHA512

    var srcString = "sha hash";    
    var hashed = EncryptProvider.Sha512(srcString);

HMAC

  • HMAC-MD5

    var key="xxx";
    var srcString = "hmac md5 hash";     
    var hashed = EncryptProvider.HMACMD5(srcString,key);
  • HMAC-SHA1

    var key="xxx";
    var srcString = "hmac sha hash";    
    var hashed = EncryptProvider.HMACSHA1(srcString,key);
  • HMAC-SHA256

    var key="xxx";
    var srcString = "hmac sha hash";    
    var hashed = EncryptProvider.HMACSHA256(srcString,key);
  • HMAC-SHA384

    var key="xxx";
    var srcString = "hmac sha hash";    
    var hashed = EncryptProvider.HMACSHA384(srcString,key);
  • HMAC-SHA512

    var key="xxx";
    var srcString = "hmac sha hash";    
    var hashed = EncryptProvider.HMACSHA512(srcStringkey);

Base64

  • Base64Encrypt

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

    var encryptedStr = "xxxxx";    
    var strValue = EncryptProvider.Base64Decrypt(encryptedStr);   //default encoding is UTF-8
    var encryptedStr = "xxxxx";    
    var strValue = EncryptProvider.Base64Decrypt(encryptedStr,Encoding.ASCII); 

Easy to use hash with EncryptExtensions

MD5 Extensions

  • String to MD5

var hashed="some string".MD5();

SHA Extensions

  • String to SHA1

var hashed="some string".SHA1();

Tips:SHA256,SHA384,SHA512 the same usage like SHA1

HMACSHA Extensions

  • String to HMACSHA1

  var key="xxx";
  var hashed="some string".HMACSHA1(key);

Tips:HMACSHA256,HMACSHA384,HMACSHA512 the same usage like HMACSHA1

LICENSE

MIT License

You can’t perform that action at this time.