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

2022-09-01 22:36:02

嗨,我想使用Java代码从密钥库中提取公钥

我正在创建密钥库

keytool -genkey -alias mykeystore -keyalg RSA -keystore mykeystore.jks -keysize 2048

并将公共导出到另一个文件中

keytool -export -alias mykeystore -keystore mykeystore.jks -rfc -file publickey.cert

如何使用 Java 代码从密钥库或 publickey.cert 文件获取公钥字符串?

谢谢。

更新

public static void main(String[] args) {

    try {

        FileInputStream is = new FileInputStream("/home/myuser/my-keystore/mykeystore.jks");
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        String password = "myuserpass";
        char[] passwd = password.toCharArray();
        keystore.load(is, passwd);
        String alias = "mykeystore";
        Key key = keystore.getKey(alias, passwd);
        if (key instanceof PrivateKey) {
          // Get certificate of public key
          Certificate cert = keystore.getCertificate(alias);
          // Get public key
          PublicKey publicKey = cert.getPublicKey();

          String publicKeyString = Base64.encodeBase64String(publicKey
                    .getEncoded());
          System.out.println(publicKeyString);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后它就像给予一样

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiG2FjSuXrraYuh0TyRNiUvVCyaFlb7VY9AFIMSDdcY0JgNF0c4YVQxYxUCbYzmkLZD/rrYMe/8nxkWV0TMz2Y7GnvichjtWHL1ui58uC0+RtFMkYJ+ftwt9qBy9hvb/rVFTsvT5/b6CQXD8a6bFveMUluQZISLCV7i11XYzp81+w6M7+2fJAwezIJnIrgwv1K9YDjWaToaNXe7hnzzy0s8AdkjTk197+hg8dRfbvkr8XAddNsEMPeUA5iY+5VEpRNI925ZT/dxnaABA0z6i4JbVjeLl8r7ySG9R/2w/j2G+/YSRQc9BmRHPa0tBgH7wvQM+WRwD9WmST+5qeBIfH3QIDAQAB

当我这样做时,它显示这个cat publickey.cert

-----BEGIN CERTIFICATE-----
MIIDgTCCAmmgAwIBAgIEf7XoMDANBgkqhkiG9w0BAQsFADBxMQswCQYDVQQGEwJJTjESMB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HMhxkNCJiRDsasAiQ8
7V5uBI3ucdSwh+gPaW8KoWlJpv5SGlAkwzq0lSrxyq2ukkC6ciPeKhUvWtHaPg==
-----END CERTIFICATE-----

它们的键是不同的,甚至在长度上也是如此。为什么?


答案 1

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

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

另请参阅:

更新:

有关该问题的其他信息,请参阅注释。


答案 2

试试这个:

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.security.PublicKey;
import java.util.Base64;

//=====================

try {
    File file = new File("C:\\Program Files (x86)\\keyStoreFilehere.kstr");
    FileInputStream is = new FileInputStream(file);
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    String password = "password";
    keystore.load(is, password.toCharArray());
    Enumeration enumeration = keystore.aliases();
    while(enumeration.hasMoreElements()) {
         String alias = (String)enumeration.nextElement();
         Certificate certificate = keystore.getCertificate(alias);
         PublicKey publicKey = keystore.getCertificate(alias).getPublicKey();
         byte[] encodedCertKey = certificate.getEncoded();
         byte[] encodedPublicKey = publicKey.getEncoded();
         String b64PublicKey = Base64.getMimeEncoder().encodeToString(encodedPublicKey);
         String b64CertKey = Base64.getMimeEncoder().encodeToString(encodedCertKey);
         String publicKeyString = "-----BEGIN CERTIFICATE-----\n"
                            + b64PublicKey
                            + "\n-----END CERTIFICATE-----";

         String certKeyString = "-----BEGIN CERTIFICATE-----\n"
                            + b64CertKey
                            + "\n-----END CERTIFICATE-----";
         System.out.println(publicKeyString);
         System.out.println(certKeyString);
    }

 } catch (CertificateException | NoSuchAlgorithmException  | KeyStoreException | IOException e) {
            e.printStackTrace();
 }

推荐