Search in sources :

Example 11 with PEMWriter

use of org.bouncycastle.openssl.PEMWriter in project MSEC by Tencent.

the class Tools method newRSAKeyAndSave.

public static KeyPair newRSAKeyAndSave() {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    if (new File("./priv.txt").exists() || new File("./pub.txt").exists()) {
        System.out.println("priv.txt/pub.txt exists, move away at first please.");
        return null;
    }
    try {
        byte[] input = "abc".getBytes();
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
        SecureRandom random = new SecureRandom();
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
        generator.initialize(1024, random);
        KeyPair pair = generator.generateKeyPair();
        Key pubKey = pair.getPublic();
        Key privKey = pair.getPrivate();
        FileWriter fw = new FileWriter("./priv.txt");
        PEMWriter pw = new PEMWriter(fw);
        pw.writeObject(privKey);
        pw.close();
        fw.close();
        FileWriter fw2 = new FileWriter("./pub.txt");
        PEMWriter pw2 = new PEMWriter(fw2);
        pw2.writeObject(pubKey);
        pw2.close();
        fw2.close();
        return pair;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : FileWriter(java.io.FileWriter) PEMWriter(org.bouncycastle.openssl.PEMWriter) Cipher(javax.crypto.Cipher) File(java.io.File)

Example 12 with PEMWriter

use of org.bouncycastle.openssl.PEMWriter in project gitblit by gitblit.

the class SshDaemon method generateKeyPair.

private void generateKeyPair(File file, String algorithm, int keySize) {
    if (file.exists()) {
        return;
    }
    try {
        KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator(algorithm);
        if (keySize != 0) {
            generator.initialize(keySize);
            log.info("Generating {}-{} SSH host keypair...", algorithm, keySize);
        } else {
            log.info("Generating {} SSH host keypair...", algorithm);
        }
        KeyPair kp = generator.generateKeyPair();
        // create an empty file and set the permissions
        Files.touch(file);
        try {
            JnaUtils.setFilemode(file, JnaUtils.S_IRUSR | JnaUtils.S_IWUSR);
        } catch (UnsatisfiedLinkError | UnsupportedOperationException e) {
        // Unexpected/Unsupported OS or Architecture
        }
        FileOutputStream os = new FileOutputStream(file);
        PEMWriter w = new PEMWriter(new OutputStreamWriter(os));
        w.writeObject(kp);
        w.flush();
        w.close();
    } catch (Exception e) {
        log.warn(MessageFormat.format("Unable to generate {0} keypair", algorithm), e);
        return;
    }
}
Also used : KeyPair(java.security.KeyPair) FileOutputStream(java.io.FileOutputStream) PEMWriter(org.bouncycastle.openssl.PEMWriter) OutputStreamWriter(java.io.OutputStreamWriter) KeyPairGenerator(java.security.KeyPairGenerator) IOException(java.io.IOException)

Example 13 with PEMWriter

use of org.bouncycastle.openssl.PEMWriter in project MonjaDB by Kanatoko.

the class MBCUtil method toPem.

// --------------------------------------------------------------------------------
public static String toPem(X509Certificate cert) {
    ByteArrayOutputStream bo = null;
    try {
        bo = new ByteArrayOutputStream();
        PEMWriter writer = new PEMWriter(new OutputStreamWriter(bo));
        writer.writeObject(cert);
        writer.flush();
        writer.close();
        byte[] _data = bo.toByteArray();
        String pemStr = new String(_data);
        return pemStr;
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    } finally {
        MStreamUtil.closeStream(bo);
    }
}
Also used : PEMWriter(org.bouncycastle.openssl.PEMWriter) CertificateException(java.security.cert.CertificateException)

Example 14 with PEMWriter

use of org.bouncycastle.openssl.PEMWriter in project platformlayer by platformlayer.

the class KeyPairUtils method serialize.

public static String serialize(KeyPair keyPair) throws IOException {
    StringWriter writer = new StringWriter();
    PEMWriter pemWriter = new PEMWriter(writer, BouncyCastleLoader.getName());
    try {
        pemWriter.writeObject(keyPair);
        pemWriter.flush();
        return writer.toString();
    } finally {
        IoUtils.safeClose(pemWriter);
    }
}
Also used : StringWriter(java.io.StringWriter) PEMWriter(org.bouncycastle.openssl.PEMWriter)

Example 15 with PEMWriter

use of org.bouncycastle.openssl.PEMWriter in project platformlayer by platformlayer.

the class CertificateUtils method toPem.

public static String toPem(Iterable<X509Certificate> certs) {
    try {
        StringWriter stringWriter = new StringWriter();
        PEMWriter writer = new PEMWriter(stringWriter, BouncyCastleLoader.getName());
        for (X509Certificate cert : certs) {
            writer.writeObject(cert);
        }
        writer.close();
        String s = stringWriter.toString();
        return s;
    } catch (IOException e) {
        throw new IllegalArgumentException("Error serializing certificates", e);
    }
}
Also used : StringWriter(java.io.StringWriter) PEMWriter(org.bouncycastle.openssl.PEMWriter) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate)

Aggregations

PEMWriter (org.bouncycastle.openssl.PEMWriter)15 StringWriter (java.io.StringWriter)8 IOException (java.io.IOException)7 X509Certificate (java.security.cert.X509Certificate)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 SecureRandom (java.security.SecureRandom)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)1 JSchException (com.jcraft.jsch.JSchException)1 SftpException (com.jcraft.jsch.SftpException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Writer (java.io.Writer)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyStore (java.security.KeyStore)1