Java Code Examples for org.bouncycastle.util.io.pem.PemWriter#writeObject()
The following examples show how to use
org.bouncycastle.util.io.pem.PemWriter#writeObject() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CryptoUtil.java From julongchain with Apache License 2.0 | 6 votes |
/** * 公钥文件生成 * @param path * @param content */ public static void publicKeyFileGen(String path, byte[] content) { PemObject pemObject = new PemObject("PUBLIC KEY", content); StringWriter str = new StringWriter(); PemWriter pemWriter = new PemWriter(str); try { pemWriter.writeObject(pemObject); pemWriter.close(); str.close(); PrintWriter pw = new PrintWriter(new FileOutputStream(path + PK)); String publiKey = new String(str.toString()); pw.print(publiKey); pw.close(); } catch (IOException e) { e.printStackTrace(); } }
Example 2
Source File: CryptoUtil.java From julongchain with Apache License 2.0 | 6 votes |
/** * 私钥文件生成 * @param path * @param content */ public static void privateKeyFileGen(String path, byte[] content) { PemObject pemObject = new PemObject("PRIVATE KEY", content); StringWriter str = new StringWriter(); PemWriter pemWriter = new PemWriter(str); try { pemWriter.writeObject(pemObject); pemWriter.close(); str.close(); PrintWriter pw = new PrintWriter(new FileOutputStream(path + SK)); String publiKey = new String(str.toString()); pw.print(publiKey); pw.close(); } catch (IOException e) { e.printStackTrace(); } }
Example 3
Source File: MessageStatusCli.java From protect with MIT License | 6 votes |
private static void writeObject(final Key key, final PemWriter writer) throws IOException { final String description; if (key instanceof RSAPrivateKey) { description = "PAILLIER PRIVATE KEY"; } else if (key instanceof RSAPublicKey) { description = "PAILLIER PUBLIC KEY"; } else if (key instanceof ECPrivateKey) { description = "EC PRIVATE KEY"; } else if (key instanceof ECPublicKey) { description = "EC PUBLIC KEY"; } else if (key instanceof EdDSAPrivateKey) { description = "ED25519 PRIVATE KEY"; } else if (key instanceof EdDSAPublicKey) { description = "ED25519 PUBLIC KEY"; } else if (key instanceof PrivateKey) { description = "PRIVATE KEY"; } else if (key instanceof PublicKey) { description = "PUBLIC KEY"; } else { description = "KEY"; } writer.writeObject(new PemObject(description, key.getEncoded())); }
Example 4
Source File: CertGen.java From snowblossom with Apache License 2.0 | 6 votes |
public static ByteString pemCode(byte[] encoded, String type) { try { PemObject po = new PemObject(type, encoded); ByteArrayOutputStream b_out = new ByteArrayOutputStream(); PemWriter w = new PemWriter( new OutputStreamWriter(b_out)); w.writeObject(po); w.flush(); w.close(); return ByteString.copyFrom(b_out.toByteArray()); } catch(java.io.IOException e) { throw new RuntimeException(e); } }
Example 5
Source File: PemFile.java From bouncycastle-rsa-pem-write with MIT License | 5 votes |
public void write(String filename) throws FileNotFoundException, IOException { PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(filename))); try { pemWriter.writeObject(this.pemObject); } finally { pemWriter.close(); } }
Example 6
Source File: Ed25519PrivateKey.java From hedera-sdk-java with Apache License 2.0 | 5 votes |
/** * Write out a PEM encoded version of this private key. * * @deprecated for removal; exporting unencrypted PEMs is very insecure and has dubious * utility. */ @Deprecated public void writePem(Writer out) throws IOException { final PemWriter pemWriter = new PemWriter(out); pemWriter.writeObject(new PemObject(PemUtils.TYPE_PRIVATE_KEY, encodeDER())); pemWriter.flush(); }
Example 7
Source File: Main.java From keystore-decryptor with Apache License 2.0 | 5 votes |
private static void showJcaPrivateKey(PrivateKey pk) throws Exception { if (pk instanceof RSAPrivateKey) { RSAPrivateKey rsaPrivKey = (RSAPrivateKey) pk; PemObject rsaPem = new PemObject("RSA PRIVATE KEY", rsaPrivKey.getEncoded()); StringWriter sw = new StringWriter(); PemWriter pemWriter = new PemWriter(sw); try { pemWriter.writeObject(rsaPem); } finally { pemWriter.close(); } System.out.println(sw.toString()); } else if (pk instanceof java.security.interfaces.ECPrivateKey) { java.security.interfaces.ECPrivateKey ecPrivKey = (java.security.interfaces.ECPrivateKey) pk; System.out.printf("EC S: %s... (%d)\n", ecPrivKey.getS().toString(16).substring(0, 32), ecPrivKey.getS().bitLength()); if (ecPrivKey.getParams() instanceof ECNamedCurveSpec) { ECNamedCurveSpec namedCurveSpec = (ECNamedCurveSpec) ecPrivKey.getParams(); System.out.println("curve name: " + namedCurveSpec.getName()); } else { System.out.println("EC params: " + ecPrivKey.getParams()); } } else if (pk instanceof DSAPrivateKey) { DSAPrivateKey dsaPrivKey = (DSAPrivateKey) pk; System.out.printf("DSA X: %s... (%d)\n", dsaPrivKey.getX().toString(16).substring(0, 32), dsaPrivKey.getX() .bitLength()); System.out.println("DSA params: " + dsaPrivKey.getParams()); } else { System.out.println("Unknown private key type: " + pk.getClass().getName()); } }
Example 8
Source File: Main.java From keystore-decryptor with Apache License 2.0 | 5 votes |
private static void showCert(KeystoreBlob ksBlob) throws Exception { X509Certificate cert = ksBlob.getCertificate(); PemObject certPem = new PemObject("CERTIFICATE", cert.getEncoded()); StringWriter sw = new StringWriter(); PemWriter pemWriter = new PemWriter(sw); try { pemWriter.writeObject(certPem); } finally { pemWriter.close(); } System.out.println(sw.toString()); }
Example 9
Source File: KeycloakOauthPolicyLegacyTest.java From apiman-plugins with Apache License 2.0 | 5 votes |
private String certificateAsPem(X509Certificate x509) throws CertificateEncodingException, IOException { StringWriter sw = new StringWriter(); PemWriter writer = new PemWriter(sw); PemObject pemObject = new PemObject("CERTIFICATE", x509.getEncoded()); try { writer.writeObject(pemObject); writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { writer.close(); } return sw.toString(); }
Example 10
Source File: KeycloakOauthPolicyTest.java From apiman-plugins with Apache License 2.0 | 5 votes |
private String certificateAsPem(X509Certificate x509) throws CertificateEncodingException, IOException { StringWriter sw = new StringWriter(); PemWriter writer = new PemWriter(sw); PemObject pemObject = new PemObject("CERTIFICATE", x509.getEncoded()); try { writer.writeObject(pemObject); writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { writer.close(); } return sw.toString(); }
Example 11
Source File: BCECUtil.java From jiguang-java-client-common with MIT License | 5 votes |
private static String convertEncodedDataToPEM(String type, byte[] encodedData) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut)); try { PemObject pemObj = new PemObject(type, encodedData); pWrt.writeObject(pemObj); } finally { pWrt.close(); } return new String(bOut.toByteArray()); }
Example 12
Source File: PEMUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
public static String toPEM(PrivateKey privateKey) throws IOException { StringWriter stringWriter = new StringWriter(); PemWriter pemWriter = new PemWriter(stringWriter); try { pemWriter.writeObject(new JcaMiscPEMGenerator(privateKey)); pemWriter.flush(); } finally { pemWriter.close(); } return stringWriter.toString(); }
Example 13
Source File: PEMUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
public static String toPEM(X509Certificate certificate) throws IOException { StringWriter stringWriter = new StringWriter(); PemWriter pemWriter = new PemWriter(stringWriter); try { pemWriter.writeObject(new JcaMiscPEMGenerator(certificate)); pemWriter.flush(); } finally { pemWriter.close(); } return stringWriter.toString(); }
Example 14
Source File: Pem.java From protect with MIT License | 5 votes |
public static void writeObject(final Object object, final PemWriter writer) throws IOException, CertificateEncodingException { final String description; if (object instanceof Certificate) { description = "CERTIFICATE"; } else if (object instanceof RSAPrivateKey) { description = "PAILLIER PRIVATE KEY"; } else if (object instanceof RSAPublicKey) { description = "PAILLIER PUBLIC KEY"; } else if (object instanceof ECPrivateKey) { description = "EC PRIVATE KEY"; } else if (object instanceof ECPublicKey) { description = "EC PUBLIC KEY"; } else if (object instanceof EdDSAPrivateKey) { description = "ED25519 PRIVATE KEY"; } else if (object instanceof EdDSAPublicKey) { description = "ED25519 PUBLIC KEY"; } else if (object instanceof PrivateKey) { description = "PRIVATE KEY"; } else if (object instanceof PublicKey) { description = "PUBLIC KEY"; } else if (object instanceof Key) { description = "KEY"; } else { throw new IllegalArgumentException("Unknwon object type"); } final byte[] encoded = (object instanceof Key) ? ((Key) object).getEncoded() : ((Certificate) object).getEncoded(); writer.writeObject(new PemObject(description, encoded)); }
Example 15
Source File: Util.java From julongchain with Apache License 2.0 | 5 votes |
public static void pemExport(String path, String pemType, byte[] bytes) throws JulongChainException { File file = new File(path); if (file.exists()) { if (!file.delete()) { throw new JulongChainException("deleted the file unsuccessfully"); } } else { File dir = new File(file.getParent()); if (!dir.exists()) { if (!dir.mkdirs()) { throw new JulongChainException("made dir failed in method newCA"); } } } try { if (!file.createNewFile()) { throw new JulongChainException("created new file unsuccessfully"); } PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(file))); PemObject pemObject = new PemObject(pemType, bytes); pemWriter.writeObject(pemObject); pemWriter.close(); } catch (Exception e) { throw new JulongChainException("An error occurred on Util.pemExport :" + e.getMessage()); } }
Example 16
Source File: BCECUtil.java From littleca with Apache License 2.0 | 5 votes |
private static String convertDerEcDataToPem(String type, byte[] encodedData) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut)); try { PemObject pemObj = new PemObject(type, encodedData); pWrt.writeObject(pemObj); } finally { pWrt.close(); } return new String(bOut.toByteArray()); }
Example 17
Source File: BCECUtil.java From gmhelper with Apache License 2.0 | 5 votes |
private static String convertEncodedDataToPEM(String type, byte[] encodedData) throws IOException { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut)); try { PemObject pemObj = new PemObject(type, encodedData); pWrt.writeObject(pemObj); } finally { pWrt.close(); } return new String(bOut.toByteArray()); }
Example 18
Source File: PemUtils.java From hedera-sdk-java with Apache License 2.0 | 5 votes |
@SuppressWarnings("unused") public static void writeEncryptedPrivateKey(PrivateKeyInfo pkInfo, Writer out, String passphrase) throws IOException { byte[] salt = CryptoUtils.randomBytes(CryptoUtils.SALT_LEN); KeyParameter derivedKey = CryptoUtils.deriveKeySha256( passphrase, salt, CryptoUtils.ITERATIONS, CryptoUtils.CBC_DK_LEN); byte[] iv = CryptoUtils.randomBytes(CryptoUtils.IV_LEN); Cipher cipher = CryptoUtils.initAesCbc128Encrypt(derivedKey, iv); byte[] encryptedKey = CryptoUtils.runCipher(cipher, pkInfo.getEncoded()); // I wanted to just do this with BC's PKCS8Generator and KcePKCSPBEOutputEncryptorBuilder // but it tries to init AES instance of `Cipher` with a `PBKDF2Key` and the former complains // So this is basically a reimplementation of that minus the excess OO PBES2Parameters parameters = new PBES2Parameters( new KeyDerivationFunc( PKCSObjectIdentifiers.id_PBKDF2, new PBKDF2Params( salt, CryptoUtils.ITERATIONS, CryptoUtils.CBC_DK_LEN, new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA256))), new EncryptionScheme(NISTObjectIdentifiers.id_aes128_CBC, ASN1Primitive.fromByteArray(cipher.getParameters().getEncoded()))); EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo( new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, parameters), encryptedKey); PemWriter writer = new PemWriter(out); writer.writeObject(new PemObject(TYPE_ENCRYPTED_PRIVATE_KEY, encryptedPrivateKeyInfo.getEncoded())); writer.flush(); }