List of usage examples for java.security.spec X509EncodedKeySpec getEncoded
public byte[] getEncoded()
From source file:com.completetrsst.crypto.Common.java
/** * Converts an EC PublicKey to an X509-encoded string. *//*from w ww . j a v a 2 s .c o m*/ public static String toX509FromPublicKey(PublicKey publicKey) throws GeneralSecurityException { KeyFactory factory = KeyFactory.getInstance("EC"); X509EncodedKeySpec spec = factory.getKeySpec(publicKey, X509EncodedKeySpec.class); return new Base64(0, null, true).encodeToString(spec.getEncoded()); }
From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java
protected static byte[] writeEncryptedPublicKey(PublicKey publicKey, char[] passphrase) { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); return Encryptor.encryptRaw(x509EncodedKeySpec.getEncoded(), passphrase); }
From source file:Main.java
/** * save private key and public key of a keypair in the directory * * @param dir/* w w w.j ava 2 s.c o m*/ * @param keyPair * @param name keys will be stored as name_private.key and name_public.key * @throws IOException */ public static void saveKeyPair(File dir, KeyPair keyPair, String name) throws IOException { // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); FileOutputStream fos = new FileOutputStream(new File(dir, name + "_public.key")); fos.write(x509EncodedKeySpec.getEncoded()); fos.close(); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded()); fos = new FileOutputStream(new File(dir, name + "_private.key")); fos.write(pkcs8EncodedKeySpec.getEncoded()); fos.close(); }
From source file:com.joyent.manta.client.crypto.SecretKeyUtils.java
/** * Writes the specified key in X509 encoding to a stream. Note: This method * doesn't close the supplied stream./*from w w w . j a va2 s .c o m*/ * * @param key key to write * @param out output stream to write to * @throws IOException thrown when there is a problem writing the key */ public static void writeKey(final SecretKey key, final OutputStream out) throws IOException { Validate.notNull(key, "Key must not be null"); Validate.notNull(out, "OutputStream must not be null"); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key.getEncoded()); out.write(x509EncodedKeySpec.getEncoded()); }
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Saves the key pair to the disk.// ww w . j a v a2 s. co m * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); out.close(); // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); out.close(); }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk./* w ww .java 2s. c om*/ * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = null; try { out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); try { out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } }
From source file:com.intuit.s3encrypt.S3Encrypt.java
public static void saveKeyPair(String filename, KeyPair keyPair) throws IOException { PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // Save public key to file. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream keyfos = new FileOutputStream(filename + ".pub"); keyfos.write(x509EncodedKeySpec.getEncoded()); keyfos.close();/*from w w w. j av a2 s .c om*/ // Save private key to file. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); keyfos = new FileOutputStream(filename); keyfos.write(pkcs8EncodedKeySpec.getEncoded()); keyfos.close(); }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
private static void saveKeys(PrivateKey privkey, PublicKey pubkey) { km_log.i("Saving keys to disk."); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkey.getEncoded()); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privkey.getEncoded()); try {/* w ww . jav a 2 s . co m*/ km_log.i(pubkeyFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubkeyFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key!"); e.printStackTrace(); } try { km_log.i(privkeyFile.createNewFile() ? "Creation of private key file successful." : "Creation of private key file failed!"); FileOutputStream privOut = new FileOutputStream(privkeyFile); privOut.write(Base64.encodeBase64(privSpec.getEncoded())); privOut.flush(); privOut.close(); } catch (IOException e) { km_log.e("Error while saving private key!"); e.printStackTrace(); } km_log.i("Finished saving keys to disk. Operation appears successful."); }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static void saveKeyByIP(String ip_address, PublicKey publicKey) { km_log.i("Saving public key for " + ip_address); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(publicKey.getEncoded()); File pubFile = new File(installationPath + File.separator + ip_address + ".pub"); km_log.i("Deleting old key if it exists."); if (pubFile.exists()) pubFile.delete();/*from w w w . j a v a 2 s .c o m*/ try { km_log.i(pubFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key for " + ip_address + "!"); e.printStackTrace(); } }
From source file:ee.ria.xroad.common.util.CryptoUtils.java
/** * Generates X509 encoded public key bytes from a given public key. * @param publicKey the public key/*from w w w . ja v a2 s . com*/ * @return generated public key bytes * @throws Exception if any errors occur */ public static byte[] generateX509PublicKey(PublicKey publicKey) throws Exception { X509EncodedKeySpec x509EncodedPublicKey = KEY_FACTORY.getKeySpec(publicKey, X509EncodedKeySpec.class); return x509EncodedPublicKey.getEncoded(); }