加解密方式是SM4,加密没有问题,解密的时候为什么明文后面有一串方框。明文是正确的,
明文后面的方框是多余的,有什么方法给去掉。为什么会出现这种问题,原因是什么?
加解密方式是SM4,加密没有问题,解密的时候为什么明文后面有一串方框。明文是正确的,
明文后面的方框是多余的,有什么方法给去掉。为什么会出现这种问题,原因是什么?
/**
* @description: 不限密文长度的SMS4解密,获得byte类型的明文
* @param: cipherText(密文)
* @param: key(密钥)
* @return: byte[]
*/
private static byte[] decodeSMS4(byte[] cipherText, byte[] key) {
byte[] plaintext = new byte[cipherText.length];
int k = 0;
int cipherLen = cipherText.length;
while (k + 16 <= cipherLen) {
byte[] cellCipher = new byte[16];
for (int i = 0; i < 16; i++) {
cellCipher[i] = cipherText[k + i];
}
byte[] cellPlain = decode16(cellCipher, key);
for (int i = 0; i < cellPlain.length; i++) {
plaintext[k + i] = cellPlain[i];
}
k += 16;
}
return plaintext;
}
/**
* @description: 解密,获得明文字符串
* @param: cipherText(密文)
* @param: key(密钥)
* @return: java.lang.String
*/
private static String decodeSMS4toString(byte[] cipherText, byte[] key) {
byte[] plaintext = new byte[cipherText.length];
plaintext = decodeSMS4(cipherText, key);
return new String(plaintext);
}