Example usage for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec

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

Introduction

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

Prototype

public PKCS8EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new PKCS8EncodedKeySpec with the given encoded key.

Usage

From source file:com.thoughtworks.go.security.RegistrationJSONizer.java

public static Registration fromJson(String json) {
    Map map = GSON.fromJson(json, Map.class);

    if (map.isEmpty()) {
        return Registration.createNullPrivateKeyEntry();
    }//from w w w. ja  v a2s  . com

    List<X509Certificate> chain = new ArrayList<>();
    try {
        PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        PrivateKey privateKey = kf.generatePrivate(spec);
        String agentCertificate = (String) map.get("agentCertificate");
        PemReader certReader = new PemReader(new StringReader(agentCertificate));
        while (true) {
            PemObject obj = certReader.readPemObject();
            if (obj == null) {
                break;
            }
            chain.add((X509Certificate) CertificateFactory.getInstance("X.509")
                    .generateCertificate(new ByteArrayInputStream(obj.getContent())));
        }
        return new Registration(privateKey, chain.toArray(new X509Certificate[0]));
    } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
        throw bomb(e);
    }
}

From source file:dk.nversion.jwt.CryptoUtils.java

public static PrivateKey loadPrivateKey(String filename)
        throws FileNotFoundException, IOException, InvalidKeySpecException, NoSuchAlgorithmException {
    PrivateKey key = null;//from   www  . ja  va 2s .c  o m
    InputStream is = null;
    try {
        is = new FileInputStream(filename);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        boolean inKey = false;

        String line;
        while ((line = br.readLine()) != null) {
            if (!inKey) {
                if (line.startsWith("-----BEGIN PRIVATE KEY-----")) {
                    inKey = true;
                }
            } else {
                if (line.startsWith("-----END PRIVATE KEY-----")) {
                    break;
                }
                builder.append(line);
            }
        }

        byte[] encoded = Base64.decodeBase64(builder.toString());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        key = kf.generatePrivate(keySpec);

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
                // Ignore
            }
        }
    }
    return key;
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?/*from  w  w  w  .j a v a2 s  .  co  m*/
 *
 * @param data
 *            ?
 * @param key
 *            ?
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {

    // ??
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ??
    PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

From source file:org.waveprotocol.wave.crypto.CertUtil.java

public static PrivateKey getPrivateKeyFromBytes(byte[] derKey) throws GeneralSecurityException {
    KeyFactory fac = KeyFactory.getInstance("RSA");
    EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(derKey);
    return fac.generatePrivate(privKeySpec);
}

From source file:com.vexsoftware.votifier.crypto.RSAIO.java

/**
 * Saves the key pair to the disk.//from   ww  w  .  jav  a2  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.github.ibole.infrastructure.security.ECDSA.java

public static void jdkECDSA() {
    try {//from  w ww .  j  a v  a 2 s . co  m
        // 1.?
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(256);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
        ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();

        // 2.??
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());

        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
        Signature signature = Signature.getInstance("SHA256withECDSA");
        signature.initSign(privateKey);
        signature.update(src.getBytes());
        byte[] res = signature.sign();
        System.out.println("??" + Base64.encodeBase64String(res));

        // 3.???
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecPublicKey.getEncoded());
        keyFactory = KeyFactory.getInstance("EC");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        signature = Signature.getInstance("SHA256withECDSA");
        signature.initVerify(publicKey);
        signature.update(src.getBytes());
        boolean bool = signature.verify(res);
        System.out.println("?" + bool);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jasig.cas.util.PrivateKeyFactoryBean.java

protected Object createInstance() throws Exception {
    final InputStream privKey = this.location.getInputStream();
    try {/*from ww w. j ava2  s  .c o  m*/
        final byte[] bytes = new byte[privKey.available()];
        privKey.read(bytes);
        privKey.close();
        final PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory factory = KeyFactory.getInstance(this.algorithm);
        return factory.generatePrivate(privSpec);
    } finally {
        privKey.close();
    }
}

From source file:com.searchbox.utils.DecryptLicense.java

public static byte[] decrypt(byte[] inpBytes) throws Exception {

    byte[] pkbytes = Base64.decodeBase64(privkey);
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pkbytes);
    PrivateKey pk = keyFactory.generatePrivate(privateKeySpec);

    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, pk);
    return cipher.doFinal(inpBytes);
}

From source file:aiai.apps.commons.utils.SecUtils.java

public static PrivateKey getPrivateKey(String keyBase64)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] keyBytes = Base64.decodeBase64(keyBase64);
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePrivate(spec);
}

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  ava2  s .  c  o  m*/
    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);
}