2017-06-17 6 views
0

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. 
+1

RSAキーの長さは、ここでの問題ではない、そこにRSAキーの長さには制限は、Java 7ではありません。しかし、対称鍵は、キーストアの暗号化に使用され、それらはJKSとPKCS12のために異なっているされています。ちょうど確かめてください:無制限の強さの方針、sthのためのあなたのコードの小切手を加えてください。 https://stackoverflow.com/a/11541337/2672392 – Omikron

+0

ありがとう、Omikron。私はあなたのリンクをたどり、私の質問に答えて下のコードに変更を加えました。 – Will

答えて

0

JCEの更新プログラムをインストールするための手順は、私は誤解されることも使用していたバージョンについての私の仮定の一つ非常に簡単と思われます。 Omikronが彼の有益なコメントで指摘したように、とにかく重要ではないはずです。彼は私を正しい方向に向かわせ、解決策に導いた。私は以下の改訂コードを投稿しています。なぜ私は最初の場所で働いていたデフォルトのキーストアの種類と弾力のある城はしていないのか分かりません。たぶん、バウンシーキャスルに精通している人は、彼らの考えを分かち合います。その間、私はAndroidでもこの機能が動作するかどうかを確認します。

public static void main(String[] args) { 
    try{ 
     // ----------------------------------------------------------------- 
     // Anonymous recommendation I found here: 
     // http://suhothayan.blogspot.com/2012/05/how-to-install-java-cryptography.html 
     // This fixed my problem. 
     try { 
      Field field = Class.forName("javax.crypto.JceSecurity"). 
           getDeclaredField("isRestricted"); 
      field.setAccessible(true); 
      field.set(null, java.lang.Boolean.FALSE); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     // -----------------------------------------------------------------   
     // Check recommended by Omikron, who was correct: I assume I didn't 
     // install the JCE properly because it prints 128 for the max 
     // key allowd key length. 
     int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES"); 
     System.out.printf("max key len: %d\n",maxKeyLen); 
     // ----------------------------------------------------------------- 

     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("JKS 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"); 
     bksKS.store(bksFos, testPass); 
     bksFos.close(); 

     // ------------------------- 
     // Open file and check key length: 
     bksKS = KeyStore.getInstance("PKCS12","BC"); 
     FileInputStream bksFis = new FileInputStream("G:\\testDest.bks"); 
     bksKS.load(bksFis, testPass); 
     PrivateKey bpk = (PrivateKey) bksKS.getKey(testAlias,testPass); 
     rsaKey = (RSAKey)bpk; 
     rsaKeyLen = rsaKey.getModulus().bitLength(); 
     System.out.printf("BKS key length is %d\n",rsaKeyLen); // 2048 
     X509Certificate bkCert = (X509Certificate) bksKS.getCertificate(testAlias); 
     System.out.printf("Issuer name: %s", bkCert.getIssuerDN().getName()); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 
関連する問題