/**
 * 对公众平台发送会给公众账号的消息加解密示例代码
 *
 * @copyright Copyright (freedbconnlist) 1998-2014 Tencent Inc.
 */

// ------------------------------------------------------------------------

package com.cnd3b.utility.qywechat.base.system;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * 提供基于PKCS7算法的加解密接口
 */
class PKCS7Encoder {
    static Charset CHARSET = StandardCharsets.UTF_8;
    static int BLOCK_SIZE = 32;

    /**
     * 获得对明文进行补位填充对字节
     *
     * @param count 需要进行填充补位操作的明文字节个数
     * @return 补齐用的字节数组
     */
    static byte[] encode(int count) {
        // 计算需要填充的位数
        int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
        if (amountToPad == 0) {
            amountToPad = BLOCK_SIZE;
        }
        // 获得补位所用的字符
        char padChr = chr(amountToPad);
        StringBuilder tmp = new StringBuilder();
        for (int index = 0; index < amountToPad; index++) {
            tmp.append(padChr);
        }
        return tmp.toString().getBytes(CHARSET);
    }

    /**
     * 删除解密后明文的补位字符
     *
     * @param decrypted ?????????
     * @return ???????????????
     */
    static byte[] decode(byte[] decrypted) {
        int pad =  decrypted[decrypted.length - 1];
        if (pad < 1 || pad > 32) {
            pad = 0;
        }
        return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
    }

    /**
     * ?????????ASCII????????????????????????
     *
     * @param a ???????????
     * @return ???????????
     */
    static char chr(int a) {
        byte target = (byte) (a & 0xFF);
        return (char) target;
    }

}
