List of usage examples for org.bouncycastle.openssl.jcajce JcaPEMWriter JcaPEMWriter
public JcaPEMWriter(Writer out)
From source file:com.hypersocket.certs.X509CertificateUtils.java
License:Open Source License
public static void saveKeyPair(KeyPair pair, OutputStream keyfile) throws CertificateException { JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(keyfile)); try {/* w w w .j a v a2s . c o m*/ writer.writeObject(pair.getPrivate()); writer.flush(); writer.close(); } catch (IOException e) { throw new CertificateException("Failed to save key pair", e); } }
From source file:com.hypersocket.certs.X509CertificateUtils.java
License:Open Source License
public static void saveCertificate(Certificate[] certs, OutputStream certfile) throws CertificateException { JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(certfile)); try {/*from w w w .j a v a 2s . c o m*/ for (Certificate c : certs) { writer.writeObject(c); } writer.flush(); writer.close(); } catch (IOException e) { throw new CertificateException("Failed to save certificate", e); } }
From source file:com.hypersocket.certs.X509CertificateUtils.java
License:Open Source License
public static byte[] generatePKCS10(PrivateKey privateKey, PublicKey publicKey, String CN, String OU, String O, String L, String S, String C) throws Exception { JcaContentSignerBuilder csb = new JcaContentSignerBuilder("SHA1withRSA"); ContentSigner cs = csb.build(privateKey); X500Principal principal = new X500Principal( "CN=" + CN + ", OU=" + OU + ", O=" + O + ", L=" + L + ", S=" + S + ", C=" + C); PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(principal, publicKey); PKCS10CertificationRequest req = builder.build(cs); ByteArrayOutputStream bout = new ByteArrayOutputStream(); JcaPEMWriter p = null;/*from w w w . j a va 2 s . c om*/ try { p = new JcaPEMWriter(new OutputStreamWriter(bout)); p.writeObject(req); } finally { if (p != null) p.close(); } return bout.toByteArray(); }
From source file:com.infinities.skyport.util.RSAUtil.java
License:Apache License
private static String extractPrivateKey(KeyPair keys) throws IOException { StringWriter writer = new StringWriter(); JcaPEMWriter pemWriter = null;//from w w w .ja va 2 s. c om try { pemWriter = new JcaPEMWriter(writer); pemWriter.writeObject(keys.getPrivate()); } finally { if (pemWriter != null) { pemWriter.close(); } } return writer.toString(); }
From source file:com.infinities.skyport.vnc.impl.PrivateKeyDecodedTest.java
License:Apache License
@Test public void testDecoded() throws NoSuchAlgorithmException, JSchException, InvalidKeySpecException, IOException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048);/*from www . ja v a 2 s. c o m*/ KeyPair keys = keyGen.genKeyPair(); StringWriter writer = new StringWriter(); JcaPEMWriter pemWriter = null; try { pemWriter = new JcaPEMWriter(writer); pemWriter.writeObject(keys.getPrivate()); } finally { if (pemWriter != null) { pemWriter.close(); } } String privateKeyStr = writer.toString(); System.out.println(privateKeyStr); CustomSession session = new CustomSession("pohsun", "127.0.0.1", 22, "password"); session.addIdentity("remote", privateKeyStr.getBytes(), null, ""); // com.jcraft.jsch.KeyPair.load(null, // privateKeyStr.getBytes("US-ASCII"), null); // PKCS8EncodedKeySpec privspec = new // PKCS8EncodedKeySpec(BaseEncoding.base64().decode(base64)); // KeyFactory factory = KeyFactory.getInstance("RSA"); // PrivateKey privkey = factory.generatePrivate(privspec); // byte[] decoded = BaseEncoding.base64().decode(base64); // CustomSession session = new CustomSession("pohsun", "127.0.0.1", 22); // session.addIdentity("remote", decoded.getBytes(), null, ""); }
From source file:com.joyent.manta.client.MantaClientAuthenticationChangeIT.java
License:Open Source License
private static void swapKeyContentPasswordness(final AuthAwareConfigContext config, final String password) throws IOException, NoSuchProviderException, NoSuchAlgorithmException { Validate.isTrue(config.getMantaKeyPath() == null, "Key path should be null when toggling key content password"); Validate.notBlank(config.getPrivateKeyContent(), "Key content should not be null"); if (password == null) { Validate.notNull(config.getPassword(), "Password removal requested but no password attached"); // removing password throw new AssertionError("Not yet implemented"); }/*from w w w . j a va 2s .c o m*/ // adding password // make sure the KeyPair is loaded before we try to serialize it with the provided password Assert.assertNotNull(config.getKeyPair()); final String keyAlgo = config.getKeyPair().getPrivate().getAlgorithm(); // we can only reliably password-protect a keypair if libnss is disabled OR it's an RSA key, otherwise just skip if (ExternalSecurityProviderLoader.getPkcs11Provider() == null || keyAlgo.equals("RSA")) { try (final StringWriter contentWriter = new StringWriter(); final JcaPEMWriter pemWriter = new JcaPEMWriter(contentWriter)) { final JcaMiscPEMGenerator keySerializer = new JcaMiscPEMGenerator(config.getKeyPair().getPrivate(), new JcePEMEncryptorBuilder("AES-128-CBC").setProvider("BC").build(password.toCharArray())); pemWriter.writeObject(keySerializer); pemWriter.flush(); config.setPrivateKeyContent(contentWriter.getBuffer().toString()); } } else { throw new SkipException(String.format( "Unsupported parameters for attaching passphrase: libnss enabled %s, key algorithm: %s", ExternalSecurityProviderLoader.getPkcs11Provider() != null, keyAlgo)); } config.setPassword(password); }
From source file:com.joyent.manta.config.TestConfigContext.java
License:Open Source License
/** * Some test cases need a direct reference to a KeyPair along with it's associated config. Manually calling * KeyPairFactory with a half-baked config can get cumbersome, so let's build a ConfigContext which has * everything ready and supplies the relevant KeyPair. * * @return the generated keypair and a config which uses a serialized version of that keypair *///from w ww .jav a2s. c o m public static ImmutablePair<KeyPair, BaseChainedConfigContext> generateKeyPairBackedConfig( final String passphrase) { final KeyPair keyPair; try { keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); } catch (final NoSuchAlgorithmException impossible) { throw new Error(impossible); // "RSA" is always provided } final Object keySerializer; if (passphrase != null) { try { keySerializer = new JcaMiscPEMGenerator(keyPair.getPrivate(), new JcePEMEncryptorBuilder("AES-128-CBC").build(passphrase.toCharArray())); } catch (IOException e) { throw new RuntimeException(e); } } else { keySerializer = keyPair.getPrivate(); } final String keyContent; try (final StringWriter content = new StringWriter(); final JcaPEMWriter writer = new JcaPEMWriter(content)) { writer.writeObject(keySerializer); writer.flush(); keyContent = content.toString(); } catch (IOException e) { throw new RuntimeException(e); } final BaseChainedConfigContext config = new ChainedConfigContext(DEFAULT_CONFIG) // we need to unset the key path in case one exists at ~/.ssh/id_rsa // see the static initializer in DefaultsConfigContext .setMantaKeyPath(null).setPrivateKeyContent(keyContent) .setMantaKeyId(KeyFingerprinter.md5Fingerprint(keyPair)); if (passphrase != null) { config.setPassword(passphrase); } return new ImmutablePair<>(keyPair, config); }
From source file:com.rovemonteux.silvertunnel.netlib.layer.tor.util.Encryption.java
License:Open Source License
/** * Converts RSA private key to PEM string. * * @param rsaKeyPair//from www . ja v a 2 s . com * @return PEM string */ public static String getPEMStringFromRSAKeyPair(final RSAKeyPair rsaKeyPair) { final StringWriter pemStrWriter = new StringWriter(); final JcaPEMWriter pemWriter = new JcaPEMWriter(pemStrWriter); try { final KeyPair keyPair = new KeyPair(rsaKeyPair.getPublic(), rsaKeyPair.getPrivate()); pemWriter.writeObject(keyPair.getPrivate()); pemWriter.close(); } catch (final IOException e) { LOG.warn("Caught exception:" + e.getMessage()); return ""; } return pemStrWriter.toString(); }
From source file:com.spotify.helios.client.tls.X509CertificateFactory.java
License:Apache License
private static String asPEMString(final Object o) throws IOException { final StringWriter sw = new StringWriter(); try (final JcaPEMWriter pw = new JcaPEMWriter(sw)) { pw.writeObject(o);/* w w w . j a v a 2 s.c om*/ } return sw.toString(); }
From source file:com.spotify.sshagenttls.Utils.java
License:Apache License
static String asPemString(final Object obj) throws IOException { final StringWriter sw = new StringWriter(); try (final JcaPEMWriter pw = new JcaPEMWriter(sw)) { pw.writeObject(obj);// w ww. ja v a2 s . co m } return sw.toString(); }