TrustyPay.Crypto 0.0.13

TrustyPay.Crypto

Nuget

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

    1. Secret key generation
      // generate 128 bit secret key.
      var secretKey = AES.GenerateSecretKey(KeySize.AES128);
      
    2. Encryption
      var aes = new AES(secretKey.Key, secretKey.IV);
      var cipherText = aes.Encrypt("hello_world!!!".FromUTF8String()).ToBase64();
      
    3. Decryption
      var aes = new AES(secretKey.Key, secretKey.IV);
      var plainText = aes.Decrypt("<the above cipher text>".FromBase64()).ToUTF8String();
      
  • SM4 class

    1. Secret key generation
      // generate 128 bit secret key.
      var secretKey = SM4.GenerateSecretKey();
      
    2. Encryption
      var sm4 = new SM4(secretKey.Key, secretKey.IV);
      var cipherText = sm4.Encrypt(
          "hello_world!!!".FromUTF8String()).ToBase64();
      
    3. Decryption
      var sm4 = new SM4(secretKey.Key, secretKey.IV);
      var plainText = sm4.Decrypt(
          "<the above cipher text>".FromBase64()).ToUTF8String();
      
  • RSA class

    1. Private/Public key generation

      // key size is 2048
      var keyPair = RSA.GenerateKeyPair(
          RSA.PrivateKey.PrivateKeyFormat.Pkcs1, RSA.KeySize.RSA2048);
      
    2. Encryption

      using System.Security.Cryptography;
      
      var rsa = new RSA(keyPair, RSAEncryptionPadding.Pkcs1);
      var cipherText = rsa.Encrypt(
          "hello_world!!!".FromUTF8String()).ToBase64();
      
    3. Decryption

      using System.Security.Cryptography;
      
      var rsa = new RSA(keyPair, RSAEncryptionPadding.Pkcs1);
      var plainText = rsa.Decrypt(
          "<the above cipher text>".FromBase64()).ToUTF8String();
      
    4. Sign

      using System.Security.Cryptography;
      
      var rsa = new RSA(keyPair, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
      var signatureText = rsa.Sign(
          "hello_world!!!".FromUTF8String()).ToBase64();
      
    5. 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

    1. Private/Public key generation

      var keyPair = SM2.GenerateKeyPair();
      
    2. Encryption

      var sm2 = new SM2(keyPair, CipherFormat.C1C2C3);
      var cipherText = sm2.Encrypt(
          "hello_world!!!".FromUTF8String()).ToBase64();
      
    3. Decryption

      var sm2 = new SM2(keyPair, CipherFormat.C1C2C3);
      var plainText = sm2.Decrypt(
          "<the above cipher text>".FromBase64()).ToUTF8String();
      
    4. Sign

      var sm2 = new SM2(keyPair);
      var signatureText = sm2.Sign(
          "hello_world!!!".FromUTF8String()).ToBase64();
      
    5. 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

.NET 5.0

.NET 6.0

Version Downloads Last updated
0.0.13 1 2025/10/14
0.0.12 0 2023/2/25
0.0.11 0 2022/10/8
0.0.10 0 2022/9/28
0.0.9 0 2022/9/26
0.0.8 0 2022/7/21
0.0.7 0 2022/7/20
0.0.6 0 2022/7/15
0.0.4 0 2022/6/25
0.0.3 0 2022/6/10
0.0.2 0 2022/6/6
0.0.1 0 2022/6/1