使用 Public 和私有 JKS 文件生成密钥对

Generate KeyPair using Public and Private JKS files

是否可以使用已经生成的 Public 和私有密钥库 (JKS) 文件生成 KeyPair 以在我的应用程序中使用?

谢谢

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

我想用已经生成的 RSA 2048 私钥和 public 密钥创建密钥对

您可以像下面这样使用:

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);

}