且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从密钥库中的文件中读取公共密钥

更新时间:2022-04-17 02:25:55

您可以通过谷歌搜索问题来找到解决方案.

You can find a solution by just googleling for your question.

来自java2s.com的示例:

Example from java2s.com:

import java.io.FileInputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;

public class Main {
  public static void main(String[] argv) throws Exception {
    FileInputStream is = new FileInputStream("your.keystore");

    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(is, "my-keystore-password".toCharArray());

    String alias = "myalias";

    Key key = keystore.getKey(alias, "password".toCharArray());
    if (key instanceof PrivateKey) {
      // Get certificate of public key
      Certificate cert = keystore.getCertificate(alias);

      // Get public key
      PublicKey publicKey = cert.getPublicKey();

      // Return a key pair
      new KeyPair(publicKey, (PrivateKey) key);
    }
  }
}

另请参阅:

  • http://www.java2s.com/Code/Java/Security/RetrievingaKeyPairfromaKeyStore.htm
  • How to retrieve my public and private key from the keystore we created

更新:

请参阅评论以获取有关该问题的其他信息.

See comments for additional information to the problem.