Example usage for java.security KeyFactory generatePublic

List of usage examples for java.security KeyFactory generatePublic

Introduction

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

Prototype

public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException 

Source Link

Document

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

Usage

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

/**
 * Read public key./* w  ww. jav  a  2 s  .  c  o  m*/
 *
 * @param publicKeyBytes
 *            the public key bytes
 * @param provider
 *            the provider
 * @param algorithm
 *            the algorithm for the {@link KeyFactory}
 * @return the public 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 PublicKey readPublicKey(final byte[] publicKeyBytes, final String provider,
        final String algorithm)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
    final KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    final PublicKey publicKey = keyFactory.generatePublic(keySpec);
    return publicKey;
}

From source file:Main.java

public static byte[] getRSAEncryptedData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);//w ww .j  a va  2 s  . co m
    BigInteger pubExp = new BigInteger("010001", 16);

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
        RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherData = cipher.doFinal(dataWithHash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeyException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (BadPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeySpecException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (NoSuchPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:net.arccotangent.pacchat.filesystem.KeyManager.java

public static KeyPair loadRSAKeys() {
    try {/*  www.j ava  2  s .c  o  m*/
        km_log.i("Loading RSA key pair from disk.");
        byte[] privEncoded = Files.readAllBytes(privkeyFile.toPath());
        byte[] pubEncoded = Files.readAllBytes(pubkeyFile.toPath());

        X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64.decodeBase64(pubEncoded));
        PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privEncoded));

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey pubkey = keyFactory.generatePublic(pubSpec);
        PrivateKey privkey = keyFactory.generatePrivate(privSpec);

        return new KeyPair(pubkey, privkey);
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
        km_log.e("Error while loading keypair!");
        e.printStackTrace();
    }
    return null;
}

From source file:com.launchkey.sdk.crypto.JCECrypto.java

/**
 * Get an RSA public key utilizing the provided provider and PEM formatted string
 * @param provider Provider to generate the key
 * @param pem PEM formatted key string/*from   w ww .j  a v a2  s . com*/
 * @return
 */
public static RSAPublicKey getRSAPublicKeyFromPEM(Provider provider, String pem) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", provider);
        return (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(getKeyBytesFromPEM(pem)));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Algorithm SHA256withRSA is not available", e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException("Invalid PEM provided", e);
    }
}

From source file:com.vmware.o11n.plugin.crypto.model.CryptoUtil.java

/**
 * Generate a RSA Public Key from a KeySpec
 *
 * @param keySpec/*w  w w  . j a va  2s.co  m*/
 * @return RSA Public Key
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
private static PublicKey getPublicKey(KeySpec keySpec)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory fac = KeyFactory.getInstance(KEYFACTORY_ALGORITHM);
    return fac.generatePublic(keySpec);
}

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

public static PublicKey getPubKey(byte[] bytes)
        throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
    KeyFactory kf = KeyFactory.getInstance("ECDSA", "BC");
    return kf.generatePublic(new X509EncodedKeySpec(bytes));
}

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

/**
 * Added code for IOS (RSA keys) compatibility.
 * /*from  w w  w.j av  a 2 s  . c  o  m*/
 * @param encodedPubKey
 * @return
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
public static PublicKey getPublicKey(byte[] encodedPubKey)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedPubKey);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(spec);
}

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

public static PublicKey getRSAPublicKey(byte[] encodedPubKey)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    RSAPublicKey pubKey8 = (RSAPublicKey) RSAPublicKey.getInstance(encodedPubKey);
    SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(
            new RSAKeyParameters(false, pubKey8.getModulus(), pubKey8.getPublicExponent()));
    X509EncodedKeySpec spec = new X509EncodedKeySpec(info.getEncoded());
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    return keyFactory.generatePublic(spec);
}

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

/**
 * Creates a public key from the X509-encoded value of the given bytes.
 *
 * @param publicKey The X509-encoded public key bytes.
 * @return The public key./*ww w. j  a va  2  s .c  om*/
 */
public static PublicKey createPublicKey(byte[] publicKey) {
    if (publicKey == null) {
        return null;
    }

    try {
        KeyFactory fac = KeyFactory.getInstance("RSA");
        EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
        return fac.generatePublic(spec);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    }
}

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

/**
 * /*from w  ww . j  a  v  a 2  s  .co  m*/
 *
 * @param publicKey
 * @return
 * @throws Exception
 */
public static PublicKey getPublicKey(String publicKey) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(publicKey);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    return keyFactory.generatePublic(spec);
}