@Test public void privateKey2EncryptionFile() throws FileNotFoundException { KeyPair keyPair = generatorKeyPair(); Converter.PrivateKey2EncryptionFile(new FileOutputStream("/Users/robert/encryption.pri"), "189213", keyPair.getPrivate()); }
public static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey) { JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new OutputStreamWriter(stream)); try { PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privateKey); PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = pkcs8EncryptedPrivateKeyInfoBuilder .build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) .setProvider(new BouncyCastleProvider()) .build(password.toCharArray())); jcaPEMWriter.writeObject(pkcs8EncryptedPrivateKeyInfo); jcaPEMWriter.close(); } catch (OperatorCreationException | IOException e) { e.printStackTrace(); } finally { try { jcaPEMWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
Converter { public static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey) { JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new OutputStreamWriter(stream)); try { PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privateKey); PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = pkcs8EncryptedPrivateKeyInfoBuilder .build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) .setProvider(new BouncyCastleProvider()) .build(password.toCharArray())); jcaPEMWriter.writeObject(pkcs8EncryptedPrivateKeyInfo); jcaPEMWriter.close(); } catch (OperatorCreationException | IOException e) { e.printStackTrace(); } finally { try { jcaPEMWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Converter { public static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey) { JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new OutputStreamWriter(stream)); try { PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privateKey); PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = pkcs8EncryptedPrivateKeyInfoBuilder .build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) .setProvider(new BouncyCastleProvider()) .build(password.toCharArray())); jcaPEMWriter.writeObject(pkcs8EncryptedPrivateKeyInfo); jcaPEMWriter.close(); } catch (OperatorCreationException | IOException e) { e.printStackTrace(); } finally { try { jcaPEMWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }  }
Converter { public static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey) { JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new OutputStreamWriter(stream)); try { PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privateKey); PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = pkcs8EncryptedPrivateKeyInfoBuilder .build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) .setProvider(new BouncyCastleProvider()) .build(password.toCharArray())); jcaPEMWriter.writeObject(pkcs8EncryptedPrivateKeyInfo); jcaPEMWriter.close(); } catch (OperatorCreationException | IOException e) { e.printStackTrace(); } finally { try { jcaPEMWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }  static void cert2File(X509Certificate x509Certificate, FileOutputStream fileOutputStream); static X509Certificate file2Cert(FileInputStream stream); static void PublicKey2File(FileOutputStream stream, PublicKey publicKey); static PublicKey File2PublicKey(FileInputStream stream, String algorithm); static void PrivateKey2File(FileOutputStream stream, PrivateKey privateKey); static PrivateKey File2PrivateKey(FileInputStream stream, String algorithm); static PKCS10CertificationRequest File2PKCS10CertificateRequest(FileInputStream stream); static void PKCS10CertificationRequest2File(PKCS10CertificationRequest request, FileOutputStream stream); static PrivateKey EncryptionPrivateKeyFile2PrivateKey(FileInputStream stream, String algorithm,
                                                                 String password); static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey); static SecretKey SecureKeyFile2SecureKey(FileInputStream stream, String algorithm, Provider provider); static void SecretKey2File(SecretKey secretKey, FileOutputStream fileOutputStream); static SecretKey String2SecureKey(String var1, String algorithm, Provider provider); static String PublicKey2String(PublicKey publicKey); static String PrivateKey2String(PrivateKey privateKey); static PublicKey String2PublicKey(String var, String algorithm); static PrivateKey String2PrivateKey(String var, String algorithm); }
Converter { public static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey) { JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new OutputStreamWriter(stream)); try { PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privateKey); PKCS8EncryptedPrivateKeyInfo pkcs8EncryptedPrivateKeyInfo = pkcs8EncryptedPrivateKeyInfoBuilder .build(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHA1AndDES_CBC) .setProvider(new BouncyCastleProvider()) .build(password.toCharArray())); jcaPEMWriter.writeObject(pkcs8EncryptedPrivateKeyInfo); jcaPEMWriter.close(); } catch (OperatorCreationException | IOException e) { e.printStackTrace(); } finally { try { jcaPEMWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }  static void cert2File(X509Certificate x509Certificate, FileOutputStream fileOutputStream); static X509Certificate file2Cert(FileInputStream stream); static void PublicKey2File(FileOutputStream stream, PublicKey publicKey); static PublicKey File2PublicKey(FileInputStream stream, String algorithm); static void PrivateKey2File(FileOutputStream stream, PrivateKey privateKey); static PrivateKey File2PrivateKey(FileInputStream stream, String algorithm); static PKCS10CertificationRequest File2PKCS10CertificateRequest(FileInputStream stream); static void PKCS10CertificationRequest2File(PKCS10CertificationRequest request, FileOutputStream stream); static PrivateKey EncryptionPrivateKeyFile2PrivateKey(FileInputStream stream, String algorithm,
                                                                 String password); static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey); static SecretKey SecureKeyFile2SecureKey(FileInputStream stream, String algorithm, Provider provider); static void SecretKey2File(SecretKey secretKey, FileOutputStream fileOutputStream); static SecretKey String2SecureKey(String var1, String algorithm, Provider provider); static String PublicKey2String(PublicKey publicKey); static String PrivateKey2String(PrivateKey privateKey); static PublicKey String2PublicKey(String var, String algorithm); static PrivateKey String2PrivateKey(String var, String algorithm);  }