TrustyPay.Crypto 0.0.13
TrustyPay.Crypto
What is TrustyPay.Crypto?
TrustyPay.Crypto is a library about cryptographic algorithm, which includes RSA/SM2 and AES/SM4. In addition, it defines a http client abstract class which provides GetAsync and PostAsync methods. These methods may wrap request or response with different encryption or signature simply. Some libraries, such as TrustyPay.Crypto.Banks.ICBC, TrustyPay.Crypto.Banks.CMB, have extended the abstract class to meet the requirements of bank api.
Installing
using .net cli:
dotnet add package TrustyPay.Crypto
import namespace:
using TrustyPay.Crypto;
How to use
AES class
- Secret key generation
// generate 128 bit secret key. var secretKey = AES.GenerateSecretKey(KeySize.AES128); - Encryption
var aes = new AES(secretKey.Key, secretKey.IV); var cipherText = aes.Encrypt("hello_world!!!".FromUTF8String()).ToBase64(); - Decryption
var aes = new AES(secretKey.Key, secretKey.IV); var plainText = aes.Decrypt("<the above cipher text>".FromBase64()).ToUTF8String();
- Secret key generation
SM4 class
- Secret key generation
// generate 128 bit secret key. var secretKey = SM4.GenerateSecretKey(); - Encryption
var sm4 = new SM4(secretKey.Key, secretKey.IV); var cipherText = sm4.Encrypt( "hello_world!!!".FromUTF8String()).ToBase64(); - Decryption
var sm4 = new SM4(secretKey.Key, secretKey.IV); var plainText = sm4.Decrypt( "<the above cipher text>".FromBase64()).ToUTF8String();
- Secret key generation
RSA class
Private/Public key generation
// key size is 2048 var keyPair = RSA.GenerateKeyPair( RSA.PrivateKey.PrivateKeyFormat.Pkcs1, RSA.KeySize.RSA2048);Encryption
using System.Security.Cryptography; var rsa = new RSA(keyPair, RSAEncryptionPadding.Pkcs1); var cipherText = rsa.Encrypt( "hello_world!!!".FromUTF8String()).ToBase64();Decryption
using System.Security.Cryptography; var rsa = new RSA(keyPair, RSAEncryptionPadding.Pkcs1); var plainText = rsa.Decrypt( "<the above cipher text>".FromBase64()).ToUTF8String();Sign
using System.Security.Cryptography; var rsa = new RSA(keyPair, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var signatureText = rsa.Sign( "hello_world!!!".FromUTF8String()).ToBase64();Verification
using System.Security.Cryptography; var rsa = new RSA(keyPair, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var ok = rsa.Verify( "hello_world!!!".FromUTF8String(), "<the above signature text>".FromBase64());
SM2 class
Private/Public key generation
var keyPair = SM2.GenerateKeyPair();Encryption
var sm2 = new SM2(keyPair, CipherFormat.C1C2C3); var cipherText = sm2.Encrypt( "hello_world!!!".FromUTF8String()).ToBase64();Decryption
var sm2 = new SM2(keyPair, CipherFormat.C1C2C3); var plainText = sm2.Decrypt( "<the above cipher text>".FromBase64()).ToUTF8String();Sign
var sm2 = new SM2(keyPair); var signatureText = sm2.Sign( "hello_world!!!".FromUTF8String()).ToBase64();Verification
var rsa = new SM2(keyPair); var ok = sm2.Verify( "hello_world!!!".FromUTF8String(), "<the above signature text>".FromBase64());
No packages depend on TrustyPay.Crypto.
.NET Core 3.1
- BouncyCastle.NetCore (>= 1.9.0)
.NET 5.0
- BouncyCastle.NetCore (>= 1.9.0)
.NET 6.0
- BouncyCastle.NetCore (>= 1.9.0)