加密的字符串长度

时间:2014-02-10 13:07:08

标签: java encryption javax.crypto

嗨,我遇到加密字符串以获取用户pdf的问题。它显示以下错误:

javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是8的倍数

代码在那里:

public void getPDF(WebRequest request, ResourceResponse response, Model model,
                   @RequestParam("cif") String cif, 
                   @RequestParam("cuenta") String cuenta, 
                   @RequestParam("objectId") String encryptedObjectId) throws Exception{

    log.info("Inicio metodo getPDF");

    OutputStream os = response.getPortletOutputStream();


     try {

        CipherHelper cipher = new CipherHelper(CipherHelper.TRIPLE_DES_ALGORITHM, InterfazConstantes.ENCRYPTION_KEY, InterfazConstantes.ENCRYPTION_SHIFT);
        String objectId = cipher.decrypt(encryptedObjectId, true);

public String getEncryptedObjectID() {
    try {
        CipherHelper cipher = new CipherHelper(CipherHelper.TRIPLE_DES_ALGORITHM, InterfazConstantes.ENCRYPTION_KEY, InterfazConstantes.ENCRYPTION_SHIFT);
        encryptedObjectID = cipher.encrypt(objectID, true);
    } catch (Exception e) {
    }

    return encryptedObjectID;
}

**我建立了encryptedObject id有40个长度,但它没有解决问题喷射。

错误的痕迹是:

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) ~[sunjce_provider.jar:1.6]
    at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) ~[sunjce_provider.jar:1.6]

有人可以帮助我吗?

感谢

1 个答案:

答案 0 :(得分:0)

问题很可能是由于使用String表示二进制数据(加密文本= byte[])。 然后使用默认编码。

String s = new String(bytes, StandardCharsets.ISO_8859_1);// Single byte encoding
byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1);
new OutputStreamWriter(outputStream, StandardCharsets.ISO_8859_1)
new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1)