TrustyPay.Crypto
0.0.11
See the version list below for details.
dotnet add package TrustyPay.Crypto --version 0.0.11
NuGet\Install-Package TrustyPay.Crypto -Version 0.0.11
<PackageReference Include="TrustyPay.Crypto" Version="0.0.11" />
paket add TrustyPay.Crypto --version 0.0.11
#r "nuget: TrustyPay.Crypto, 0.0.11"
// Install TrustyPay.Crypto as a Cake Addin #addin nuget:?package=TrustyPay.Crypto&version=0.0.11 // Install TrustyPay.Crypto as a Cake Tool #tool nuget:?package=TrustyPay.Crypto&version=0.0.11
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());
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- BouncyCastle.NetCore (>= 1.9.0)
-
net5.0
- BouncyCastle.NetCore (>= 1.9.0)
-
net6.0
- BouncyCastle.NetCore (>= 1.9.0)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on TrustyPay.Crypto:
Package | Downloads |
---|---|
TrustyPay.Crypto.Banks.CMB
This is a http client for bank 'CMB' interface with SM2/SM4. |
|
TrustyPay.Crypto.Banks.ICBC
This is a http client for bank 'ICBC' interface with RSA2. |
|
TrustyPay.Crypto.Sdks.DotNet
This is an dotnet client. |
|
TrustyPay.Crypto.Authentication
This is an aspnet core middleware for authentication. |
GitHub repositories
This package is not used by any popular GitHub repositories.