小编Kri*_*hna的帖子

线程 java.lang.IllegalStateException 中的异常:密码未初始化

在运行时,在解密密文时,使用 Cipher not initialized 消息获取 java.lang.IllegalStateException 我的代码如下:

public String decrypt(String cipherText) throws SecurityException {
        String clearText = null;
        try {
            cipher = Cipher.getInstance("AES/OFB/NoPadding");
            byte[] cipherTextBytes = Base64.decodeBase64(cipherText.getBytes());
            byte[] iv = ArrayUtils.subarray(cipherTextBytes, 0, INIT_VECTOR_LENGTH);
            cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
            byte[] decryptedBytes = cipher
                    .doFinal(ArrayUtils.subarray(cipherTextBytes, INIT_VECTOR_LENGTH, cipherTextBytes.length));
            clearText = new String(decryptedBytes, CHARACTER_ENCODING).trim();
        } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException
                | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
            throw new SecurityException(e);
        }
        return clearText;
    }
Run Code Online (Sandbox Code Playgroud)

线程“pool-5-thread-3”中的异常java.lang.IllegalStateException:密码未在javax.crypto.Cipher.doFinal(Cipher.java:2156)处的javax.crypto.Cipher.checkCipherState(Cipher.java:1749)处初始化)

这是间歇性问题,一段时间后它解密密文并按预期工作。

java encryption multithreading cryptography illegalstateexception

1
推荐指数
1
解决办法
597
查看次数