2. Java版本代码demo:

  1. /**
    * JAVA AES 加减密
    *
    * @date 2018/2/25 下午4:13
    */
    public class AesEncryptUtil {
    private static Charset CHARSET = Charset.forName("utf-8");
    private Base64 base64 = new Base64();
    private byte[] aesKey;
    private String token;
    private String clientId;
    /**
    * 构造函数
    *
    * @param encodingAesKey 开发者encodingAESKey
    * @param clientId 开发者clientId
    *
    * @throws TpException 异常错误信息
    */
    public AesEncryptUtil(String encodingAesKey, String clientId) throws TpException {
    int encodingAesKeyLength = 43;
    if (encodingAesKey.length() != encodingAesKeyLength) {
    throw new TpException(TpErrorEnum.ILLEGAL_AES_KEY_ERROR);
    }
    this.clientId = clientId;
    aesKey = Base64.decodeBase64(encodingAesKey + "=");
    }
    /**
    * 构造函数
    *
    * @param token 开发者token
    * @param encodingAesKey 开发者encodingAESKey
    * @param clientId 开发者clientId
    *
    * @throws TpException 异常错误信息
    */
    public AesEncryptUtil(String token, String encodingAesKey, String clientId) throws TpException {
    int encodingAesKeyLength = 43;
    if (encodingAesKey.length() != encodingAesKeyLength) {
    throw new TpException(TpErrorEnum.ILLEGAL_AES_KEY_ERROR);
    }
    this.token = token;
    this.clientId = clientId;
    aesKey = Base64.decodeBase64(encodingAesKey + "=");
    }
    /**
    * 生成4个字节的网络字节序
    *
    * @param sourceNumber 文本长度
    *
    * @return orderBytes
    */
    private byte[] getNetworkBytesOrder(int sourceNumber) {
    byte[] orderBytes = new byte[4];
    orderBytes[3] = (byte) (sourceNumber & 0xFF);
    orderBytes[2] = (byte) (sourceNumber >> 8 & 0xFF);
    orderBytes[1] = (byte) (sourceNumber >> 16 & 0xFF);
    orderBytes[0] = (byte) (sourceNumber >> 24 & 0xFF);
    return orderBytes;
    }
    /**
    * 还原4个字节的网络字节序
    *
    * @param orderBytes 字节码
    *
    * @return sourceNumber
    */
    private int recoverNetworkBytesOrder(byte[] orderBytes) {
    int sourceNumber = 0;
    int length = 4;
    int number = 8;
    for (int i = 0; i < length; i++) {
    sourceNumber <<= number;
    sourceNumber |= orderBytes[i] & 0xff;
    }
    return sourceNumber;
    }
    /**
    * 随机生成16位字符串
    *
    * @return 随机串
    */
    public String getRandomStr() {
    String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuilder stringBuild = new StringBuilder();
    int randStrLength = 16;
    for (int i = 0; i < randStrLength; i++) {
    int number = random.nextInt(base.length());
    stringBuild.append(base.charAt(number));
    }
    return stringBuild.toString();
    }
    /**
    * 对明文进行加密
    *
    * @param text 待加密明文
    *
    * @return 加密后base64编码的字符串
    *
    * @throws TpException 异常错误信息
    */
    public String encrypt(String randomStr, String text) throws TpException {
    ByteGroup byteCollector = new ByteGroup();
    byte[] randomStrBytes = randomStr.getBytes(CHARSET);
    byte[] textBytes = text.getBytes(CHARSET);
    byte[] networkBytesOrder = getNetworkBytesOrder(textBytes.length);
    byte[] clientIdBytes = clientId.getBytes(CHARSET);
    byteCollector.addBytes(randomStrBytes);
    byteCollector.addBytes(networkBytesOrder);
    byteCollector.addBytes(textBytes);
    byteCollector.addBytes(clientIdBytes);
    byte[] padBytes = PKCS7Encoder.encode(byteCollector.size());
    byteCollector.addBytes(padBytes);
    byte[] unencrypted = byteCollector.toBytes();
    try {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
    IvParameterSpec iv = new IvParameterSpec(aesKey, 0, 16);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
    byte[] encrypted = cipher.doFinal(unencrypted);
    return base64.encodeToString(encrypted);
    } catch (Exception e) {
    throw new TpException(TpErrorEnum.ENCRYPT_AES_ERROR);
    }
    }
    /**
    * 对密文进行解密
    *
    * @param text 需要解密的密文
    *
    * @return 解密得到的明文
    *
    * @throws TpException 异常错误信息
    */
    public String decrypt(String text)
    throws TpException {
    byte[] original;
    try {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec keySpec = new SecretKeySpec(aesKey, "AES");
    IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(aesKey, 0, 16));
    cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
    byte[] encrypted = Base64.decodeBase64(text);
    original = cipher.doFinal(encrypted);
    } catch (Exception e) {
    throw new TpException(TpErrorEnum.DECRYPT_AES_ERROR);
    }
    String xmlContent;
    String fromClientId;
    try {
    // 去除补位字符
    byte[] bytes = PKCS7Encoder.decode(original);
    // 分离16位随机字符串,网络字节序和ClientId
    byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
    int xmlLength = recoverNetworkBytesOrder(networkOrder);
    xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
    fromClientId = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET);
    } catch (Exception e) {
    throw new TpException(TpErrorEnum.ILLEGAL_BUFFER_ERROR);
    }
    return xmlContent;
    }
    /**
    * 加密机密demo
    * @param args
    */
    public static void main(String[] args) {
    String clientId = "1172";
    String key = "ql1234fnhsjndyskemdlsowerfvmjhxswedfrtgbqwe";
    AesEncryptUtil aesEncryptUtil = new AesEncryptUtil(key, clientId);
    String enData = aesEncryptUtil.encrypt(aesEncryptUtil.getRandomStr(), "test");
    String deData = aesEncryptUtil.decrypt(data);
    }

通用错误码对照表更新日志