Common

UUID

HTTP Request

Cryptography

Common

Reflection

IO

WeiXinCrypto Tutorial

This tutorial provides an overview of the WeiXinCrypto class, which is part of the lightweight-component/aj-util library. The WeiXinCrypto class provides encryption and decryption utilities for WeChat/Weixin related operations.

Introduction

The WeiXinCrypto class contains static methods for handling WeChat/Weixin encryption and decryption operations, including:

Main Features

Methods

1. AES Decryption

  1. aesDecryptToString(byte[] aesKey, byte[] associatedData, byte[] nonce, String cipherText) - AEAD_AES_256_GCM decryption
  2. aesDecryptPhone(String iv, String cipherText, String sessionKey) - Mini-program phone number decryption

2. RSA Encryption/Decryption

  1. encryptOAEP(String message, X509Certificate certificate) - RSA-OAEP encryption
  2. decryptOAEP(String cipherText, PrivateKey privateKey) - RSA-OAEP decryption
  3. rsaEncrypt(String message, String certPath) - RSA encryption with certificate file

3. Signatures

  1. rsaSign(PrivateKey privateKey, byte[] data) - RSA signature generation

Usage Examples

AES Decryption

String decrypted = WeiXinCrypto.aesDecryptToString(
    apiKey.getBytes(), 
    associatedData.getBytes(),
    nonce.getBytes(),
    cipherText
);

String phone = WeiXinCrypto.aesDecryptPhone(iv, cipherText, sessionKey);

RSA Encryption

String encrypted = WeiXinCrypto.encryptOAEP(message, certificate);
String encryptedFromFile = WeiXinCrypto.rsaEncrypt(message, "path/to/cert.pem");

RSA Decryption

String decrypted = WeiXinCrypto.decryptOAEP(cipherText, privateKey);

Signature Generation

String signature = WeiXinCrypto.rsaSign(privateKey, data.getBytes());

Implementation Notes

Conclusion

The WeiXinCrypto class provides comprehensive utilities for handling WeChat/Weixin related cryptographic operations, making integration with WeChat services more convenient.