c. 解密操作指南
本指南详细说明了如何解密从 WaaS API 获取的加密响应数据。i. 解密流程步骤说明
以下是解密操作的步骤说明:- 从 API 响应中获取 加密数据
- 对数据进行 Base64 解码
- 使用 RSA公钥 进行解密
- 将解密结果解析为 业务对象
ii.解密代码实例
class DecryptionExample {
class AddressVO {
private String tenantUserId;
private String chainName;
private String address;
// TODO: Add getter and setter methods
}
public static void main(String[] args) {
try {
// ===================== 1. 获取API响应 =====================
// 发送API请求并获取响应
APIResult<String> 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());
} catch (Exception e) {
System.err.println("\n解密过程中发生错误: " + e.getMessage());
e.printStackTrace();
}
}
}
async function main() {
try {
// ===================== 1. 模拟获取API响应 =====================
// 假设已发送请求并获取到响应
const response = await axios.post(
'https://waas2-out-api.powersafe-rel.cc/api/v1/api/user/getUserAddress',
{ /* 请求体 */ },
{ headers: { 'X-API-KEY': 'MERCHANT_123456' } }
);
// 获取加密数据(假设响应结构为 { data: "加密数据", ... })
const encryptedData = response.data.data;
// ===================== 2. Base64解码 =====================
const decodedBytes = Base64Util.decodeString(encryptedData);
// ===================== 3. RSA公钥解密 =====================
const publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ...";
// 解密数据
const decryptedBytes = await RSAUtils.decryptByPublicKey(
decodedBytes.buffer,
publicKey
);
// 转为字符串
const decryptedText = new TextDecoder().decode(decryptedBytes);
// ===================== 4. 解析业务对象 =====================
// 使用JsonUtils反序列化为AddressVO实例
const address = JsonUtils.fromJson(decryptedText, AddressVO);
// ===================== 5. 使用解密数据 =====================
console.log("解析得到的地址信息:");
console.log("商户用户ID: " + address.getTenantUserId());
console.log("链名称: " + address.getChainName());
console.log("钱包地址: " + address.getAddress());
} catch (error) {
console.error("\n解密过程中发生错误: " + error.message);
console.error(error.stack);
}
}
// 执行主函数
main();
import requests
from JsonUtils import JsonUtils
from RSAUtils import RSAUtils
from Base64Util import Base64Util
def main():
try:
# ===================== 1. 模拟获取API响应 =====================
# 假设已发送请求并获取到响应
response = requests.post(
'https://waas2-out-api.powersafe-rel.cc/api/v1/api/user/getUserAddress',
data={},
headers={'X-API-KEY': 'MERCHANT_123456'}
)
# 解析JSON响应并获取加密数据
response_data = response.json()
encrypted_data = response_data['data']
# ===================== 2. Base64解码 =====================
decoded_bytes = Base64Util.decode_string(encrypted_data)
# ===================== 3. RSA公钥解密 =====================
public_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQ..."
decrypted_bytes = RSAUtils.decrypt_by_public_key(
decoded_bytes,
public_key
)
# 转为字符串
decrypted_text = decrypted_bytes.decode('utf-8')
# ===================== 4. 解析业务对象 =====================
# 使用JsonUtils反序列化为AddressVO实例
address_data = JsonUtils.from_json(decrypted_text)
address = AddressVO(address_data)
# ===================== 5. 使用解密数据 =====================
print("\n解析得到的地址信息:")
print("商户用户ID:", address.get_tenant_user_id())
print("链名称:", address.get_chain_name())
print("钱包地址:", address.get_address())
except Exception as e:
print("\n解密过程中发生错误:", str(e))
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()
