BouncyCastle和RSA -非法密钥大小



我正在使用以下代码:

@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

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

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

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

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

问题出在Tomcat服务上。它调用的是Java jre和jdk中的Java jre,而jre没有增强的jar文件。

这个堆栈答案可能在改变tomcat正在使用的Java版本时有用:如何更改TOMCAT使用的Java版本?

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

相关内容

最新更新