首页 > 解决方案 > 例外:Linux 中的“给定最终块未正确填充”,但它在 Windows 中有效

问题描述

这可能是Exception: "Given final block not proper padded" 在 Linux 中的重复,但它在 Windows 中有效,但并非完全有效。

我的应用程序在 Windows 中工作,但在 Linux 中失败

给定最终块未正确填充异常

配置:

JDK版本:1.8u162

视窗:版本 10

Linux:OpenSuSE 42.2 (x86_64)

我的代码如下:

public static Cipher createCipher(int mode, String passPhrase) throws CypherException {
        // Salt value
        byte[] salt = new byte[128]; // Should be atleast 8 bytes
        SecureRandom secRandom = new SecureRandom(passPhrase.getBytes(StandardCharsets.UTF_8));
        secRandom.nextBytes(salt); // Self-seeded randomizer for salt

        // Iteration count
        int iterationCount = 12288;

        int derivedKeyLength = 256 ; // Should be atleast longer than 112 bits. Depends on Key size of algorithm.

        Cipher cipher;
        try {
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, derivedKeyLength * 8);
            SecretKey key = SecretKeyFactory.getInstance(APBEWithHmacSHA512AndAES_256).generateSecret(keySpec);

            byte iv[] = new byte[16];
            secRandom.nextBytes(iv); // Self-seeded randomizer to generate IV
            IvParameterSpec ivSpec = new IvParameterSpec(iv) ; // IvParameterSpec initialized using its own randomizer

            // Note: there is no typical transformation string. Algorithm, mode (CBC) and padding scheme (PKCS5Padding) is all taken care by ALGORITHM_NAME.
            cipher = Cipher.getInstance(PBEWithHmacSHA512AndAES_256);
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount, ivSpec);

            // Create the Cipher
            cipher.init(mode, key, paramSpec);
        } catch (NoSuchPaddingException nspe) {
            throw new CypherException("No such Padding: " + nspe.getMessage());
        } catch (NoSuchAlgorithmException nsae) {
            throw new CypherException("Algorithm not supported: " + nsae.getMessage());
        } catch (InvalidKeyException ike) {
            throw new CypherException("Invalid key: " + ike.getMessage());
        } catch (InvalidKeySpecException ikse) {
            throw new CypherException("Invalid key specification: " + ikse.getMessage());
        } catch (InvalidAlgorithmParameterException iape) {
            throw new CypherException("Invalid algorithm parameter: " + iape.getMessage());
        }
        return cipher;
    }

public static void decrypt(InputStream in, OutputStream out, String passPhrase) throws IOException, CypherException {
        // Bytes read from in will be decrypted
        in = new CipherInputStream(in, createCipher(Cipher.DECRYPT_MODE, passPhrase));
        int numRead;
        // Buffer used to transport the bytes from one stream to another
        byte[] buf = new byte[1024];
        // Read in the decrypted bytes and write the cleartext to out
        try {
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
        } finally {
           // close the streams
        }
    }

我得到

javax.crypto.BadPaddingException:给定最终块未正确填充

在以下行:

while ((numRead = in.read(buf)) >= 0) {

现在,正如Exception: "Given final block not proper padded" in Linux 中所建议的那样,但它在 Windows 中由 Maarten Bodewes 工作我已经使用 PBKDF2 作为 PBEWith*,实际上是 PBKDF2 + 加密方案(带有 PKCS5Padding 的 CBC 模式)。那么在我的情况下,为什么在 Windows 上完成的加密在 Linux 上不起作用?有人可以帮忙吗?

标签: javaencryption

解决方案


推荐阅读