2048ビットの鍵を持つ証明書をBouncy Castle KeyStoreに追加できません。 JREとJDKの両方のセキュリティフォルダのJCEバージョンをUnlimitedJCEPolicyJDK7.zipで更新しました。以下のコードは、エラーの場所を示しています。私はbcprov-jdk15on-149を使用していますが、bcprov-jdk15on-157と同じ結果を試しました。対称暗号化に関する問題は数多くありますが、PKEには少なすぎます。私はWindows 10 Pro、JRE 7、JDK 1.7.0_51を実行しています。私は何か提案を感謝します。InvalidKeyException:不正なキーサイズの保存BouncyCastleで、デフォルトプロバイダの公開鍵でない
char[] testPass = "changeit".toCharArray();
String testAlias = "express";
// -----------------------------------------------------------------
// Open source TrustStore and extract certificate and key
FileInputStream jksFis = new FileInputStream("G:\\testSrc.jks");
KeyStore jksKS = KeyStore.getInstance(KeyStore.getDefaultType());
jksKS.load(jksFis, testPass);
PrivateKey jksPK = (PrivateKey) jksKS.getKey(testAlias,testPass);
RSAKey rsaKey = (RSAKey)jksPK;
int rsaKeyLen = rsaKey.getModulus().bitLength();
System.out.printf("Key length is %d\n",rsaKeyLen); // 2048
X509Certificate[] jksCerts = new X509Certificate[1];
jksCerts[0] = (X509Certificate) jksKS.getCertificate(testAlias);
// -----------------------------------------------------------------
// Create new default type keystore and add certificate and key.
KeyStore jksDest = KeyStore.getInstance(KeyStore.getDefaultType());
jksDest.load(null,null);
jksDest.setKeyEntry(testAlias, jksPK, testPass, jksCerts);
FileOutputStream jfos = new FileOutputStream("G:\\testDest.jks");
jksDest.store(jfos, testPass);
jfos.close();
// -----------------------------------------------------------------
// Create Bouncy Castle KeyStore and add certificate and key
Security.addProvider(new BouncyCastleProvider());
KeyStore bksKS = KeyStore.getInstance("PKCS12","BC");
bksKS.load(null,null);
bksKS.setKeyEntry(testAlias, jksPK, testPass, jksCerts);
FileOutputStream bksFos = new FileOutputStream("G:\\testDest.bks");
// -----------------------------------------------------------------
// Next line gives this error:
// java.io.IOException: exception encrypting data -
// java.security.InvalidKeyException: Illegal key size
bksKS.store(bksFos, testPass); // This is the error line.
// Error on previous line.
RSAキーの長さは、ここでの問題ではない、そこにRSAキーの長さには制限は、Java 7ではありません。しかし、対称鍵は、キーストアの暗号化に使用され、それらはJKSとPKCS12のために異なっているされています。ちょうど確かめてください:無制限の強さの方針、sthのためのあなたのコードの小切手を加えてください。 https://stackoverflow.com/a/11541337/2672392 – Omikron
ありがとう、Omikron。私はあなたのリンクをたどり、私の質問に答えて下のコードに変更を加えました。 – Will