java 从密钥库加载证书
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7140242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
loading a certificate from keystore
提问by user839917
Load a certificate and keys from keystore which is password protected and then use it for cert verification and digital signing
从受密码保护的密钥库加载证书和密钥,然后将其用于证书验证和数字签名
回答by Cratylus
To read the certificate is really trivial.
阅读证书真的很简单。
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) factory.generateCertificate(new FileInputStream("file.pem"));
This is with standard APIs (in try/catch) etc and you have loaded your certificate.
Now the toString
method of certificate is not suitable for you since it just captures the "user's" view of the certificate e.g. you would use it for println
for instance
Can't you send the certificate object itself?
Not sure what your server expects so you can look into the various methods of certificate
X509Certificate
这是标准 API(在 try/catch 中)等,并且您已经加载了您的证书。
现在toString
证书的方法不适合你,因为它只捕获证书的“用户”视图,例如你会使用它println
例如
你不能发送证书对象本身吗?
不确定您的服务器期望什么,因此您可以查看证书X509Certificate的各种方法
回答by Vit Hnilica
I use this code
我用这个代码
PEMReader pr=new PEMReader(new StringReader(trust_certs));
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
Object o;
int i=0;
while((o=pr.readObject())!=null){
if(o instanceof X509Certificate){
i++;
X509Certificate c=(X509Certificate)o;
trustStore.setCertificateEntry(Integer.toString(i), c);
}
}
http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/openssl/PEMReader.html
http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/openssl/PEMReader.html
回答by JB Nizet
A pem file is read as any other text file. Read the Java tutorial about IO(and concentrate on character streams, since a pem file contains text, and on File IO, since this is what you want to do)
pem 文件的读取方式与任何其他文本文件一样。阅读有关 IO的Java 教程(并专注于字符流,因为 pem 文件包含文本,以及文件 IO,因为这是您想要做的)