List of usage examples for java.security PrivateKey getEncoded
public byte[] getEncoded();
From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java
/** * PKCS #8 encode a private key./* w w w . j av a 2 s.c o m*/ * * @return The encoding * @param privateKey * The private key */ public static byte[] get(PrivateKey privateKey) { return privateKey.getEncoded(); }
From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java
/** * PKCS #8 encode a private key and PEM the encoding. * * @return The PEM'd encoding//from ww w . j ava 2 s . co m * @param privateKey * The private key */ public static String getPem(PrivateKey privateKey) { PemInfo pemInfo = new PemInfo(PKCS8_UNENC_PVK_PEM_TYPE, null, privateKey.getEncoded()); return PemUtil.encode(pemInfo); }
From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java
/** * Saves public and private keys to specified streams. * * @param keyPair the key pair// w ww . ja v a2 s. co m * @param privateKeyOutput the private key output stream * @param publicKeyOutput the public key output stream * @throws IOException Signals that an I/O exception has occurred. */ public static void saveKeyPair(KeyPair keyPair, OutputStream privateKeyOutput, OutputStream publicKeyOutput) throws IOException { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); publicKeyOutput.write(x509EncodedKeySpec.getEncoded()); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); privateKeyOutput.write(pkcs8EncodedKeySpec.getEncoded()); }
From source file:com.owncloud.android.utils.EncryptionUtils.java
public static String privateKeyToPEM(PrivateKey privateKey) { String privateKeyString = encodeBytesToBase64String(privateKey.getEncoded()); return "-----BEGIN PRIVATE KEY-----\n" + privateKeyString.replaceAll("(.{65})", "$1\n") + "\n-----END PRIVATE KEY-----"; }
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();/*ww w . j av a 2 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:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk./*from w w w .ja v a 2 s . com*/ * * @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.vmware.identity.idm.server.ServerUtils.java
public static LdapValue[] getLdapValue(PrivateKey key) { LdapValue[] value = null;//www. j a v a2 s. c om if (key != null) { byte[] bytes = key.getEncoded(); if ((bytes != null) && (bytes.length > 0)) { value = new LdapValue[] { new LdapValue(bytes) }; } } return value; }
From source file:org.apache.sshd.common.config.keys.loader.pem.PKCS8PEMResourceKeyPairParserTest.java
@Test // see SSHD-760 public void testPkcs8() throws IOException, GeneralSecurityException { KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator(algorithm); if (keySize > 0) { generator.initialize(keySize);/*from ww w .j a va 2 s.co m*/ } KeyPair kp = generator.generateKeyPair(); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { Collection<Object> items = new ArrayList<>(); PrivateKey prv1 = kp.getPrivate(); items.add(new PEMItem(prv1.getEncoded(), "PRIVATE KEY")); byte[] bytes = PEMUtil.encode(items); os.write(bytes); os.close(); try (ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { KeyPair kp2 = SecurityUtils.loadKeyPairIdentity(getCurrentTestName(), bais, null); assertEquals("Mismatched public key", kp.getPublic(), kp2.getPublic()); assertEquals("Mismatched private key", prv1, kp2.getPrivate()); } } }
From source file:com.bluepixel.security.manager.Server.java
private void generateKey() { try {/*from ww w . j a v a2 s . co m*/ KeyPairGenerator keyGen = KeyPairGenerator.getInstance(DEFAULT_ALGORITHM); keyGen.initialize(DEFAULT_KEY_LENGTH); KeyPair keypair = keyGen.generateKeyPair(); PublicKey pbKey = keypair.getPublic(); PrivateKey piKey = keypair.getPrivate(); publicKey = Base64.encodeWebSafe(pbKey.getEncoded(), false); privateKey = Base64.encodeWebSafe(piKey.getEncoded(), false); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, piKey); secretKeys = new ConcurrentHashMap<String, String>(); String[] randomKeys = generateRandomWords(10); for (String key : randomKeys) { String cipherText = Base64.encodeWebSafe(cipher.doFinal(key.getBytes()), false); secretKeys.put(key, cipherText); } } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } catch (NoSuchPaddingException e) { } catch (IllegalBlockSizeException e) { } catch (BadPaddingException e) { } }
From source file:org.jgrades.security.utils.KeyStoreContentExtractorTest.java
@Test public void shouldExtractPrivateKeyForSigning() throws Exception { // when// w w w .j a va 2 s. c o m PrivateKey privateKey = extractor.getPrivateKeyForSigning(); // then assertThat(privateKey).isNotNull(); assertThat(privateKey.getAlgorithm()).isEqualTo("RSA"); assertThat(privateKey.getEncoded()).isEqualTo(FileUtils.readFileToByteArray(signingPrivateKey)); }