文档中心 > 国际机票大卖家API

order和pay接口加密范例

更新时间:2017/01/18 访问次数:1545

order和pay接口需要做加密和解密。
加密范例仅适用于国际机票运价直连的大卖家API接口。
其他业务的接口加密算法请查询对应文档。

...

private static final String CHARSET         = "utf-8";               // 指定utf-8
private static final byte[] IV              = new byte[16];
private static final String Algorithm       = "AES";
private static final String CipherAlgorithm = "AES/CBC/PKCS5Padding";

/**
 * 做 AES 加密后进行 BASE64 转码
 * @param content  待加密内容
 * @param password 加密密钥
 * @return
 * @throws IllegalArgumentException
 ...
 */
public static String decrypt(String content, String password) throws IllegalArgumentException
                                                                     ...
                                                                     {
    if (null == content || null == password) {
        throw new IllegalArgumentException("待加密内容或密钥为空,请重新设定后再试。");
    }

    byte[] raw = password.getBytes(CHARSET);
    if (raw.length != 16) {
        throw new IllegalArgumentException("密钥长度必须为16位,请重新设定后再试。" + password);
    }

    SecretKeySpec key = new SecretKeySpec(raw, Algorithm);
    Cipher cipher = Cipher.getInstance(CipherAlgorithm); // 创建密码器   
    byte[] byteContent = Base64.decode(content);
    if (StringUtil.isNotBlank(content) && (null == byteContent || byteContent.length == 0)) {
        throw new IllegalArgumentException("Base64解码失败,业务处理结果需经AES加密和Base64编码再输出。");
    }

    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); // 初始化   
    byte[] result = cipher.doFinal(byteContent);
    return new String(result, CHARSET); // 解密
}

...

FAQ

关于此文档暂时还没有FAQ
返回
顶部