Example usage for java.security KeyPairGenerator initialize

List of usage examples for java.security KeyPairGenerator initialize

Introduction

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

Prototype

public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException 

Source Link

Document

Initializes the key pair generator with the given parameter set and source of randomness.

Usage

From source file:MainClass.java

public static KeyPair generateRSAKeyPair() throws Exception {
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
    kpGen.initialize(1024, new SecureRandom());
    return kpGen.generateKeyPair();
}

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 genKeyPair() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchProviderException, InvalidKeyException, SignatureException {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGen.initialize(1024, new SecureRandom());
    KeyPair keypair = keyPairGen.genKeyPair();
    return keypair;
}

From source file:Main.java

static KeyPair generateRsaKeyPair(int keySizeInBits) {
    if (keySizeInBits == 0 || keySizeInBits % 8 != 0)
        return null;
    try {/*from   w  ww. j  a v a 2 s . com*/
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SC");
        generator.initialize(keySizeInBits, new SecureRandom());

        KeyPair pair = generator.generateKeyPair();

        // Log.d("","public : "+keyToBase64(pair.getPublic()));
        // Log.d("","private: "+keyToBase64(pair.getPrivate()));

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

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

/**
 * ???//from   w  w  w. jav  a2  s . c om
 * ????hacker??license
 * @throws Exception
 */
public static void generateKey() throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyGen.initialize(1024, new SecureRandom());
    KeyPair pair = keyGen.generateKeyPair();
    PrivateKey priv = pair.getPrivate();
    PublicKey pub = pair.getPublic();

    log.info("?");
    DataOutputStream out = new DataOutputStream(new FileOutputStream(PUBLIC_KEY_FILE));
    out.writeBytes(EasyUtils.encodeHex(pub.getEncoded()));
    out.close();
    log.info("??" + PUBLIC_KEY_FILE);

    out = new DataOutputStream(new FileOutputStream(PRIVATE_KEY_FILE));
    out.writeBytes(EasyUtils.encodeHex(priv.getEncoded()));
    out.close();
    log.info("??" + PRIVATE_KEY_FILE);
}

From source file:com.lingxiang2014.util.RSAUtils.java

public static KeyPair generateKeyPair() {
    try {/*from  w  w w.j av a  2 s. co  m*/
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER);
        keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:MainClass.java

public static 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);/* w ww .  ja  v a2 s .  com*/
    keyGenerator.initialize(1024, rng);

    return (keyGenerator.generateKeyPair());
}

From source file:org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator.java

@VisibleForTesting
public static String generate() throws NoSuchAlgorithmException, IOException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    SecureRandom sr = new SecureRandom();
    keyGen.initialize(KEY_SIZE, sr);
    KeyPair keypair = keyGen.generateKeyPair();
    return toString(keypair.getPrivate());
}

From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java

/**
 * Generates a <code>KeyPair</code> in RSA format.
 *
 * @param keyLen - key size/*from  ww w .j a  va  2s . com*/
 * @return KeyPair the key pair
 * @throws NoSuchAlgorithmException
 */
public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException {
    KeyPairGenerator keyGen;

    keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(keyLen, new SecureRandom());

    KeyPair keypair = keyGen.generateKeyPair();

    return keypair;

}

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 a2s .co  m*/
    kpGenerator.initialize(2014, random);
    KeyPair keyPair = kpGenerator.generateKeyPair();
    return keyPair;
}