2017-01-18 6 views
1

既に生成されたパブリックおよびプライベートキーストア(JKS)ファイルを使用してアプリケーション内で使用するためにKeyPairを生成することは可能ですか?パブリックおよびプライベートJKSファイルを使用してKeyPairを生成

おかげ

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 
keyGen.initialize(2048); 
KeyPair keypair = keyGen.genKeyPair(); 

私はあなたが以下のように使用することができ、既に生成RSA 2048秘密鍵と公開鍵

答えて

0

で鍵ペアを作成したい:

public static KeyPair loadKeyStore(final File keystoreFile, 
     final String password, final String alias, final String keyStoreType) 
     throws Exception { 
    if (null == keystoreFile) { 
     throw new IllegalArgumentException("Keystore url may not be null"); 
    } 
    final KeyStore keystore = KeyStore.getInstance(keyStoreType); 
    InputStream is = null; 
    try { 
     is = new FileInputStream(keystoreFile); 
     keystore.load(is, null == password ? null : password.toCharArray()); 
    } finally { 
     if (null != is) { 
      is.close(); 
     } 
    } 
    final PrivateKey key = (PrivateKey) keystore.getKey(alias, 
      password.toCharArray()); 
    final Certificate cert = keystore.getCertificate(alias); 
    final PublicKey publicKey = cert.getPublicKey(); 
    return new KeyPair(publicKey, key); 

} 
+0

パラメータ「最終ファイルいるkeystoreFile」についてパブリックキーストアまたはプライベートキーストアを渡しますか? – user3520080

+0

プライベートキーストアをロードすると、このコードの例外が発生します。「例外がスレッド内にあります」メイン「java.security.UnrecoverableKeyException:キーを回復できません」 – user3520080

+0

問題は鍵とパスワードの両方が異なるためです。証明書パス。私は少しコードを更新し、それは動作します – user3520080

関連する問題