java - 使用 Java 从 Keystore 中导入的证书获取公钥

标签 java keystore public-key

我已经从 sales force 创建并下载了证书,按照 PicketLink document 中的说明.

我下载了证书,它的名字是 mysample.crt 和 我将证书导入 keystore 。

keytool -import -file mysample.crt -keystore keystore.jks -alias salesforce-idp

为了检查,我还导出了公钥

keytool -export -alias salesforce-idp -keystore keystore.jks -rfc -file public.cert

我有一个 Java 代码来获取公钥,但它不起作用。这是我的代码

package com.sample.keystore;

import java.io.File;
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 org.apache.commons.codec.binary.Base64;

public class ExtractPublicKey {

    public static void main(String[] args) {

        try {
            // Load the keystore
            File file = new File("/home/user/salesforce-cert/keystore.jks");
            FileInputStream is = new FileInputStream(file);
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            String alias = "salesforce-idp";
            String password = "user";
            char[] passwd = password.toCharArray();
            keystore.load(is, passwd);
            KeyPair kp = getKeyPair(keystore, alias, passwd);
            Base64 base64 = new Base64();
            PublicKey pubKey = kp.getPublic();

            String publicKeyString = base64.encodeBase64String(pubKey
                    .getEncoded());

            System.out.println(publicKeyString);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static KeyPair getKeyPair(KeyStore keystore, String alias, char[] password) throws Exception {
            // Get private key
            Key key = keystore.getKey(alias, password);
            if (key instanceof PrivateKey) {
                // Get certificate of public key
                java.security.cert.Certificate cert = keystore.getCertificate(alias);

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

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

}

但是当我运行代码时,出现以下异常

java.lang.NullPointerException
    at com.sample.keystore.ExtractPublicKey.main(ExtractPublicKey.java:28)

第 28 行引用了 PublicKey pubKey = kp.getPublic();。因为该方法返回 null 而不是 key 对。这是为什么?以及如何获取公钥?

更新 1

我将代码更新为

keystore.load(is, passwd);
PublicKey pubKey = keystore.getCertificate(alias).getPublicKey();
String publicKeyString = Base64.encodeBase64String(pubKey.getEncoded());
System.out.println(publicKeyString);

然后我得到以下 key

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlKJTbmfEumDR7nAfBbfAstuUvsgKxizZ1mwGc990dSsmgldIhsrLqpAECdf7vl2q2F8DyXciOopZbJPt/UBmpl6M1TJCQ34UyZaYGI2qid8jSNxFYGApfYPxIBJAk9YOAATqqyAREL+i1mUaFfN8WULFDvz6WsuXOjuxBobqjkg4TUumyyVgZda9ksl3aJmft02AfDMw/GCT8gKPTQb3nZP9BwTo5AQkV5fy0cKZ80G4qD+fiuZJ+8IecgFgXl5agZ0y2Wri8i1OGTGw34SUP2gOO+NUd17YA5AO+ocHlH8yzlXHNH7DPQsLo+Uz8CcXV+eLyzxGTGfuiTw8qsPCCwIDAQAB

Nut 实际 key 不同。在 public,cert 中, key 与我通过 Java 代码获得的 key 不同。

最佳答案

要获取与私钥关联的证书,您应该调用 getCertificateChain(),并使用返回数组的第零个元素。不是 getCertificate()

关于java - 使用 Java 从 Keystore 中导入的证书获取公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709265/

相关文章:

java - 如何在 RSS android 应用程序中查看来自多个源的提要

android - keyStore rsa privateKeyEntry,公钥和私钥相等

android - keystore apk 统一错误

heroku - 无法推送到heroku

encryption - 便宜的 SSL 与昂贵的 SSL

java - 在 KOI8_R 中检索 html

Android 上的 java.lang.ClassNotFoundException : net. sourceforge.jtds.jdbc.Driver

java - 使用 java 在 HSQL 数据库中出现异常 - 用户缺乏权限或未找到对象错误

android - 在公共(public)存储库上为 CircleCI 添加 Google-services.json 以使其运行

java - 为什么 Java KeyStore 在加载 OpenPGP key 时失败?