API接口解密说明
易荟通OPEN API 接口默认没有开通接口加密功能,如有需要先请往控制台配置后自动开启接口加密功能
加密说明#
- 加密支持的类型 aes-128 加密方式
- 加密范围针对所有post 请求请求体(url 上的参数不加密)和所有接口的Response 返回报文
- 加密完的密文采用base64进行编解码传输,返回报文Response数据也需要采用base64进行解码后在采用对应解密方式在解密;base64 加密的时候注意去掉换行符
- aes-128 加密方式的秘钥(encryptKey)和盐(encryptVi)是128位;因为一个字符占用8位,所有长度需要设置为16字符长度
- ase 加密模式采用 CBC
加解密工具类:#
- Java
- PHP
package com.shuopan.yunhuitong.common.utils;
import java.io.UnsupportedEncodingException;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;
import cn.hutool.core.codec.Base64;
/** * @Title: AESUtils.java * @Description: TODO * @author zhongbaoluo * @version V1.0*/public class AESUtils { /** * AES 加密 * CBC 模式 * PKCS5Padding 填充 * @param content 需加密的字符串 * @param key 秘钥 * @param iv 初始向量 * @return base64 编码后的字符串 * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws UnsupportedEncodingException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static String encryptAES(String content, String key,String iv) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
byte[] byteContent = content.getBytes("UTF-8");
// 注意,为了能与 iOS 统一 // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成 byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = iv.getBytes(); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam); // 指定加密的算法、工作模式和填充方式 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encryptedBytes = cipher.doFinal(byteContent); // 同样对加密后数据进行 base64 编码。 注意原生的base64加密工具会有换行要去掉 return Base64.encode(encryptedBytes); } /** * AES 解密 * CBC 模式 * PKCS5Padding 填充 * @param content content 传入的值会base64 解密 * @param key 秘钥 * @param iv 初始向量 * @return 解密后的字符串 * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws Exception */ public static String decryptAES(String content, String key,String iv) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, Exception { // base64 解码 byte[] encryptedBytes = Base64.decode(content); byte[] enCodeFormat = key.getBytes(); SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES"); byte[] initParam = iv.getBytes(); IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] result = cipher.doFinal(encryptedBytes); return new String(result, "UTF-8"); } }<?php
class Aes{ public $key = ''; public $iv = ''; public function __construct($config) { foreach($config as $k => $v){ $this->$k = $v; } } //加密 public function aesEn($data){ return base64_encode(openssl_encrypt($data, $this->method,$this->key, OPENSSL_RAW_DATA , $this->iv)); } //解密 public function aesDe($data){ return openssl_decrypt(base64_decode($data), $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv); }} $config = [ 'key' => '1234567890123456', //加密key 'iv' => '1234567890123456', //保证偏移量为16位 'method' => 'AES-128-CBC' //加密方式 # AES-256-CBC等 ]; $obj = new Aes($config); $res = $obj->aesEn('{"data":[{"mobile":"xxxx","text":"【xxx】测试"}]}');//加密数据 echo $res;echo '<hr>'; echo $obj->aesDe($res);//解密