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:Main.java

public static PublicKey getPublicKey(String n, String publicExponent) {
    KeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(n, 16), new BigInteger(publicExponent, 16));

    KeyFactory factory = null;/*ww  w.  j  a va 2  s  .c om*/
    PublicKey publicKey = null;
    try {
        factory = KeyFactory.getInstance(KEY_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    try {
        publicKey = factory.generatePublic(publicKeySpec);
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return publicKey;
}

From source file:Main.java

public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) {
    if (msg != null && pubKeySpec != null && !msg.isEmpty()) {
        try {/*  www.  jav  a  2  s. c  om*/
            Log.w(TAG, "msg is: " + msg + " with length " + msg.length());
            KeyFactory fact = KeyFactory.getInstance("RSA");

            PublicKey pubKey = fact.generatePublic(pubKeySpec);

            // TODO encrypt the message and send it
            // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            // Cipher cipher =
            // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding",
            // "BC");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            Log.d(TAG, "cipher block size is " + cipher.getBlockSize());
            byte[] msgByteArray = msg.getBytes();
            byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)];
            cipherData = cipher.doFinal(msgByteArray);
            Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length));
            Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData)));
            return cipherData;

        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "RSA algorithm not available", e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "", e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "", e);
        } catch (InvalidKeyException e) {
            Log.e(TAG, "", e);
        } catch (BadPaddingException e) {
            Log.e(TAG, "", e);
        } catch (IllegalBlockSizeException e) {
            Log.e(TAG, "", e);
        } catch (Exception e) {
            Log.e(TAG, "", e);
        } /*
           * catch (NoSuchProviderException e) { Log.e(TAG, "", e); }
           */
    }
    return null;
}

From source file:Main.java

static PublicKey loadDERPublicKey(byte[] der) throws Exception {
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(der);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return factory.generatePublic(publicKeySpec);
}

From source file:Main.java

static Key base64ToRsaPrivateKey(String b64) {
    if (b64 != null) {
        try {//from  w ww . j  av a 2 s. co  m
            byte[] keyBytes = decodeB64(b64);
            if (keyBytes != null) {
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
                return keyFactory.generatePrivate(privateKeySpec);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static PrivateKey getPrivateKey(String modulus, String privateExponent)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    BigInteger bigIntModulus = new BigInteger(modulus);
    BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
    KeyFactory keyFactory = KeyFactory.getInstance(RSA);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}

From source file:com.completetrsst.crypto.Common.java

/**
 * Converts a X509-encoded EC key to a PublicKey.
 *///w  w  w.  ja va  2 s. c  o m
public static PublicKey toPublicKeyFromX509(String stored) throws GeneralSecurityException {
    KeyFactory factory = KeyFactory.getInstance("EC");
    byte[] data = Base64.decodeBase64(stored);
    X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
    return factory.generatePublic(spec);
}

From source file:Main.java

public static PublicKey decodePublic(byte[] encoded, String keyType)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encoded);
    KeyFactory kf = KeyFactory.getInstance(keyType);
    return kf.generatePublic(pubKeySpec);
}

From source file:Main.java

public static byte[] getRSAEncryptedData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);//from   w w w .  j a va2  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:MainClass.java

public static void createKey() throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");

    kpg.initialize(512);// w w  w .j ava 2s . co m
    KeyPair kp = kpg.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

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

From source file:Main.java

public static PublicKey getPublicKey(String key) {
    try {// w w w . ja  v  a  2s .com
        Base64 base64_decoder = new Base64();
        byte[] byteKey = base64_decoder.decode(key.getBytes()); // ,
        // Base64.DEFAULT);
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");

        return kf.generatePublic(X509publicKey);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}