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.sinekartads.utils.X509Utils.java

public static PrivateKey privateKeyFromHex(String privateKeyHex/*, EncryptionAlgorithm encryptionAlgorithm*/ ) {

    byte[] privateKeyEnc = HexUtils.decodeHex(privateKeyHex);

    PrivateKey privateKey;//from  w  w w. j av a  2  s .com
    try {
        KeyFactory kf = KeyFactory.getInstance("RSA", "SunJSSE");
        PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(privateKeyEnc);
        privateKey = kf.generatePrivate(ks);
    } catch (NoSuchAlgorithmException e) {
        // never thrown, algorithm granted by CipherAlgorithm
        throw new RuntimeException(e);
    } catch (NoSuchProviderException e) {
        // never thrown, using a system security provider
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        // never thrown, the algorithm must be the same for the certificate's public key
        throw new RuntimeException(e);
    }

    return privateKey;
}

From source file:com.shenit.commons.codec.RsaUtils.java

/**
 * RSA??/*from   w  w  w  .  ja v  a2  s  .c o  m*/
 * 
 * @param content
 *            ???
 * @param privateKey
 *            ?
 * @param input_charset
 *            ??
 * @return ??
 */
public static String sign(String content, String privateKey, String algorithm, String input_charset) {
    try {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
        KeyFactory keyf = KeyFactory.getInstance(CODEC_RSA);
        PrivateKey priKey = keyf.generatePrivate(priPKCS8);

        Signature signature = Signature.getInstance(algorithm);
        signature.initSign(priKey);
        signature.update(content.getBytes(input_charset));
        byte[] signed = signature.sign();
        return Base64Utils.base64EncodeHex(signed);
    } catch (Exception e) {
        if (LOG.isWarnEnabled())
            LOG.warn("[sign] could not sign with exception", e);
    }

    return null;
}

From source file:org.psl.fidouaf.core.crypto.KeyCodec.java

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

From source file:com.shenit.commons.codec.RsaUtils.java

/**
 * ?//  w ww.  j a  v  a2s .com
 * 
 * @param key
 *            ?base64?
 * @throws Exception
 */
public static PrivateKey getPrivateKey(String key) throws Exception {

    byte[] keyBytes;

    keyBytes = Base64.decodeBase64(key);

    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

    KeyFactory keyFactory = KeyFactory.getInstance(CODEC_RSA);

    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

    return privateKey;
}

From source file:libcore.tzdata.update_test_app.installupdatetestapp.MainActivity.java

private static PrivateKey createKey() throws Exception {
    byte[] derKey = Base64.decode(TEST_KEY.getBytes(), Base64.DEFAULT);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derKey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePrivate(keySpec);
}

From source file:org.springframework.security.oauth.common.signature.RSAKeySecret.java

/**
 * Creates a private key from the PKCS#8-encoded value of the given bytes.
 *
 * @param privateKey The PKCS#8-encoded private key bytes.
 * @return The private key./*from  ww w .ja  v a 2  s.c  om*/
 */
public static PrivateKey createPrivateKey(byte[] privateKey) {
    if (privateKey == null) {
        return null;
    }

    try {
        KeyFactory fac = KeyFactory.getInstance("RSA");
        EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
        return fac.generatePrivate(spec);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}

From source file:pxb.android.tinysign.TinySign.java

private static Signature instanceSignature() throws Exception {
    byte[] data = dBase64(Constants.privateKey);
    KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(data));
    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign(privateKey);/*from  ww w  .  j a  va2 s  .c  o m*/
    return signature;
}

From source file:de.alpharogroup.crypto.key.reader.PrivateKeyReader.java

/**
 * Read private key.//from   w  ww  .jav  a 2  s  . co  m
 *
 * @param privateKeyBytes
 *            the private key bytes
 * @param provider
 *            the provider
 * @param algorithm
 *            the algorithm for the {@link KeyFactory}
 * @return the private key
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchProviderException
 *             is thrown if the specified provider is not registered in the security provider
 *             list.
 */
public static PrivateKey readPrivateKey(final byte[] privateKeyBytes, final String provider,
        final String algorithm)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
    final KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    final PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * ?/*  w w w .j  a  v  a  2s . co  m*/
 *
 * @param privateKey
 * @return
 * @throws Exception
 */
public static PrivateKey getPrivateKey(String privateKey) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(privateKey);
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    return keyFactory.generatePrivate(spec);
}

From source file:com.turo.pushy.apns.auth.ApnsSigningKey.java

/**
 * Loads a signing key from the given input stream.
 *
 * @param inputStream the input stream from which to load the key
 * @param teamId the ten-character, Apple-issued identifier for the team to which the key belongs
 * @param keyId the ten-character, Apple-issued identitifier for the key itself
 *
 * @return an APNs signing key with the given key ID and associated with the given team
 *
 * @throws IOException if a key could not be loaded from the given file for any reason
 * @throws NoSuchAlgorithmException if the JVM does not support elliptic curve keys
 * @throws InvalidKeyException if the loaded key is invalid for any reason
 *//*from  w w w . j av  a  2 s  .  c om*/
public static ApnsSigningKey loadFromInputStream(final InputStream inputStream, final String teamId,
        final String keyId) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
    final ECPrivateKey signingKey;
    {
        final String base64EncodedPrivateKey;
        {
            final StringBuilder privateKeyBuilder = new StringBuilder();

            final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            boolean haveReadHeader = false;
            boolean haveReadFooter = false;

            for (String line; (line = reader.readLine()) != null;) {
                if (!haveReadHeader) {
                    if (line.contains("BEGIN PRIVATE KEY")) {
                        haveReadHeader = true;
                    }
                } else {
                    if (line.contains("END PRIVATE KEY")) {
                        haveReadFooter = true;
                        break;
                    } else {
                        privateKeyBuilder.append(line);
                    }
                }
            }

            if (!(haveReadHeader && haveReadFooter)) {
                throw new IOException("Could not find private key header/footer");
            }

            base64EncodedPrivateKey = privateKeyBuilder.toString();
        }

        final byte[] keyBytes = Base64.decodeBase64(base64EncodedPrivateKey);

        final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        final KeyFactory keyFactory = KeyFactory.getInstance("EC");

        try {
            signingKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    }

    return new ApnsSigningKey(keyId, teamId, signingKey);
}