5 changed files with 83 additions and 14 deletions
@ -1,4 +1,4 @@ |
|||
package com.epmet.util; |
|||
package com.epmet.constant; |
|||
|
|||
/** |
|||
* @Description |
@ -0,0 +1,63 @@ |
|||
package com.epmet.util; |
|||
|
|||
import com.epmet.commons.tools.constant.NumConstant; |
|||
|
|||
import java.nio.charset.Charset; |
|||
import java.util.Arrays; |
|||
|
|||
/** |
|||
* @Author zxc |
|||
* @CreateTime 2020/7/6 10:45 |
|||
*/ |
|||
public class PKCS7EncoderUtil { |
|||
|
|||
static Charset CHARSET = Charset.forName("utf-8"); |
|||
static int BLOCK_SIZE = 32; |
|||
|
|||
/** |
|||
* 获得对明文进行补位填充的字节. |
|||
* |
|||
* @param count 需要进行填充补位操作的明文字节个数 |
|||
* @return 补齐用的字节数组 |
|||
*/ |
|||
static byte[] encode(int count) { |
|||
// 计算需要填充的位数
|
|||
int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE); |
|||
if (amountToPad == NumConstant.ZERO) { |
|||
amountToPad = BLOCK_SIZE; |
|||
} |
|||
// 获得补位所用的字符
|
|||
char padChr = chr(amountToPad); |
|||
String tmp = new String(); |
|||
for (int index = NumConstant.ZERO; index < amountToPad; index++) { |
|||
tmp += padChr; |
|||
} |
|||
return tmp.getBytes(CHARSET); |
|||
} |
|||
|
|||
/** |
|||
* 删除解密后明文的补位字符 |
|||
* |
|||
* @param decrypted 解密后的明文 |
|||
* @return 删除补位字符后的明文 |
|||
*/ |
|||
static byte[] decode(byte[] decrypted) { |
|||
int pad = (int) decrypted[decrypted.length - 1]; |
|||
if (pad < NumConstant.ONE || pad > 32) { |
|||
pad = NumConstant.ZERO; |
|||
} |
|||
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; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue