Example usage for java.security KeyFactory getKeySpec

List of usage examples for java.security KeyFactory getKeySpec

Introduction

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

Prototype

public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException 

Source Link

Document

Returns a specification (key material) of the given key object.

Usage

From source file:Main.java

private static String getSignatureAlgorithm(Key key) throws Exception {
    if ("EC".equals(key.getAlgorithm())) {
        int curveSize;
        KeyFactory factory = KeyFactory.getInstance("EC");

        if (key instanceof PublicKey) {
            ECPublicKeySpec spec = factory.getKeySpec(key, ECPublicKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else if (key instanceof PrivateKey) {
            ECPrivateKeySpec spec = factory.getKeySpec(key, ECPrivateKeySpec.class);
            curveSize = spec.getParams().getCurve().getField().getFieldSize();
        } else {//w w  w.j  a v a  2  s.com
            throw new InvalidKeySpecException();
        }

        if (curveSize <= 256) {
            return "SHA256withECDSA";
        } else if (curveSize <= 384) {
            return "SHA384withECDSA";
        } else {
            return "SHA512withECDSA";
        }
    } else if ("RSA".equals(key.getAlgorithm())) {
        return "SHA256withRSA";
    } else {
        throw new IllegalArgumentException("Unsupported key type " + key.getAlgorithm());
    }
}

From source file:com.vmware.identity.rest.idm.data.PrivateKeyDTO.java

private static String encodePrivateKey(PrivateKey key)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    if (key == null) {
        return null;
    }/*from  ww w .ja v  a  2 s  .c om*/

    KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
    PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
    byte[] packed = spec.getEncoded();
    String encodePrivateKey = Base64.encodeBase64String(packed);
    Arrays.fill(packed, (byte) 0);
    return encodePrivateKey;
}

From source file:com.sixsq.slipstream.cookie.CryptoUtils.java

static private String savePrivateKey(PrivateKey privateKey) throws GeneralSecurityException {
    KeyFactory fact = KeyFactory.getInstance(keyPairAlgorithm);
    PKCS8EncodedKeySpec spec = fact.getKeySpec(privateKey, PKCS8EncodedKeySpec.class);
    byte[] encoded = spec.getEncoded();
    String privateKeyStr = new Base64().encodeToString(encoded);
    return privateKeyStr;
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static String savePublicKey(PublicKey key) {
    try {//from  w  w w  .  ja  v a 2  s  . c  o m
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static String savePrivateKey(PrivateKey key) {
    try {/*  w w w  .  j a v  a2 s  .  c om*/
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}

From source file:org.umit.icm.mobile.utils.RSACrypto.java

public static RSAKey getPublicKeyIntegers(PublicKey publicKey)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
    RSAKey rsaKey = RSAKey.newBuilder().setExp(publicKeySpec.getPublicExponent().toString())
            .setMod(publicKeySpec.getModulus().toString()).build();

    return rsaKey;
}

From source file:com.orange.oidc.tim.service.KryptoUtils.java

public static String getJwkPublic(KeyPair kp) {
    try {//from   w  w  w.  j ava2s  .co m
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid for tim_app_key
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

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

        RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);

        jk.put("n", encodeB64(kspec.getModulus().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        // Log.d("getJwkPublic key: ",pubkey.toString());
        // Log.d("getJwkPublic jwk: ",jo.toString());

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.orange.oidc.tim.service.KryptoUtils.java

public static String getJwkPrivate(KeyPair kp) {
    try {/*w  w w  . ja  v a2 s .co  m*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid for tim_app_key
        SecureRandom random = new SecureRandom();
        String kid = new BigInteger(130, random).toString(32);
        jk.put("kid", kid);
        jk.put("e", "AQAB");

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

        RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(),
                RSAPrivateKeySpec.class);
        RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
                RSAPublicKeySpec.class);

        // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString());
        // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString());

        jk.put("n", encodeB64(pubkspec.getModulus().toByteArray()));
        jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray()));
        JSONArray ja = new JSONArray();
        ja.put(jk);
        JSONObject jo = new JSONObject();
        jo.put("keys", ja);

        return jo.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.umit.icm.mobile.test.utils.RSACryptoTests.java

public void testPublicReadWrite() throws Throwable {
    keyPair = RSACrypto.generateKey();// ww w .j  av a  2 s. c o m
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSACrypto.saveKey("rsaKey.pub", publicKeySpec.getModulus(), publicKeySpec.getPublicExponent());
    Assert.assertEquals(RSACrypto.readPublicKey("rsaKey.pub"), keyPair.getPublic());
}

From source file:org.umit.icm.mobile.test.utils.RSACryptoTests.java

public void testPrivateReadWrite() throws Throwable {
    keyPair = RSACrypto.generateKey();/*  w w  w. j  a v  a 2  s  . c o m*/
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);
    RSACrypto.saveKey("rsaKey.priv", privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent());
    Assert.assertEquals(RSACrypto.readPrivateKey("rsaKey.priv"), keyPair.getPrivate());
}