Example usage for java.security.spec PKCS8EncodedKeySpec getEncoded

List of usage examples for java.security.spec PKCS8EncodedKeySpec getEncoded

Introduction

In this page you can find the example usage for java.security.spec PKCS8EncodedKeySpec getEncoded.

Prototype

public byte[] getEncoded() 

Source Link

Document

Returns the key bytes, encoded according to the PKCS #8 standard.

Usage

From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java

public void storePrivateKey(String path, PrivateKey privateKey) {
    FileOutputStream fos = null;/*w ww.j  a v  a2 s  .c o m*/
    try {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
        fos = new FileOutputStream(path);
        fos.write(pkcs8EncodedKeySpec.getEncoded());
    } catch (FileNotFoundException e) {
        LOG.error("Cannot save the private key to the specified path.", e);
    } catch (IOException e) {
        LOG.error("An I/O error occured while saving the private key", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

private void savePrivateKey(String file, PrivateKey privateKey) throws IOException {
    // Store Private Key.
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(pkcs8EncodedKeySpec.getEncoded());
    fos.close();//from  w ww .j  a  v  a  2s  .  c om
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static String savePrivateKey(PrivateKey key) {
    try {/*from ww  w .  j a  v  a  2  s.c o  m*/
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}

From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java

/**
 * Saves public and private keys to specified streams.
 *
 * @param keyPair          the key pair//from   w w  w  . jav a  2  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());
}