Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

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

Introduction

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

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:Main.java

public static PublicKey getPublicKey(String key) {
    try {/*from  ww w . jav  a  2 s . c o  m*/
        Base64 base64_decoder = new Base64();
        byte[] byteKey = base64_decoder.decode(key.getBytes()); // ,
        // Base64.DEFAULT);
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");

        return kf.generatePublic(X509publicKey);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.completetrsst.crypto.Common.java

/**
 * Converts a X509-encoded EC key to a PublicKey.
 *//*from w  w w .j a v  a  2s  .c om*/
public static PublicKey toPublicKeyFromX509(String stored) throws GeneralSecurityException {
    KeyFactory factory = KeyFactory.getInstance("EC");
    byte[] data = Base64.decodeBase64(stored);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
    return factory.generatePublic(spec);
}

From source file:zerogame.info.javapay.web.RSASignature.java

/**
* 
*/// w w  w  . ja  v a2  s.  c  o  m
public static PublicKey getPublicKey() throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    byte[] encodedKey = Base64.decodeBase64(RSA_PUBLIC);
    PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));

    return pubKey;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

private static PublicKey getRSAPublicKeyFrom(String content)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    PemReader reader = new PemReader(new StringReader(content));
    EncodedKeySpec spec = new X509EncodedKeySpec(reader.readPemObject().getContent());
    return KeyFactory.getInstance("RSA").generatePublic(spec);
}

From source file:Main.java

/**
 * save private key and public key of a keypair in the directory
 *
 * @param dir/*from w ww. j av a2 s. co 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.vexsoftware.votifier.crypto.RSAIO.java

/**
 * Saves the key pair to the disk./*  w w  w  . j ava  2  s.  c o 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.//from  ww  w  .  j  av a  2  s. c o 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 = 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:aiai.apps.commons.utils.SecUtils.java

public static PublicKey getPublicKey(String keyBase64)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] keyBytes = Base64.decodeBase64(keyBase64);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
}

From source file:com.xinferin.licensing.LicenceActivator.java

private void initialiseKeys() throws Exception {
    try {//from   ww  w .ja  va2 s  . c  o m

        byte[] publicKeyBytes = Base64.decodeBase64(spublicKey);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        publicKey = keyFactory.generatePublic(publicKeySpec);

    } catch (InvalidKeySpecException e) {
        throw new Exception("Invalid Key Specs not valid Key files." + e.getCause());
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There is no such algorithm. Please check the JDK ver." + e.getCause());
    }
}

From source file:Main.java

public static KeyPair loadKeyPair(Context context, String name)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Public Key.
    File filePublicKey = new File(context.getFilesDir(), name + "_public.key");
    FileInputStream fis = new FileInputStream(filePublicKey);
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);//from   w  w w . j  a v a  2s .c om
    fis.close();

    // Read Private Key.
    File filePrivateKey = new File(context.getFilesDir(), name + "_private.key");
    fis = new FileInputStream(filePrivateKey);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);
    fis.close();

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return new KeyPair(publicKey, privateKey);
}