我已根据PicketLink文档的说明,从sales force创建并下载了一个证书。
我已经下载了这个证书,它的名称是mysample.crt,并将其导入到密钥库中。
检查一下,我也导出了公钥。
我有一段Java代码可以获取公钥,但它没有起作用。这是我的代码。
但是当我运行代码时,出现了以下异常。
第28行指的是
然后我得到了以下密钥。
实际的密钥不同。在“公共,证书”中,该密钥与我通过Java代码获取的密钥不同。
我已经下载了这个证书,它的名称是mysample.crt,并将其导入到密钥库中。
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而不是密钥对。这是为什么?如何获取公钥?
更新1
我已经更新了代码。keystore.load(is, passwd);
PublicKey pubKey = keystore.getCertificate(alias).getPublicKey();
String publicKeyString = Base64.encodeBase64String(pubKey.getEncoded());
System.out.println(publicKeyString);
然后我得到了以下密钥。
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlKJTbmfEumDR7nAfBbfAstuUvsgKxizZ1mwGc990dSsmgldIhsrLqpAECdf7vl2q2F8DyXciOopZbJPt/UBmpl6M1TJCQ34UyZaYGI2qid8jSNxFYGApfYPxIBJAk9YOAATqqyAREL+i1mUaFfN8WULFDvz6WsuXOjuxBobqjkg4TUumyyVgZda9ksl3aJmft02AfDMw/GCT8gKPTQb3nZP9BwTo5AQkV5fy0cKZ80G4qD+fiuZJ+8IecgFgXl5agZ0y2Wri8i1OGTGw34SUP2gOO+NUd17YA5AO+ocHlH8yzlXHNH7DPQsLo+Uz8CcXV+eLyzxGTGfuiTw8qsPCCwIDAQAB
实际的密钥不同。在“公共,证书”中,该密钥与我通过Java代码获取的密钥不同。
getCertificateChain()
返回 null。 - user2094311salesforce-idp
,这与指令中所说的不同。 - user207421