以下是显示如何使用 org.bouncycastle.openssl.PEMWriter 的最佳投票示例。这些示例是从开源项目中提取的。您可以对您喜欢的示例进行投票,您的投票将在我们的系统中使用,以生成更多好的示例。
示例一 保存密钥和证书到文件中
/**
* 保存私钥和证书至文件
* @throws Exception
*/
protected void saveKeyPairAndCertificateToFile() throws Exception {
if(localPrivateKeyFile==null){
LOGGER.info(“not saving private key nor certificate”);
return;
}
//Encode in PEM format, the format prefered by openssl
// if(false){
// PEMWriter pemWriter=new PEMWriter(new FileWriter(localPrivateKeyFile));
// pemWriter.writeObject(localPrivateECKey);
// pemWriter.close();
// }
// else{
String keyText = “—–BEGIN EC PRIVATE KEY—–\n” +
Base64.encode(Unpooled.wrappedBuffer(localPrivateECKey.getEncoded()), true).toString(CharsetUtil.US_ASCII) +
“\n—–END EC PRIVATE KEY—–\n”;
Files.write(keyText, localPrivateKeyFile, CharsetUtil.US_ASCII);
Files.write(localId.toString(), new File(localPrivateKeyFile.getParentFile(), “localPublic.hash”), CharsetUtil.US_ASCII);
// }
PEMWriter certificateWriter=new PEMWriter(new FileWriter(localCertificateFile));
certificateWriter.writeObject(cert);
certificateWriter.close();
LOGGER.info(“Saved to “+localCertificateFile.getAbsolutePath());
}
示例二:对私钥进行加密
/**
* 加密私钥
*
* @param key 私钥对象
* @param algorithm 密钥算法
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws IOException
*/
private void encryptedTest(PrivateKey key, ASN1ObjectIdentifier algorithm)
throws NoSuchProviderException, NoSuchAlgorithmException, IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut), “BC”);
PKCS8Generator pkcs8 = new PKCS8Generator(key, algorithm, “BC”);
pkcs8.setPassword(“hello”.toCharArray());
pWrt.writeObject(pkcs8);
pWrt.close();
PEMReader pRd = new PEMReader(new InputStreamReader(new ByteArrayInputStream(bOut.toByteArray())), new PasswordFinder() {
public char[] getPassword() {
return “hello”.toCharArray();
}
});
PrivateKey rdKey = (PrivateKey) pRd.readObject();
assertEquals(key, rdKey);
}
示例三 转换 rsa 的私钥为 pem 字符串
/**
* 转换 rsa 的私钥为 pem 字符串
*
* @param rsaKeyPair RSA 类型 keypair
* @return PEM string
*/
public static String getPEMStringFromRSAKeyPair(RSAKeyPair rsaKeyPair) {
StringWriter pemStrWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(pemStrWriter);
try {
KeyPair keyPair = new KeyPair(rsaKeyPair.getPublic(), rsaKeyPair.getPrivate());
//pemWriter.writeObject(keyPair);
pemWriter.writeObject(keyPair.getPrivate());
//pemWriter.flush();
pemWriter.close();
} catch (IOException e) {
log.warning(“Caught exception:” + e.getMessage());
return “”;
}
return pemStrWriter.toString();
}
示例四 将 pem 数据对象转换成 pem 格式文件数据
/**
* 将 pem 数据对象转换成 pem 格式文件数据
* @param object
* @return
* @throws IOException
*/
public static byte[] toPem(Object object) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (PEMWriter writer = new PEMWriter(new OutputStreamWriter(outputStream))) {
writer.writeObject(object);
writer.flush();
return outputStream.toByteArray();
}
}
示例五 将多份 certificate 对象写入文件
private void writeCertificate(Certificate… certificates)
throws IOException {
final PEMWriter writer = new PEMWriter(new FileWriter(destfile));
for (final Certificate c : certificates) {
writer.writeObject(c);
}
writer.close();
}
示例六 将 X509Certificate 转换成 pem 格式数据
public String x509CertificateToPem(final X509Certificate cert) throws IOException {
final StringWriter sw = new StringWriter();
try (final PEMWriter pw = new PEMWriter(sw)) {
pw.writeObject(cert);
}
return sw.toString();
}
示例七 将 rsa 私钥对象转换为 PEM 格式数据
public String rsaPrivateKeyToPem(final PrivateKey key) throws IOException {
final PemObject pemObject = new PemObject(CCS_RSA_PRIVATE_KEY, key.getEncoded());
final StringWriter sw = new StringWriter();
try (final PEMWriter pw = new PEMWriter(sw)) {
pw.writeObject(pemObject);
}
return sw.toString();
}
示例八 将私钥、证书文件等转换为 PEM 数据
private static byte[] getPemBytes(Object… objects) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (PEMWriter pemWriter =
new PEMWriter(new OutputStreamWriter(byteArrayOutputStream, UTF_8))) {
for (Object object : objects) {
pemWriter.writeObject(object);
}
}
return byteArrayOutputStream.toByteArray();
}
示例九 将 X509Certificate 转换为 PEM 数据
private static String toPem(X509Certificate certificate) throws IOException {
StringWriter stringWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(stringWriter, BouncyCastleProvider.PROVIDER_NAME);
pemWriter.writeObject(certificate);
pemWriter.close();
return stringWriter.toString();
}
示例十 将多个 证书数据 写入文件
private void writeCertificate(Certificate… certificates)
throws IOException {
final PEMWriter writer = new PEMWriter(new FileWriter(destfile));
for (final Certificate c : certificates) {
writer.writeObject(c);
}
writer.close();
}
示例十一 将 keyPair 转换成 Pem 格式
private String keyPairToString(KeyPair keyPair) {
StringWriter stringWriter = new StringWriter();
PEMWriter pemWriter = new PEMWriter(stringWriter);
try {
pemWriter.writeObject(keyPair);
pemWriter.flush();
pemWriter.close();
} catch (IOException e) {
throw new RuntimeException(“Unexpected IOException: ”
+ e.getMessage(), e);
}
return stringWriter.getBuffer().toString();
}