例外:在Linux中“给出的最终块未正确填充”,但在Windows中有效

时间:2018-07-20 15:11:51

标签: java encryption

这可以与Exception: "Given final block not properly padded" in Linux, but it works in Windows重复,但不能完全重复。

我的应用程序可在Windows中运行,但在Linux中使用

会失败
  

给出了最终块未正确填充的异常

配置:

JDK版本: 1.8u162

Windows:版本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) {

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

0 个答案:

没有答案