Example usage for java.security KeyFactory generatePrivate

List of usage examples for java.security KeyFactory generatePrivate

Introduction

In this page you can find the example usage for java.security KeyFactory generatePrivate.

Prototype

public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException 

Source Link

Document

Generates a private key object from the provided key specification (key material).

Usage

From source file:oscar.oscarLab.ca.all.pageUtil.LabUploadAction.java

private static PrivateKey getServerPrivate() {

    PrivateKey Key = null;
    Base64 base64 = new Base64(0);
    byte[] privateKey;

    try {/*from   w  ww . j ava2 s .  c  o  m*/
        OscarKeyDao oscarKeyDao = (OscarKeyDao) SpringUtils.getBean("oscarKeyDao");
        OscarKey oscarKey = oscarKeyDao.find("oscar");
        logger.info("oscar key: " + oscarKey);

        privateKey = base64.decode(oscarKey.getPrivateKey().getBytes(MiscUtils.ENCODING));
        PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privateKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key = keyFactory.generatePrivate(privKeySpec);
    } catch (Exception e) {
        logger.error("Could not retrieve private key: ", e);
    }
    return (Key);
}

From source file:be.fedict.eid.idp.model.CryptoUtil.java

public static PrivateKey getPrivate(byte[] keyBytes) throws KeyLoadException {

    // try DSA/*from  ww  w.ja v  a2  s . co  m*/
    try {
        KeyFactory dsaKeyFactory = KeyFactory.getInstance("DSA");
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
        try {
            return dsaKeyFactory.generatePrivate(privateKeySpec);
        } catch (InvalidKeySpecException e) {
            // try RSA
            KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
            try {
                return rsaKeyFactory.generatePrivate(privateKeySpec);
            } catch (InvalidKeySpecException e1) {
                throw new KeyLoadException(e);
            }

        }
    } catch (NoSuchAlgorithmException e) {
        throw new KeyLoadException(e);
    }
}

From source file:license.regist.ProjectInfo.java

private static PrivateKey readPrivateKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(
            new BufferedInputStream(ProjectInfo.class.getClassLoader().getResourceAsStream("private.key")));
    try {//from   w ww.ja va 2  s.  c om
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePrivate(keySpec);
    } finally {
        oin.close();
    }
}

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

/**
 * Gets the private key from bytes./*w  ww .j a v a  2  s  .c  om*/
 *
 * @param keyBytes the key bytes
 * @return the private
 * @throws InvalidKeyException invalid key exception
 */
public static PrivateKey getPrivate(byte[] keyBytes) throws InvalidKeyException {
    try {
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory kf = KeyFactory.getInstance(RSA);
        return kf.generatePrivate(spec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        throw new InvalidKeyException(ex);
    }
}

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

/**
 * ?// w  ww .jav  a2s  .  c  om
 *
 * @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:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?// ww w. ja va 2 s. co  m
 *
 * @param data
 *            ?
 * @param key
 *            ?
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] encryptByPrivateKey(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.ENCRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

From source file:com.cablevision.util.sso.UtilSSO.java

/**
 * Creates a PrivateKey from the specified public key file and algorithm.
 * Returns null if failure to generate PrivateKey.
 * //from  w  w  w.j av  a2 s .  c  o m
 * @param PrivateKeyFilepath location of public key file
 * @param algorithm algorithm of specified key file
 * @return PrivateKey object representing contents of specified private key
 *         file, null if error in generating key or invalid file specified
 */
public static PrivateKey getPrivateKey(String privateKeyFilepath, String algorithm) throws SamlException {
    try {
        InputStream privKey = null;

        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        privKey = cl.getResourceAsStream(privateKeyFilepath);

        byte[] bytes = IOUtils.toByteArray(privKey);

        privKey.close();

        System.out.println("Private bytes: " + Arrays.toString(bytes));

        PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory factory = KeyFactory.getInstance(algorithm);
        return factory.generatePrivate(privSpec);
    } catch (FileNotFoundException e) {
        throw new SamlException("ERROR: Private key file not found - " + privateKeyFilepath);
    } catch (IOException e) {
        throw new SamlException("ERROR: Invalid private key file - " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new SamlException("ERROR: Invalid private key algorithm - " + e.getMessage());
    } catch (InvalidKeySpecException e) {
        throw new SamlException("ERROR: Invalid private key spec - " + e.getMessage());
    }
}

From source file:com.sixsq.slipstream.cookie.CryptoUtils.java

static private void setKeyPairFromDb()
        throws NoSuchAlgorithmException, InvalidKeySpecException, CertificateException {
    CookieKeyPair ckp = CookieKeyPair.load();
    if (ckp == null) {
        return;//from  w ww  .  j av  a2  s  . c  om
    }
    String privateKeyBase64 = ckp.getPrivateKey();
    String publicKeyBase64 = ckp.getPublicKey();
    if (privateKeyBase64 == null || publicKeyBase64 == null) {
        return;
    }

    byte[] privateKeyBytes = new Base64().decode(privateKeyBase64);
    KeyFactory keyFactory = KeyFactory.getInstance(keyPairAlgorithm);
    KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
    privateKey = keyFactory.generatePrivate(privateKeySpec);

    byte[] publicKeyBytes = new Base64().decode(publicKeyBase64);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
    keyFactory = KeyFactory.getInstance(keyPairAlgorithm);
    publicKey = keyFactory.generatePublic(x509KeySpec);
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???/*from w  w w  . j  a  v a  2s.  c  o  m*/
 * <dl>
 * <dt>?
 * <dd>RSA?????????????
 * </dl>
 * @param modulus 
 * @param exponent ??
 * @return RSA?
 */
public static RSAPrivateKey createPrivateKey(final BigInteger modulus, final BigInteger exponent) {
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateKey) keyFactory.generatePrivate(new RSAPrivateKeySpec(modulus, exponent));
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:cloudeventbus.pki.CertificateUtils.java

public static PrivateKey loadPrivateKey(String fileName)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    final Path path = Paths.get(fileName);
    final byte[] encodedPrivateKey = Files.readAllBytes(path);
    final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    return keyFactory.generatePrivate(privateKeySpec);
}