本文整理汇总了Java中org.bouncycastle.openssl.PEMWriter.writeObject方法的典型用法代码示例。如果您正苦于以下问题:Java PEMWriter.writeObject方法的具体用法?Java PEMWriter.writeObject怎么用?Java PEMWriter.writeObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.openssl.PEMWriter
的用法示例。
在下文中一共展示了PEMWriter.writeObject方法的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: 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();
}
示例5: 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();
}
示例6: 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();
}
示例7: 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();
}
示例8: 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();
}
示例9: 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();
}
示例10: 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();
}
示例11: writeKeyPair
import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
public static String writeKeyPair(KeyPair kp) throws Exception {
StringWriter stringWriter = new StringWriter();
PEMWriter w = new PEMWriter(stringWriter);
w.writeObject(kp);
w.flush();
IOUtils.closeQuietly(w);
return stringWriter.toString();
}
示例12: writeKeyPair
import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
protected String writeKeyPair(KeyPair kp) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PEMWriter w = new PEMWriter(new OutputStreamWriter(baos));
w.writeObject(kp);
w.flush();
IOUtils.closeQuietly(w);
return Base64.encodeBase64String(baos.toByteArray());
}
示例13: getPemBytes
import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
public byte[] getPemBytes(final Object o) throws IOException {
LOG.debug(String.format("getPemBytes(%s)", o));
ByteArrayOutputStream pemByteOut = new ByteArrayOutputStream();
PEMWriter pemOut = new PEMWriter(new OutputStreamWriter(pemByteOut));
pemOut.writeObject(o);
pemOut.close();
return pemByteOut.toByteArray();
}
示例14: doWriteReadTest
import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void doWriteReadTest(
PrivateKey akp,
String provider)
throws IOException
{
StringWriter sw = new StringWriter();
PEMWriter pw = new PEMWriter(sw, provider);
pw.writeObject(akp);
pw.close();
String data = sw.toString();
PEMReader pr = new PEMReader(new StringReader(data));
Object o = pr.readObject();
if (o == null || !(o instanceof KeyPair))
{
fail("Didn't find OpenSSL key");
}
KeyPair kp = (KeyPair) o;
PrivateKey privKey = kp.getPrivate();
if (!akp.equals(privKey))
{
fail("Failed to read back test");
}
}
示例15: encryptedTestNew
import org.bouncycastle.openssl.PEMWriter; //导入方法依赖的package包/类
private void encryptedTestNew(PrivateKey key, ASN1ObjectIdentifier algorithm)
throws NoSuchProviderException, NoSuchAlgorithmException, IOException, OperatorCreationException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PEMWriter pWrt = new PEMWriter(new OutputStreamWriter(bOut), "BC");
JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(algorithm);
encryptorBuilder.setProvider("BC");
encryptorBuilder.setPasssword("hello".toCharArray());
PKCS8Generator pkcs8 = new JcaPKCS8Generator(key, encryptorBuilder.build());
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);
}