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.cert.tool.PrivateKeyTool.java

public static PrivateKey coverString2PrivateKey(String base64PrivateKey) throws CertException {

    try {/*from  www. j av a  2s  .com*/
        byte[] priEncoded = getKeyEncoded(base64PrivateKey);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALG);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(priEncoded);
        return keyFactory.generatePrivate(privateKeySpec);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (InvalidKeySpecException e) {
        throw new CertException(e);
    }
}

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

/**
 * ?license???/*from   ww  w  .  ja 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.signature = EasyUtils.encodeHex(sig.sign());
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

private static PrivateKey getRSAPrivateKeyFrom(String content)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    PemReader reader = new PemReader(new StringReader(content));
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
    return KeyFactory.getInstance("RSA").generatePrivate(spec);
}

From source file:com.xinferin.licensing.LicenceActivator.java

private void initialiseKeys() throws Exception {
    try {//www.j av  a 2 s .  c  o m

        byte[] publicKeyBytes = Base64.decodeBase64(spublicKey);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        publicKey = keyFactory.generatePublic(publicKeySpec);

    } catch (InvalidKeySpecException e) {
        throw new Exception("Invalid Key Specs not valid Key files." + e.getCause());
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There is no such algorithm. Please check the JDK ver." + e.getCause());
    }
}

From source file:com.floreantpos.license.FiveStarPOSLicenseGenerator.java

private static PrivateKey readPrivateKey(InputStream privateKey)
        throws FileNotFoundException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(privateKey));
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    PrivateKey key = keyFactory.generatePrivate(keySpec);
    return key;/*from w  w w  . j  ava 2s.  c om*/
}

From source file:org.hypoport.jwt.common.Toolbox.java

public static RSAPrivateKey readRSAPrivateKey(Reader keyReader) throws Exception {
    return (RSAPrivateKey) KeyFactory.getInstance("RSA")
            .generatePrivate(new PKCS8EncodedKeySpec(readPemFile(keyReader)));
}

From source file:com.thoughtworks.go.security.RegistrationJSONizer.java

public static Registration fromJson(String json) {
    Map map = GSON.fromJson(json, Map.class);

    if (map.isEmpty()) {
        return Registration.createNullPrivateKeyEntry();
    }//w ww.j  av a 2 s .  com

    List<X509Certificate> chain = new ArrayList<>();
    try {
        PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        PrivateKey privateKey = kf.generatePrivate(spec);
        String agentCertificate = (String) map.get("agentCertificate");
        PemReader certReader = new PemReader(new StringReader(agentCertificate));
        while (true) {
            PemObject obj = certReader.readPemObject();
            if (obj == null) {
                break;
            }
            chain.add((X509Certificate) CertificateFactory.getInstance("X.509")
                    .generateCertificate(new ByteArrayInputStream(obj.getContent())));
        }
        return new Registration(privateKey, chain.toArray(new X509Certificate[0]));
    } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
        throw bomb(e);
    }
}

From source file:org.intermine.web.security.Base64PublicKeyDecoder.java

/**
 * Construct a new decoder.//from w w w.j  av  a  2s. c  o m
 */
public Base64PublicKeyDecoder() {
    decoder = new Base64();
    try {
        fact = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("This JVM cannot create RSA keys.");
    }
}

From source file:aiai.apps.commons.utils.SecUtils.java

public static PublicKey getPublicKey(String keyBase64)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] keyBytes = Base64.decodeBase64(keyBase64);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePublic(spec);
}

From source file:be.solidx.hot.utils.SSLUtils.java

protected static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return (RSAPrivateKey) factory.generatePrivate(spec);
}