Skip to content

接口地址

https://openapi.lvcang.cn/

授权

  • 开放平台 会给出授权账号信息 x-lvcang-api-account  需要放到请求头中,用于校验接口权限 aesKey 对请求数据进行加解密。
  • 对请求数据 Body 的Json数据,进行AES加密(aes key),放置在统一的请求参数 signature

例子

java
请求Body是
{
"page": 1,
"pageSize": 20
}
进行AES加密后,最终的请求参数是
{
"signature": "gHml6FJKU+tP14g9KI3kJ+kkrImXDzVLI9UDpw0fz64=",
"requestId": "每次请求的唯一ID,建议采用 UUID"
}

加密解密方法(Java)

java
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AESUtil {

    /**
     * AES加密字符串
     *
     * @param content 需要被加密的字符串
     * @param aesKey  加密需要的密码
     * @return 密文
     */
    public static String encrypt(String content, String aesKey) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8), "AES"));
            byte[] bytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            String encode = new BASE64Encoder().encode(bytes);
            return encode.replaceAll(System.getProperty("line.separator"), "");

        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 解密AES加密过的字符串
     *
     * @param content AES加密过过的内容
     * @param aesKey  加密时的密码
     * @return 明文
     */
    public static String decrypt(String content, String aesKey) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8), "AES"));
            byte[] bytes = new BASE64Decoder().decodeBuffer(content);
            bytes = cipher.doFinal(bytes);
            return new String(bytes, "utf-8");

        } catch (Exception e) {
            return null;
        }
    }

}

加密解密方法(Python)

python
from Crypto.Cipher import AES
import base64

class AESUtil:

    @staticmethod
    def encrypt(content, aes_key):
        key_bytes = aes_key.encode('utf-8')
        content_bytes = content.encode('utf-8')
        pad_len = 16 - len(content_bytes) % 16
        padded_content_bytes = content_bytes + (chr(pad_len) * pad_len).encode()
        cipher = AES.new(key_bytes, AES.MODE_ECB)
        cipher_bytes = cipher.encrypt(padded_content_bytes)
        encrypted_content = base64.b64encode(cipher_bytes).decode('utf-8')
        return encrypted_content

    @staticmethod
    def decrypt(encrypted_content, aes_key):
        key_bytes = aes_key.encode('utf-8')
        encrypted_bytes = base64.b64decode(encrypted_content)
        cipher = AES.new(key_bytes, AES.MODE_ECB)
        decrypted_bytes = cipher.decrypt(encrypted_bytes)
        pad_length = decrypted_bytes[-1]
        decrypted_content = decrypted_bytes[:-pad_length].decode('utf-8')
        return decrypted_content

加密解密方法(PHP)

php
<?php

class Encrypt
{
    private $secretKey = '';

    /**
     * 加密
     */
    public function encryptAES($data)
    {
        $key = $this->secretKey;
        $data = self::addPKCSPadding($data);
        $encrypt_str = openssl_encrypt(mb_convert_encoding($data, 'UTF-8'), 'AES-256-ECB', $key, OPENSSL_NO_PADDING);
        return base64_encode($encrypt_str);
    }

    private static function addPKCSPadding($data)
    {
        $blockSize = 16;
        $padding = $blockSize - (strlen($data) % $blockSize);
        $padChar = chr($padding);
        return $data . str_repeat($padChar, $padding);
    }
}

class Decrypt
{
    private $secretKey = '';

    /**
     * 解密
     */
    public function decryptAES($encryptData)
    {
        $key = $this->secretKey;
        $encryptData = base64_decode($encryptData);
        $decrypt_str = openssl_decrypt($encryptData, 'AES-256-ECB', $key, OPENSSL_NO_PADDING);
        return self::stripPKCSPadding($decrypt_str);
    }

    private static function stripPKCSPadding($data)
    {
        $padding = ord($data[strlen($data)-1]);
        return substr($data, 0, -$padding);
    }
}

?>