BouncyCastle和RSA - 非法密钥大小

时间:2013-01-04 15:06:16

标签: java rsa bouncycastle

我正在使用以下代码:

@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-----

1 个答案:

答案 0 :(得分: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的版本很有用: How to change Java version used by TOMCAT?

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