Example usage for java.security KeyFactory getInstance

List of usage examples for java.security KeyFactory getInstance

Introduction

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

Prototype

public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyFactory object that converts public/private keys of the specified algorithm.

Usage

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

/**
 * ?public key// www.  j a  v a2  s .  c o  m
 * 
 * @throws RuntimeException key
 */
public static PublicKey readPublicKey(byte[] keyBytes) throws RuntimeException {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        byte[] encodedKey = Base64.decodeBase64(keyBytes);
        EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.sharky.Security.java

public static PublicKey generatePublicKey(String encodedPublicKey) {
    try {// w w w .ja  v  a2 s  .c  o m
        byte[] decodedKey = Base64.decode(encodedPublicKey);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
        return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException(e);
    } catch (Base64DecoderException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

static KeyPair createKeyPair(byte[] encodedPrivateKey, byte[] encodedPublicKey) {
    try {// w w  w.j a v  a2s. co  m
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
        KeyFactory generator = KeyFactory.getInstance(ASYM_ALGORITHM);
        PrivateKey privateKey = generator.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
        PublicKey publicKey = generator.generatePublic(publicKeySpec);
        return new KeyPair(publicKey, privateKey);
    } catch (Exception e) {
        throw new IllegalArgumentException("Failed to create KeyPair from provided encoded keys", e);
    }
}

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

/**
 * RSA???//from w w  w  . j a v  a2  s  . 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: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  www  . java2s  . c om*/
 * 
 * @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;
}

From source file:enc_mods.aes.java

/**
 * Encrypts the AES key to a file using an RSA public key
 *///ww w.  j av a 2s.c om
public void saveKey(File out, File publicKeyFile) {
    try {
        // read public key to be used to encrypt the AES key
        byte[] encodedKey = new byte[(int) publicKeyFile.length()];
        new FileInputStream(publicKeyFile).read(encodedKey);

        // create public key
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey pk = kf.generatePublic(publicKeySpec);

        // write AES key
        cipher.init(Cipher.ENCRYPT_MODE, pk);
        CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), cipher);
        os.write(key);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * RSA???//  w w  w  .ja v a  2  s .  c  o m
 *
 * @param data ?
 * @param key  ?
 * @return
 * @throws Exception
 */
public static String sign(byte[] data, Key key) throws Exception {
    byte[] keyBytes = key.getEncoded();
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
    PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

    signature.initSign(privateK);
    signature.update(data);

    return Base64.encodeBase64String(signature.sign());
}

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  w w . java  2s  .  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:org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.java

public static PublicKey getRsaPublicKey(JsonWebKey key) {
    final Base64 decoder = new Base64(true);
    String e = (String) key.getKeyProperties().get("e");
    String n = (String) key.getKeyProperties().get("n");
    BigInteger modulus = new BigInteger(1, decoder.decode(n.getBytes(StandardCharsets.UTF_8)));
    BigInteger exponent = new BigInteger(1, decoder.decode(e.getBytes(StandardCharsets.UTF_8)));
    try {//from  ww w  .  j  a v a2s . co m
        return KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));
    } catch (InvalidKeySpecException | NoSuchAlgorithmException e1) {
        throw new IllegalStateException(e1);
    }
}

From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java

public PublicKey loadPublicKey(String path) {
    FileInputStream fis = null;//w ww.j  a  v  a2s . co  m
    try {
        File filePublicKey = new File(path);
        fis = new FileInputStream(path);
        byte[] pubKey = new byte[(int) filePublicKey.length()];
        fis.read(pubKey);
        KeyFactory keyFactory = KeyFactory.getInstance(getAlgorithm());
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKey);
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
        return publicKey;
    } catch (Exception e) {
        LOG.error("An error occurred while loading the public key from disk.", e);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    return null;
}