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

public static void main(String[] args) throws Exception {
    int eValue = 79;
    int bitLength = 1024; // KeySize

    BigInteger e = e = new BigInteger(Integer.toString(eValue));

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(bitLength);//from  w ww  .  j av a  2s . c om
    KeyPair kp = kpg.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);

    kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");

    e = new BigInteger(Integer.toString(eValue));
    System.out.println("e =" + e);
    RSAKeyGenParameterSpec param = new RSAKeyGenParameterSpec(bitLength, e);
    kpg.initialize(param);
    kp = kpg.generateKeyPair();

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

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

From source file:MainClass.java

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(512);//from   w w w.j  ava 2 s .  c om
    KeyPair keys = kpg.genKeyPair();
    PrivateKey priKey = keys.getPrivate();
    PublicKey pubKey = keys.getPublic();
    KeyFactory kf = KeyFactory.getInstance("DSA");
    DSAPrivateKeySpec dsaPriKeySpec = (DSAPrivateKeySpec) kf.getKeySpec(priKey, DSAPrivateKeySpec.class);
    DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec) kf.getKeySpec(pubKey, DSAPublicKeySpec.class);
    System.out.println("\nDSA Private Key");
    System.out.println("\nx = " + dsaPriKeySpec.getX());
    System.out.println("\nDSA Public Key");
    System.out.println("\ng = " + dsaPubKeySpec.getG());
    System.out.println("\np = " + dsaPubKeySpec.getP());
    System.out.println("\nq = " + dsaPubKeySpec.getQ());
    System.out.println("\ny = " + dsaPubKeySpec.getY());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);//from www  . j  av a  2s  .  com
    keyGen.initialize(1024, random);
    KeyPair keypair = keyGen.generateKeyPair();

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

    DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(),
            DSAPublicKeySpec.class);

    System.out.println(keypair.getPublic());
    FileOutputStream fos = new FileOutputStream("publicKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(kspec.getY());
    oos.writeObject(kspec.getP());
    oos.writeObject(kspec.getQ());
    oos.writeObject(kspec.getG());

    FileInputStream fin = new FileInputStream("publicKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);

    BigInteger Y = (BigInteger) ois.readObject();
    BigInteger P = (BigInteger) ois.readObject();
    BigInteger Q = (BigInteger) ois.readObject();
    BigInteger G = (BigInteger) ois.readObject();

    DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G);
    PublicKey pkey = kfactory.generatePublic(keyspec);
    System.out.println(pkey);
}

From source file:com.cliqset.magicsig.util.KeyGen.java

public static void main(String[] args) {
    try {/*from w w w .j  av a 2  s.c o  m*/
        FileOutputStream fos = new FileOutputStream(fileName);
        for (int x = 0; x < numKeys; x++) {
            fos.write((x + " RSA.").getBytes("ASCII"));
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(keySize);
            KeyPair keyPair = keyPairGenerator.genKeyPair();

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec publicSpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
            RSAPrivateKeySpec privateSpec = keyFactory.getKeySpec(keyPair.getPrivate(),
                    RSAPrivateKeySpec.class);

            fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getModulus())));

            fos.write(".".getBytes("ASCII"));

            fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getPublicExponent())));

            fos.write(".".getBytes("ASCII"));

            fos.write(Base64.encodeBase64URLSafe(getBytes(privateSpec.getPrivateExponent())));

            fos.write("\n".getBytes("ASCII"));

            System.out.println(x);
        }
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

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

    kpg.initialize(512);/*from   w  w  w  . j a  v a2 s  .  com*/
    KeyPair kp = kpg.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

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

From source file:MainClass.java

public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");

    DHParameterSpec param = new DHParameterSpec(p, g);
    kpg.initialize(param);/*from  ww  w  . j  ava 2  s  .c  om*/
    KeyPair kp = kpg.generateKeyPair();

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

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

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

/**
 * Converts an EC PublicKey to an X509-encoded string.
 */// w  w w. ja  v a2  s. c  om
public static String toX509FromPublicKey(PublicKey publicKey) throws GeneralSecurityException {
    KeyFactory factory = KeyFactory.getInstance("EC");
    X509EncodedKeySpec spec = factory.getKeySpec(publicKey, X509EncodedKeySpec.class);
    return new Base64(0, null, true).encodeToString(spec.getEncoded());
}

From source file:org.opentravel.schemacompiler.security.GenerateEncryptionKeys.java

/**
 * Generates the public and private key files used for encrypting and decrypting passwords.
 *//*from  w  ww . j ava  2s .c o  m*/
public static void generateKeyFiles() throws Exception {
    File publicKeyFile = new File(System.getProperty("user.dir"),
            "/src/main/resources" + PasswordHelper.PUBLIC_KEYFILE);
    File privateKeyFile = new File(System.getProperty("user.dir"),
            "/src/main/resources" + PasswordHelper.PRIVATE_KEYFILE);
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM);

    keyGenerator.initialize(ENCRYPTION_KEY_SIZE);

    KeyPair keyPair = keyGenerator.generateKeyPair();
    KeyFactory fact = KeyFactory.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM);
    RSAPublicKeySpec publicKeySpec = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSAPrivateKeySpec privateKeySpec = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);

    System.out
            .println("Public Key : " + publicKeySpec.getModulus() + " / " + publicKeySpec.getPublicExponent());
    System.out.println(
            "Private Key: " + privateKeySpec.getModulus() + " / " + privateKeySpec.getPrivateExponent());

    writeKeyFile(publicKeyFile, publicKeySpec.getModulus(), publicKeySpec.getPublicExponent());
    writeKeyFile(privateKeyFile, privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent());
}

From source file:Main.java

public static String getJwkPublic(KeyPair kp) {
    try {/* w  ww . j ava  2s .co m*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid 
        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:Main.java

public static String getJwkPrivate(KeyPair kp) {
    try {/*from   w w w.  j av a2  s  .  c  om*/
        JSONObject jk = new JSONObject();
        jk.put("kty", "RSA");
        // generate random kid 
        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;
}