Skip to content

加密说明

加密方式

标准机构适配器当前使用纯 AES 加密。

纯 AES 加密

  • 算法:AES/ECB/PKCS5Padding
  • 密钥长度:128位(16字节)
  • 编码方式:Base64

AES 加密示例(Java)

java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESUtil {
    public static String encrypt(String content, String key) throws Exception {
        byte[] keyBytes = key.getBytes("UTF-8");
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedContent, String key) throws Exception {
        byte[] keyBytes = key.getBytes("UTF-8");
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decoded = Base64.getDecoder().decode(encryptedContent);
        byte[] decrypted = cipher.doFinal(decoded);
        return new String(decrypted, "UTF-8");
    }
}

PHP 示例

php
<?php

class AESUtil
{
    public static function encrypt(string $content, string $key): string
    {
        $encrypted = openssl_encrypt($content, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return base64_encode($encrypted);
    }

    public static function decrypt(string $encryptedContent, string $key): string
    {
        $decoded = base64_decode($encryptedContent);
        return openssl_decrypt($decoded, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }
}

注意事项

  1. 请求外层保持 { channelCode, data }
  2. data 为业务 JSON 字符串经过 AES 加密后再 Base64 编码的结果。
  3. 加密前请确保 JSON 字符串编码为 UTF-8。