标签:read 转换 java set private element pen reader 解密
//读DER证书
File file = new File(path);
long filesize = file.length();
FileInputStream fis = new FileInputStream(file);
byte[] cert = new byte[(int) filesize];
int offset = 0;
int numRead = 0;
while (offset < cert.length
&& (numRead = fis.read(cert, offset, cert.length - offset)) >= 0) {
offset += numRead;
}
fis.close();
//输出X509格式证书
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");//获取工厂实例
Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(cert));//用文件流读入证书
X509Certificate x509Certificate = (X509Certificate)certificate;
return x509Certificate;
//加载BC
Security.addProvider( new BouncyCastleProvider() );
//使用BC提供的Pemreader读证书
PemReader pemReader = new PemReader( new FileReader( "E:\\认真的侯颖好好看(课内)\\大三下\\课设\\certificate\\test.pem" ) );
PemObject pemObject = pemReader.readPemObject();
byte cert[] = pemObject.getContent();
//输出X509格式证书
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");//获取工厂实例
Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(cert));//用文件流读入证书
X509Certificate x509Certificate = (X509Certificate)certificate;
System.out.println(x509Certificate);
return x509Certificate;
//读密钥
PemReader pemReader_key = new PemReader(new FileReader(path));
PemObject pemObject1 = pemReader_key.readPemObject();
byte[] keyByte = pemObject1.getContent();
//加密私钥
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyByte);//使用给定的编码密钥创建新的 PKCS8EncodedKeySpec
KeyFactory keyFactory = KeyFactory.getInstance("RSA");//创建一个密钥工厂对象就可以按照要求生成一个RSA私钥
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
//读PFX证书,用密钥解密证书
FileInputStream fs = new FileInputStream(path);
PKCS12KeyStore store = new PKCS12KeyStore();
store.engineLoad(fs,passwd);//从给定的输入流加载密钥库
fs.close();
//生成x509格式读证书
Enumeration enumas = store.engineAliases();
String keyAlias = null;
if (enumas.hasMoreElements())
{
keyAlias = (String)enumas.nextElement();
System.out.println("alias=[" + keyAlias + "]");
}
Certificate cert = store.engineGetCertificate(keyAlias);
X509Certificate x509Certificate = (X509Certificate)cert;
System.out.println(x509Certificate);
return x509Certificate;
byte[] cert = x509Certificate.getEncoded();
File file=new File(path);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(cert);
fos.close();
System.out.println("Successful!");
//加载BC
Security.addProvider( new BouncyCastleProvider() );
//BC转化PEM格式证书
StringWriter str = new StringWriter();
PemWriter pemWriter = new PemWriter(str);
pemWriter.writeObject(new PemObject("CERTIFICATE",x509Certificate.getEncoded()));
pemWriter.close();
str.close();
//写入文件
FileOutputStream fos = new FileOutputStream(path);
fos.write(str.toString().getBytes());
System.out.println("Successful!");
//证书链
X509Certificate[] chain = new X509Certificate[1];
chain[0]= x509Certificate;
//PFX证书转换
PKCS12KeyStore store = new PKCS12KeyStore();
store.engineSetCertificateEntry( "CA‘s Primary Certificate", x509Certificate );
store.engineSetKeyEntry( "CA‘s Primary Certificate",privateKey, storePwd,chain);
FileOutputStream out = new FileOutputStream( path );
store.engineStore(out,storePwd );
out.close();
System.out.println("Successful!");
类 X509Certificate
Bouncy Castle 配置
BouncyCastle配置
2020_1课程设计—基于BC的证书格式转换工具的设计与实现—Week3
标签:read 转换 java set private element pen reader 解密
原文地址:https://www.cnblogs.com/orii/p/12801656.html