2

我正在使用以下代码:

@Test
public void simpleEncryptDecryptTest_shouldSucceed() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    String text = "ASDF-asdföjk_\n394ysf";
    String encryptedText = null;


    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    PEMReader in = new PEMReader(new FileReader("C:/Users/User/tu/vs_exc3/keys/auction-server.pem"), new PasswordFinder() {
        @Override
        public char[] getPassword() {
            return new char[] {'2', '3', '4', '5', '6'};
        }
    });

    PrivateKey privateKey = (PrivateKey)in.readObject();

    in = new PEMReader(new FileReader("C:/Users/User/tu/vs_exc3/keys/auction-server.pub.pem"));


    PublicKey publicKey = (PublicKey)in.readObject();

    Cipher decodeCipher = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding");
    Cipher encodeCipher = Cipher.getInstance("RSA/NONE/OAEPWithSHA256AndMGF1Padding");
    decodeCipher.init(Cipher.DECRYPT_MODE, privateKey);
    encodeCipher.init(Cipher.ENCRYPT_MODE, publicKey);


    byte[] encrypted = encodeCipher.doFinal(text.getBytes());
    encryptedText = new String(encrypted);
    byte[] decrypted = decodeCipher.doFinal(encryptedText.getBytes());

    Assert.assertTrue(text.equals(new String(decrypted)));
}

我得到以下异常:

    org.bouncycastle.openssl.EncryptionException: exception using cipher - please check password and data.
        at org.bouncycastle.openssl.PEMUtilities.crypt(Unknown Source)
            at org.bouncycastle.openssl.PEMReader.readKeyPair(Unknown Source)
            at org.bouncycastle.openssl.PEMReader.readObject(Unknown Source)
            at      utils.Testibert.simpleEncryptDecryptTest_shouldSucceed(Testibert.java:57)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
            at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
            at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
            at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
            at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
            at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:      
    Caused by: java.security.InvalidKeyException: Illegal key size
            at javax.crypto.Cipher.a(DashoA13*..)
            at javax.crypto.Cipher.init(DashoA13*..)
            at javax.crypto.Cipher.init(DashoA13*..)
            ... 27 more

我已经在 J​​RE 和我的 JDK 目录中安装了 JCE 无限强度文件。什么可能导致异常?

编辑 1: 私钥文件如下所示:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,8429830AD224E4D56A21C3C680D6EA57

key...
-----END RSA PRIVATE KEY-----
4

1 回答 1

0

这是一个迟到的答案,但我遇到了同样的问题。我在 Tomcat 上部署了一个 Web 应用程序,并在解密密钥时遇到了同样的错误。文件已Unlimited Strength Jurisdiction Policy正确安装。这是有效的,然后我们在服务器上更新了 Tomcat,我们开始出现Caused by: java.security.InvalidKeyException: Illegal key size异常。

事实证明,问题出在 Tomcat 服务上。它在 jdk 中调用 Java jre 与 Java jre,而 jre 没有增强的 jar 文件。

此堆栈答案可能有助于更改正在使用的 Java Tomcat 的版本: 如何更改 TOMCAT 使用的 Java 版本?

尽管我的问题与 Tomcat 有关,但我相信这也可能是在任何环境中运行 Java 的问题。

于 2020-02-13T16:36:23.927 回答