Example usage for java.security KeyPairGenerator getInstance

List of usage examples for java.security KeyPairGenerator getInstance

Introduction

In this page you can find the example usage for java.security KeyPairGenerator getInstance.

Prototype

public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyPairGenerator object that generates public/private key pairs for the specified algorithm.

Usage

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

public static void createKeys(String publicKeyUri, String privateKeyUri)
        throws NoSuchAlgorithmException, IOException {

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyGen.generateKeyPair();

    IOUtils.write(keyPair.getPublic().getEncoded(), new FileOutputStream(publicKeyUri));
    IOUtils.write(keyPair.getPrivate().getEncoded(), new FileOutputStream(privateKeyUri));

}

From source file:Main.java

public static KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYGEN_ALGORITHM/* , PROVIDER_NAME */);
    SecureRandom secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM/* , PROVIDER_NAME */);
    generator.initialize(keySize, secureRandom);
    return generator.generateKeyPair();
}

From source file:com.example.license.RSAUtil.java

public static KeyPair generatorKeyPair(String seed) throws Exception {
    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes());/*from   w w w.  j a v  a 2  s  .c  o m*/
    kpGenerator.initialize(2014, random);
    KeyPair keyPair = kpGenerator.generateKeyPair();
    return keyPair;
}

From source file:com.java.demo.RsaDemo.java

public static void Init() {
    try {/*w ww .ja va 2 s. co  m*/
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair kp = keyPairGenerator.generateKeyPair();
        rSAPublicKey = (RSAPublicKey) kp.getPublic();
        rSAPrivateKey = (RSAPrivateKey) kp.getPrivate();
    } catch (Exception ex) {
        System.out.println("com.Java.Demo.RsaDemo.Init()" + ex.getMessage());
    }
}

From source file:MainClass.java

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

    kpg.initialize(512);/*w  w  w  .  j a va2  s  . c  om*/
    KeyPair kp = kpg.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

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

From source file:Main.java

/**
 * Generates a public/private key pair that meets Thali's security requirements
 * @return//from   www. jav  a  2s  .  com
 */
public static KeyPair GenerateThaliAcceptablePublicPrivateKeyPair() {
    KeyPairGenerator keyPairGenerator = null;
    try {
        keyPairGenerator = KeyPairGenerator.getInstance(KeyTypeIdentifier);
        // TODO: http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html talks about security
        // failures in Android caused by improperly initialized RNGs. It would appear that this issue doesn't
        // apply to the latest version of Android. But obviously this is something that has to be further investigated
        // to make sure we are doing this correctly.
        keyPairGenerator.initialize(KeySizeInBits, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:MainClass.java

public KeyPair generateKeyPair(long seed) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(seed);//www  . j av a  2 s.  c o m
    keyGenerator.initialize(1024, rng);

    return (keyGenerator.generateKeyPair());
}

From source file:love.sola.netsupport.util.RSAUtil.java

public static void genKeyPair() {
    try {/* ww w . j  a va 2 s. com*/
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();
        publicKey_s = Base64.encodeBase64String(publicKey.getEncoded());
        privateKey_s = Base64.encodeBase64String(privateKey.getEncoded());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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);/*w w  w . j a  v  a 2 s . c o m*/
    KeyPair kp = kpg.generateKeyPair();

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

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

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static void geraChave() {
    try {//from  w  w w  . j  av  a 2 s  .c o  m

        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_ASYM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File chavePrivadaFileServer = new File(path, PATH_CHAVE_PRIVADA_SERVER);
        File chavePublicaFileServer = new File(path, PATH_CHAVE_PUBLICA_SERVER);

        // Cria os arquivos para armazenar a chave Privada e a chave Publica            
        if (chavePrivadaFileServer.getParentFile() != null) {
            chavePrivadaFileServer.getParentFile().mkdirs();
        }

        chavePrivadaFileServer.createNewFile();

        if (chavePublicaFileServer.getParentFile() != null) {
            chavePublicaFileServer.getParentFile().mkdirs();
        }

        chavePublicaFileServer.createNewFile();

        // Salva a Chave Pblica do servidor no arquivo
        ObjectOutputStream chavePublicaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePublicaFileServer));
        chavePublicaOSS.writeObject(key.getPublic());
        chavePublicaOSS.close();

        // Salva a Chave Privada do servidor no arquivo
        ObjectOutputStream chavePrivadaOSS = new ObjectOutputStream(
                new FileOutputStream(chavePrivadaFileServer));
        chavePrivadaOSS.writeObject(key.getPrivate());
        chavePrivadaOSS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}