UUID

HTTP 请求

加密/解密

Common 常用模块

Reflection 反射

Input/Output 输入/输出

CommonUtil 教程

本教程提供了 CommonUtil 类的概述,该类是 lightweight-component/aj-util 库的一部分。CommonUtil 类提供了整个库中使用的常见加密实用工具。

简介

CommonUtil 类包含用于各种加密操作的静态方法,包括:

主要特性

方法

1. 加密操作

  1. doCipher(String algorithmName, int mode, Key key, byte[] data) - 基本加密操作
  2. doCipher(String algorithmName, int mode, Key key, byte[] data, AlgorithmParameterSpec spec) - 带参数的加密
  3. doCipher(String algorithmName, int mode, byte[] keyData, AlgorithmParameterSpec spec, String cipherText, byte[] associatedData) - 带关联数据的 AES 加密

2. XOR 加密

  1. XOR_encode(String res, String key) - 简单 XOR 加密
  2. XOR_decode(String res, String key) - XOR 解密
  3. XOR(int res, String key) - 整数 XOR 操作

3. 密钥管理

  1. loadPrivateKeyByPath(String privateKeyPath) - 从文件加载私钥
  2. loadPrivateKey(String privateKey) - 从字符串加载私钥
  3. loadPrivateKey(InputStream inputStream) - 从流加载私钥

4. 证书处理

  1. deserializeToCerts(String apiV3Key, Map<String, Object> pMap) - 反序列化和解密证书

使用示例

加密操作

byte[] encrypted = CommonUtil.doCipher("AES/CBC/PKCS5Padding", 
    Cipher.ENCRYPT_MODE, key, data, ivSpec);

String decrypted = CommonUtil.doCipher("AES/GCM/NoPadding",
    Cipher.DECRYPT_MODE, keyBytes, gcmSpec, cipherText, aad);

XOR 加密

String encoded = CommonUtil.XOR_encode("secret", "password");
String decoded = CommonUtil.XOR_decode(encoded, "password");

密钥加载

PrivateKey key = CommonUtil.loadPrivateKeyByPath("keys/private.pem");
PrivateKey key2 = CommonUtil.loadPrivateKey(privateKeyString);

证书处理

Map<BigInteger, X509Certificate> certs = 
    CommonUtil.deserializeToCerts(apiV3Key, responseMap);

实现说明

结论

CommonUtil 类提供了基础的加密实用工具,被库中其他安全相关类使用,提供了一致的常见加密操作处理方式。