人的记忆力会随着岁月的流逝而衰退,写作可以弥补记忆的不足,将曾经的人生经历和感悟记录下来,也便于保存一份美好的回忆。范文书写有哪些要求呢?我们怎样才能写好一篇范文呢?以下是小编为大家收集的优秀范文,欢迎大家分享阅读。
java读取pem证书篇一
公钥加密技术12号标准(public key cryptography standards #12,pkcs#12)为存储和传输用户或服务器私钥、公钥和证书指定了一个可移植的格式。它是一种二进制格式,这些文件也称为pfx文件。
开发人员通常需要将pfx文件转换为某些不同的格式,如pem或jks,以便可以为使用ssl通信的独立java客户端或weblogic server使用
在security编程中,有几种典型的密码交换信息文件格式:
der-encoded certificate: .cer, .crt
pem-encoded message: .pem
pkcs#12 personal information exchange: .pfx, .p12
pkcs#10 certification request: .p10
pkcs#7 cert request response: .p7r
pkcs#7 binary message: .p7b
.cer/.crt是用于存放证书,它是2进制形式存放的,不含私钥。
.pem跟crt/cer的区别是它以ascii来表示。
pfx/p12用于存放个人证书/私钥,他通常包含保护密码,2进制方式
p10是证书请求
p7r是ca对证书请求的回复,只用于导入
p7b以树状展示证书链(certificate chain),同时也支持单个证书,不含私钥。
其中,我介绍如何从p12/pfx文件中提取密钥对及其长度:
1,首先,读取pfx/p12文件(需要提供保护密码)
2,通过别名(alias,注意,所有证书中的信息项都是通过alias来提取的.)提取你想要分析的证书链
3,再将其转换为一个以x509证书结构体
4,提取里面的项,如果那你的证书项放在第一位(单一证书),直接读取 x509certs[0](见下面的代码)这个x509certificate对象
5,x509certificate对象有很多方法,tain198127网友希望读取rsa密钥(公私钥)及其长度(见http:///?topicid=43786&forumid=55&#reply),那真是太easy了,
x509certificate keypaircert = x509certs[0];
int ikeysize = tificatekeylength(keypaircert);
n("证书密钥算法="+lickey().getalgorithm());
n("证书密钥长度="+ikeysize);
提取了他所需要的信息。
package r;
import ;
import inputstream;
import notfoundexception;
import ption;
import re;
import reexception;
import algorithmexception;
import providerexception;
import ty;
import icate;
import icateexception;
import .x509certificate;
import l.x509certutil;
public class loadkeyfrompkcs12 {
public static void main(string[] args) {
try {
// open an input stream on the keystore file
string pfxfilename = " c:\\ " ;
string pfxpassword = " 123456 " ;
file fpkcs12 = null ;
if (pfxfilename != null ) {
// open the file
fpkcs12 = new file(pfxfilename);
}
fileinputstream fis = new fileinputstream(fpkcs12);
// create a keystore object
keystore keystore = null ;
try
{
// need bc provider for pkcs #12, bks and uber
if (vider( " bc " ) == null )
{
throw new exception( " 不能load入bouncycastle! " );
}
keystore = tance( " pkcs12 " , " bc " );
}
catch (keystoreexception ex)
{
throw new exception( " 不能正确解释pfx文件! " );
}
catch (nosuchproviderexception ex)
{
throw new exception( " security provider配置有误! " );
}
try
{
// load the file into the keystore
(fis, array());
}
catch (certificateexception ex)
{
throw new exception( " 证书格式问题! " );
}
catch (nosuchalgorithmexception ex)
{
throw new exception( " 算法不支持! " );
}
catch (filenotfoundexception ex)
{
throw new exception( " pfx文件没找到 " );
}
catch (ioexception ex)
{
throw new exception( " 读取pfx有误! " );
}
// 获取我的证书链的中keyentry的别名
certificate[] certs = tificatechain( " " );
x509certificate[] x509certs = tcertificates(certs);
if (x509certs == null )
{
return ;
}
x509certs = 509certchain(x509certs);
x509certificate keypaircert = x509certs[ 0 ];
int ikeysize = tificatekeylength(keypaircert);
n( " 证书密钥算法= " + lickey().getalgorithm());
n( " 证书密钥长度= " + ikeysize);
} catch (exception e) {
tacktrace();
}
}
}
s("content_relate");【如何在java处理pfx格式证书】相关文章:
1.java中日期的处理方法2.java里处理文件的技巧3.如何在java中解压zip和rar文件4.用java如何处理xml数据5.cad如何在布局中处理图形6.如何在php中处理protocol buffers数据7.java对数字证书的常用操作8.java证书的加密与解密代码

一键复制