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;
}
}
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;
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations