javax.crypto.IllegalBlockSizeException:解密中的最后一个块不完整 - 解密加密的AES字符串

我正在尝试解密我从后端服务器收到的字符串,该服务器使用OpenSSL使用AES-256-CBC加密字符串。有代码块:"~9?8?m???=?T?G"

public static String decryptText(String textToDecrypt) {
    try {

        byte[] base64TextToDecrypt = Base64.encodeBase64(textToDecrypt.getBytes("UTF-8"));

        byte[] guid = "fjakdsjkld;asfj".getBytes("UTF-8");

        byte[] iv = new byte[16];
        System.arraycopy(guid, 0, iv, 0, guid.length);
        IvParameterSpec ips = new IvParameterSpec(iv);

        byte[] secret = DECRYPTION_SECRET_HASH.getBytes("UTF-8");
        SecretKeySpec secretKey = new SecretKeySpec(secret, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // decryption pass
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
        byte[] converted = cipher.doFinal(base64TextToDecrypt);
        System.out.println(new String(converted));

    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Decipher error for " + textToDecrypt, e);
    }
    return "";
}

不幸的是,当我到达

byte[] converted = cipher.doFinal(base64TextToDecrypt);

语句将引发以下异常:

javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

有什么想法吗?


答案 1

您应该在方法的开头对字符串进行解码,而不是对字符串的平台特定表示形式进行编码。

byte[] base64TextToDecrypt = Base64.decodeBase64(textToDecrypt);

或者更准确地说:

byte[] bytesToDecrypt = Base64(base64TextToDecrypt);

如果正确命名变量。

一般来说,每次你(感觉你必须)使用方法或构造函数时,你都可能做错了什么。您应该首先考虑要执行的操作,如果确实需要使用它,请指定String.getBytes(): byte[]String(byte[])

在您的例子中,变量中的输出可能是字符编码的。因此,您可以使用以下片段:converted

String plainText = new String(converted, StandardCharsets.UTF_8);
System.out.println(plainText);

而不是你现在拥有的。


答案 2

因此,多亏了@owlstead,我才能够找出解决方案。是我犯了Base64编码已经Base64编码的字符串的错误。以下是按代码块。

public static String decryptText(String textToDecrypt) {
    try {
        byte[] decodedValue = Base64.decodeBase64(textToDecrypt.getBytes());

        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ips = new IvParameterSpec(iv);

        byte[] input = textToDecrypt.getBytes();

        Cipher cipher = Cipher.getInstance(ENCRYPTION_METHOD);

        // decryption pass
        cipher.init(Cipher.DECRYPT_MODE, SECRET_KEY, ips);
        byte[] plainText = cipher.doFinal(decodedValue);

        return new String(plainText);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Decipher error for " + textToDecrypt, e);
    }

    return "";
}

相应的加密是这样的

public static String encryptText(String textToEncrypt) {
    try {
        byte[] guid = "1234567890123456".getBytes("UTF-8");

        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ips = new IvParameterSpec(iv);

        // The secret key from the server needs to be converted to byte array for encryption.
        byte[] secret = ENCRYPTION_SECRET_HASH.getBytes("UTF-8");

        // we generate a AES SecretKeySpec object which contains the secret key.
        // SecretKeySpec secretKey = new SecretKeySpec(secret, "AES");
        Cipher cipher = Cipher.getInstance(ENCRYPTION_METHOD);
        cipher.init(Cipher.ENCRYPT_MODE, SECRET_KEY, ips);

        byte[] cipherText = cipher.doFinal(textToEncrypt.getBytes());
        byte[] base64encodedSecretData = Base64.encodeBase64(cipherText);
        String secretString = new String(base64encodedSecretData);
        return secretString;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Encryption error for " + textToEncrypt, e);
    }
    return "";
}

推荐