Example usage for java.security.spec X509EncodedKeySpec getEncoded

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

Introduction

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

Prototype

public byte[] getEncoded() 

Source Link

Document

Returns the key bytes, encoded according to the X.509 standard.

Usage

From source file:net.nicholaswilliams.java.licensing.TestLicenseManager.java

@BeforeClass
public static void setUpClass() throws Exception {
    TestLicenseManager.control = EasyMock.createStrictControl();

    TestLicenseManager.licenseProvider = TestLicenseManager.control.createMock(LicenseProvider.class);
    TestLicenseManager.publicKeyPasswordProvider = TestLicenseManager.control
            .createMock(PasswordProvider.class);
    TestLicenseManager.licensePasswordProvider = TestLicenseManager.control.createMock(PasswordProvider.class);
    TestLicenseManager.keyDataProvider = TestLicenseManager.control.createMock(PublicKeyDataProvider.class);
    TestLicenseManager.licenseValidator = TestLicenseManager.control.createMock(LicenseValidator.class);

    try {// w  w w. ja  v a 2 s .  c o m
        LicenseManager.getInstance();
        fail("Expected java.lang.IllegalArgumentException, got no exception.");
    } catch (IllegalArgumentException ignore) {
    }

    LicenseManagerProperties.setLicenseProvider(TestLicenseManager.licenseProvider);

    try {
        LicenseManager.getInstance();
        fail("Expected java.lang.IllegalArgumentException, got no exception.");
    } catch (IllegalArgumentException ignore) {
    }

    LicenseManagerProperties.setPublicKeyDataProvider(TestLicenseManager.keyDataProvider);

    try {
        LicenseManager.getInstance();
        fail("Expected java.lang.IllegalArgumentException, got no exception.");
    } catch (IllegalArgumentException ignore) {
    }

    LicenseManagerProperties.setPublicKeyPasswordProvider(TestLicenseManager.publicKeyPasswordProvider);
    LicenseManagerProperties.setLicensePasswordProvider(TestLicenseManager.licensePasswordProvider);
    LicenseManagerProperties.setLicenseValidator(TestLicenseManager.licenseValidator);
    LicenseManagerProperties.setCacheTimeInMinutes(0);

    LicenseManager.getInstance();

    KeyPair keyPair = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair();

    TestLicenseManager.privateKey = keyPair.getPrivate();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
    IOUtils.write(Encryptor.encryptRaw(x509EncodedKeySpec.getEncoded(), TestLicenseManager.keyPassword),
            outputStream);
    TestLicenseManager.encryptedPublicKey = outputStream.toByteArray();
}

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

public void storePublicKey(String path, PublicKey publicKey) {
    FileOutputStream fos = null;//  www.j a v  a  2s  .  co m
    try {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
        fos = new FileOutputStream(path);
        fos.write(x509EncodedKeySpec.getEncoded());
    } catch (FileNotFoundException e) {
        LOG.error("Cannot save the public key to the specified path.", e);
    } catch (IOException e) {
        LOG.error("An I/O error occured while saving the public key", e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}

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

private void savePublicKey(String file, PublicKey publicKey) throws IOException {
    // Store Public Key.
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(x509EncodedKeySpec.getEncoded());
    fos.close();/*from  w  w w  .ja  va  2  s . c  o m*/
}

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

public static String savePublicKey(PublicKey key) {
    try {/*from   w  ww.j  av a 2s .  com*/
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.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  . c  om*/
 * @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());
}