加密机制
为保证数据的安全性,平台会对每个请求和响应进行加密。
接入方与平台需使用对称加密算法AES/ECB/PKCS5Padding,对请求体和响应体进行加密与解密,结果采用Base64 编码。
平台需要接入方提前提供128位AES密钥(Base64 编码格式)。
请求体
原始请求体各字段含义详见下方各API说明。
接入方:对原始请求体先使用AES密钥加密,再进行Base64编码。结果作为实际请求体中bizContent字段的值。发送实际请求体。
平台:对收到的实际请求体的bizContent字段的值先进行Base64解码,再使用AES密钥解密。结果即为原始请求体。
{
"key_1": "value_1",
"key_2": "value_2",
"key_3": "value_3"
}{"bizContent":"8YryQ8hYkTL3UzzEdvlUGGrL/5rABcclaP9NZt9GAMRVR9ehvJv9aJHfM3XqPlRIk9lvPN+U0iERGz9wpHtxvA=="}工具类示例
<?php
class AesUtils {
public static function aesEncrypt(string $keyStr, string $plaintext): string {
return base64_encode(openssl_encrypt($plaintext, 'AES-128-ECB', base64_decode($keyStr), OPENSSL_RAW_DATA));
}
public static function aesDecrypt(string $keyStr, string $ciphertext): string {
return openssl_decrypt(base64_decode($ciphertext), 'AES-128-ECB', base64_decode($keyStr), OPENSSL_RAW_DATA);
}
}import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
public class AesUtils {
private static final Base64.Encoder ENCODER = Base64.getEncoder();
private static final Base64.Decoder DECODER = Base64.getDecoder();
public static String aesEncrypt(SecretKey key, String plaintext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherBytes = cipher.doFinal(plaintext.getBytes(UTF_8));
return new String(ENCODER.encode(cipherBytes), UTF_8);
}
public static String aesDecrypt(SecretKey key, String ciphertext) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cipherBytes = DECODER.decode(ciphertext.getBytes(UTF_8));
return new String(cipher.doFinal(cipherBytes), UTF_8);
}
public static SecretKey newAesKey(String keyStr) {
return new SecretKeySpec(DECODER.decode(keyStr.getBytes(UTF_8)), "AES");
}
public static String toAesKeyStr(SecretKey key) {
return new String(ENCODER.encode(key.getEncoded()), UTF_8);
}
}最后更新于