本文整理汇总了Java中org.bouncycastle.openssl.PEMWriter类的典型用法代码示例。如果您正苦于以下问题:Java PEMWriter类的具体用法?Java PEMWriter怎么用?Java PEMWriter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PEMWriter类属于org.bouncycastle.openssl包,在下文中一共展示了PEMWriter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveKeyPairAndCertificateToFile
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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());
}
示例2: encryptedTest
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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);
}
示例3: getPEMStringFromRSAKeyPair
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
/**
* Converts RSA private key to PEM string.
*
* @param rsaKeyPair
*
* @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();
}
示例4: toPem
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
@SuppressWarnings("deprecation")
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();
}
}
示例5: writeCertificate
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例6: x509CertificateToPem
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例7: rsaPrivateKeyToPem
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例8: getPemBytes
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例9: toPem
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例10: getPEM
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
private String getPEM(Key key) {
StringWriter sw = new StringWriter(1024);
PEMWriter pw = new PEMWriter(sw);
try {
pw.writeObject(key);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return sw.getBuffer().toString();
}
示例11: keyPairToString
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
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();
}
示例12: getPEMCertificate
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
@NotNull
public String getPEMCertificate(@NotNull String name, String alias) throws KeyStoreException, IOException {
Certificate certificate = Optional.ofNullable(getKeyStore(name).getKeyStore().getCertificate(alias))
.orElseThrow(() -> new IllegalArgumentException("Cannot find certificate for alias: " + alias));
StringWriter writer = new StringWriter();
PEMWriter pemWriter = new PEMWriter(writer);
pemWriter.writeObject(certificate);
pemWriter.flush();
return writer.getBuffer().toString();
}
示例13: getInPemFormat
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
private static String getInPemFormat(PrivateKey privateKey)
throws IOException {
final StringWriter stringWriter = new StringWriter();
final PEMWriter pemWriter = new PEMWriter(stringWriter);
pemWriter.writeObject(privateKey);
pemWriter.flush();
pemWriter.close();
return stringWriter.toString();
}
示例14: getPEMCertificate
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
@NotNull
public String getPEMCertificate(@NotNull String name, String alias) throws KeyStoreException, IOException {
Certificate certificate = ofNullable(getKeyStore(name).getKeyStore().getCertificate(alias))
.orElseThrow(() -> new IllegalArgumentException("Cannot find certificate for alias: " + alias));
StringWriter writer = new StringWriter();
PEMWriter pemWriter = new PEMWriter(writer);
pemWriter.writeObject(certificate);
pemWriter.flush();
return writer.getBuffer().toString();
}
示例15: convertToPEMString
import org.bouncycastle.openssl.PEMWriter; //导入依赖的package包/类
public String convertToPEMString(X509Certificate x509Cert) throws IOException {
StringWriter sw = new StringWriter();
try (PEMWriter pw = new PEMWriter(sw)) {
pw.writeObject(x509Cert);
}
return sw.toString();
}