Skip to content

Latest commit

 

History

History
90 lines (67 loc) · 3.03 KB

1.2.4_加密机制.md

File metadata and controls

90 lines (67 loc) · 3.03 KB

加密机制

AES是什么

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是目前对称密钥加密中比较通用的一种加密方式。

AES密钥有什么用

炼金台开放平台所有API均要求对接口的请求内容和响应内容进行AES加密。加密后,在网络上传输的接口报文内容将会由明文内容变为密文内容,可以大大提升接口内容传输的安全性。

AES密钥与签名的关系

AES密钥是对接口请求和响应内容进行加密,密文无法被第三方识别,从而防止接口传输数据泄露。 调用方要对请求体先做AES加密,然后对密文进行签名。

AES密钥获取

请联系对接专员获取。

使用AES密钥加解密

加密算法demo

{% codetabs name="Java", type="java" -%} public static String encrypt() throws Exception {

    String key = "调用方的AES秘钥";
    String content = "需要加密的内容";
    String charset = "UTF-8";
    String fullAlg = "AES/CBC/PKCS5Padding";

    Cipher cipher = Cipher.getInstance(fullAlg);
    IvParameterSpec iv = new IvParameterSpec(initIv(fullAlg));
    cipher.init(Cipher.ENCRYPT_MODE,
            new SecretKeySpec(Base64.decodeBase64(key.getBytes()), "AES"),
            iv);

    byte[] encryptBytes = cipher.doFinal(content.getBytes(charset));
    return new String(Base64.encodeBase64(encryptBytes));
}

private static byte[] initIv(String fullAlg) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance(fullAlg);
    int blockSize = cipher.getBlockSize();
    byte[] iv = new byte[blockSize];
    for (int i = 0; i < blockSize; ++i) {
        iv[i] = 0;
    }
    return iv;
}

{%- language name="Python", type="py" -%} 码农正在加班编写中。 {%- language name="PHP", type="php" -%} 码农正在加班编写中。 {%- endcodetabs %}

解密算法demo

{% codetabs name="Java", type="java" -%} public String decrypt() throws Exception { String key = "调用方的AES秘钥"; String content = "需要解密的内容";

    //反序列化AES密钥
    SecretKeySpec keySpec = new SecretKeySpec(Base64.decodeBase64(key.getBytes()), "AES");
      
    //128bit全零的IV向量
    byte[] iv = new byte[16];
    for (int i = 0; i < iv.length; i++) {
        iv[i] = 0;
    }
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
    
    //初始化加密器并加密
    Cipher deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    deCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
    byte[] encryptedBytes = Base64.decodeBase64(content.getBytes());
    byte[] bytes = deCipher.doFinal(encryptedBytes);
    return new String(bytes);
}

{%- language name="Python", type="py" -%} 码农正在加班编写中。 {%- language name="PHP", type="php" -%} 码农正在加班编写中。 {%- endcodetabs %}

##FAQ 关于此文档暂时还没有FAQ