c. 解密操作指南

本指南详细说明了如何解密从 WaaS API 获取的加密响应数据。

i. 解密流程步骤说明

以下是解密操作的步骤说明:

  1. 从 API 响应中获取 加密数据
  2. 对数据进行 Base64 解码
  3. 使用 RSA公钥 进行解密
  4. 将解密结果解析为 业务对象

ii.解密代码实例

class DecryptionExample {
    public static void main(String[] args) {
        try {
            // ===================== 1. 获取API响应 =====================
            // 发送API请求并获取响应
            APIResult response = restTemplate.postForObject(
                "https://waas2-out-api.powersafe-rel.cc/api/v1/api/user/getUserAddress",
                httpEntity,
                APIResult.class
            );

            // 提取加密数据(确保响应格式与API一致)
            String encryptedData = response.getData();

            // ===================== 2. Base64解码 =====================
            // 使用Base64工具类进行解码(具体实现参考工具类)
            byte[] decodedBytes = Base64Util.decodeString(encryptedData);

            // ===================== 3. RSA公钥解密 =====================
            // 使用与请求相同的公钥
            String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ...";

            // 执行RSA解密
            byte[] decryptedBytes = RSAUtils.decryptByPublicKey(
                decodedBytes,
                publicKey
            );

            // ===================== 4. 解析业务对象 =====================
            // 将解密后的JSON数据转换为业务对象
            AddressVO address = JsonUtils.fromJson(
                new String(decryptedBytes, StandardCharsets.UTF_8),
                AddressVO.class
            );

            // ===================== 5. 使用解密数据 =====================
            System.out.println("解析得到的地址信息:");
            System.out.println("商户用户ID: " + address.getTenantUserId());
            System.out.println("链名称: " + address.getChainName());
            System.out.println("钱包地址: " + address.getAddress());
            System.out.println("标签: " + address.getTag());
            System.out.println("状态: " + address.getState());
            System.out.println("风险评分: " + address.getRiskScore());

        } catch (Exception e) {
            System.err.println("\n解密过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}