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:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java

/**
 * I know it returns a "private key" object but in reality it's a public key
 * because it's a key we give out to other people.
 *///w  ww . java  2s . c o m
public static PrivateKey getPublicServiceKey(String publicServiceKeyString)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(publicServiceKeyString));
    KeyFactory privKeyFactory = KeyFactory.getInstance("RSA");
    PrivateKey publicServiceKey = privKeyFactory.generatePrivate(privKeySpec);
    return publicServiceKey;
}

From source file:org.syphr.utils.x509.X509Utils.java

/**
 * Create a signature using the given token and private key.
 *
 * @param message/*ww w.j ava2  s  .  c  o m*/
 *            the message to sign
 * @param key
 *            the private key to use to create the signature (this must be
 *            PKCS8 encoded)
 * @param keyAlg
 *            the algorithm used to create the private key
 * @param sigAlg
 *            the algorithm to use to create the signature
 * @return the signature
 * @throws IOException
 *             if there is an error reading the key
 * @throws InvalidKeySpecException
 *             if the key algorithm is not appropriate for the given private
 *             key
 * @throws InvalidKeyException
 *             if the given private key is not valid
 * @throws SignatureException
 *             if there is an error while generating the signature
 */
public static byte[] sign(String message, InputStream key, KeyAlgorithm keyAlg, SignatureAlgorithm sigAlg)
        throws IOException, InvalidKeySpecException, InvalidKeyException, SignatureException {
    KeySpec privateKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(key));

    try {
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlg.getAlgorithm());
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        Signature sig = Signature.getInstance(sigAlg.getAlgorithm());
        sig.initSign(privateKey);
        sig.update(message.getBytes());
        return sig.sign();
    } catch (NoSuchAlgorithmException e) {
        /*
         * This is protected against by enforcing specific algorithm
         * choices.
         */
        throw new IllegalArgumentException("Unknown algorithm", e);
    }
}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

public static PrivateKey getPrivKey(byte[] bytes)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    KeyFactory kf = KeyFactory.getInstance("ECDSA", "SC");
    return kf.generatePrivate(new PKCS8EncodedKeySpec(bytes));
}

From source file:org.casbah.provider.SSLeayEncoder.java

public static RSAPrivateCrtKey decodeKey(String pemData, String keypass) throws CAProviderException {

    BufferedReader reader = null;
    try {//ww w  .j av  a2 s  .  c  om
        String strippedData = PemEncoder.stripArmor(pemData);

        String[] portions = strippedData.split("(?m)^\\n");
        if ((portions == null) || (portions.length != 3)) {
            throw new CAProviderException("Could not extract metainfo from file", null);
        }
        Properties props = new Properties();
        props.load(new StringReader(portions[1]));
        String procType = props.getProperty(PROC_TYPE);
        if ((procType == null) || (!procType.equals(SUPPORTED_PROC_TYPE))) {
            throw new CAProviderException("Missing or invalid Proc-Type declaration", null);
        }
        String dekInfo = props.getProperty(DEK_INFO);
        if (dekInfo == null) {
            throw new CAProviderException("Missing DEK-Info declaration", null);
        }
        String[] infoPortions = dekInfo.split(",");
        if ((infoPortions == null) || (infoPortions.length != 2)
                || (!SSLEAY_ENC_ALGORITHM.equals(infoPortions[0]))) {
            throw new CAProviderException("Invalid DEK-Info declaration", null);
        }

        byte[] decData = decryptKey(portions[2], Hex.decodeHex(infoPortions[1].toCharArray()), keypass);

        PKCS1EncodedKeySpec encodedKeySpec = new PKCS1EncodedKeySpec(decData);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        return (RSAPrivateCrtKey) keyFactory.generatePrivate(encodedKeySpec.toRsaKeySpec());

    } catch (CAProviderException cpe) {
        throw cpe;
    } catch (Exception e) {
        throw new CAProviderException("Could not decode SSLeay key", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.jinhe.tss.framework.license.LicenseFactory.java

/**
 * ?license???/*from  w w  w  . j  a  va  2  s.  c  o m*/
 * @param license
 * @throws Exception
 */
public static synchronized void sign(License license) throws Exception {
    String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE));
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim()));
    PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);

    Signature sig = Signature.getInstance(SIGN_ALGORITHM);
    sig.initSign(privKey);
    sig.update(license.getFingerprint());

    license.licenseSignature = EasyUtils.encodeHex(sig.sign());
}

From source file:com.hhi.bigdata.platform.push.client.RegisterUtil.java

/**
 * <pre>/*  w  w  w  .  j  a  v  a 2s  .  c o m*/
 * create a SSLSocketFactory instance with given parameters
 * </pre>
 * @param keystore
 * @param password
 * @return
 * @throws IOException
 */
private static PrivateKey getPrivateKey(URI keyFile) throws Exception {
    InputStream privKeyIs = new FileInputStream(new File(keyFile));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(privKeyIs));
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    return keyFactory.generatePrivate(keySpec);
}

From source file:com.cloud.utils.security.CertificateHelper.java

public static Key buildPrivateKey(String base64EncodedKeyContent)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(Base64.decodeBase64(base64EncodedKeyContent));
    return kf.generatePrivate(keysp);
}

From source file:org.excalibur.core.util.SecurityUtils2.java

public static PrivateKey readPrivateKey(@Nonnull File key) throws IOException, GeneralSecurityException {
    try (PemReader reader = new PemReader(new FileReader(key))) {
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        KeyFactory kf = SecurityUtils.getKeyFactory("RSA");
        return kf.generatePrivate(keySpec);
    }/* www.  j a  v  a2  s .  c  o m*/
}

From source file:com.boubei.tss.modules.license.LicenseFactory.java

/**
 * ?license???/*from ww  w  . ja  v a 2  s .c o m*/
 * @param license
 * @throws Exception
 */
public static synchronized void sign(License license) throws Exception {
    String privateKey = FileHelper.readFile(new File(PRIVATE_KEY_FILE));
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(EasyUtils.decodeHex(privateKey.trim()));
    PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);

    Signature sig = Signature.getInstance(SIGN_ALGORITHM);
    sig.initSign(privKey);
    sig.update(license.getFingerprint());

    license.signature = EasyUtils.encodeHex(sig.sign());
}

From source file:org.opentravel.schemacompiler.security.PasswordHelper.java

/**
 * Returns an decryption cipher that is based on the private encryption key file located on the
 * application's classpath./*from w  w  w .  ja v a 2s .  c  o m*/
 * 
 * @return Cipher
 * @throws GeneralSecurityException
 *             thrown if encryption key is not valid
 * @throws IOException
 *             thrown if the contents of the private key file cannot be loaded
 */
private static Cipher loadDecryptionCipher() throws GeneralSecurityException, IOException {
    BigInteger[] keyComponents = loadKeyFile(PRIVATE_KEYFILE);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(keyComponents[0], keyComponents[1]);
    KeyFactory factory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    PrivateKey privateKey = factory.generatePrivate(keySpec);
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);

    cipher.init(Cipher.PRIVATE_KEY, privateKey);
    return cipher;
}