Skip to content

加密说明

加密方式

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

加密流程

  1. 将请求参数组装为 JSON 字符串
  2. 使用 AES 算法加密 JSON 字符串
  3. 将加密结果进行 Base64 编码
  4. 将编码后的字符串作为 data 字段值

解密流程

  1. data 字段进行 Base64 解码
  2. 使用 AES 算法解密
  3. 得到原始 JSON 字符串

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 aesKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(aesKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encrypted);
    }

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

Python 示例

python
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64

def encrypt(content, aes_key):
    cipher = AES.new(aes_key.encode('utf-8'), AES.MODE_ECB)
    padded = pad(content.encode('utf-8'), AES.block_size)
    encrypted = cipher.encrypt(padded)
    return base64.b64encode(encrypted).decode('utf-8')

def decrypt(encrypted_content, aes_key):
    cipher = AES.new(aes_key.encode('utf-8'), AES.MODE_ECB)
    decoded = base64.b64decode(encrypted_content)
    decrypted = cipher.decrypt(decoded)
    unpadded = unpad(decrypted, AES.block_size)
    return unpadded.decode('utf-8')

PHP 示例

php
<?php
class AESUtil {
    public static function encrypt($content, $aesKey) {
        $encrypted = openssl_encrypt($content, 'AES-128-ECB', $aesKey, OPENSSL_RAW_DATA);
        return base64_encode($encrypted);
    }

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