Example usage for java.security KeyPairGenerator generateKeyPair

List of usage examples for java.security KeyPairGenerator generateKeyPair

Introduction

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

Prototype

public KeyPair generateKeyPair() 

Source Link

Document

Generates a key pair.

Usage

From source file:net.firejack.platform.web.security.x509.KeyUtils.java

public static KeyPair generate(File keystore) {
    if (keystore == null) {
        throw new IllegalArgumentException("Key Store file should not be null.");
    }//from   www . j  a v a 2  s  . c om

    try {
        KeyStore ks = KeyStore.getInstance("JKS", "SUN");
        if (keystore.exists()) {
            FileInputStream stream = new FileInputStream(keystore);
            ks.load(stream, SECRET);
            IOUtils.closeQuietly(stream);
        } else {
            ks.load(null, SECRET);
        }

        if (ks.containsAlias(ALIAS)) {
            PrivateKey privateKey = (PrivateKey) ks.getKey(ALIAS, SECRET);
            PublicKey publicKey = ks.getCertificate(ALIAS).getPublicKey();
            return new KeyPair(publicKey, privateKey);
        } else {
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
            generator.initialize(KEYSIZE, new SecureRandom());
            return generator.generateKeyPair();
        }
    } catch (Throwable th) {
        logger.error("Failed to initialize key store");
        throw new OpenFlameRuntimeException(th.getMessage(), th);
    }
}

From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java

public static KeyPair generateKey() throws GeneralSecurityException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
    keyGen.initialize(1024);/* w w  w  .  ja v  a  2  s. co m*/
    KeyPair key = keyGen.generateKeyPair();
    return key;
}

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

public static KeyPair generateKey() throws Exception {

    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(Constants.RSA_KEY_SIZE);
    return keyPairGen.generateKeyPair();
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

public static KeyPair generateKeyPair() {
    try {/* www. j av  a 2s. co  m*/
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ASYM_ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        keyGen.initialize(512, random);
        return keyGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("No such algorithm:" + e.getMessage());
    }
}

From source file:net.jmhertlein.core.crypto.Keys.java

/**
 * Generates a new Elliptic Curve Digital Signature Algorithm (ECDSA) public/private key pair.
 *
 * System's default SecureRandom is used
 * @param curveName the name of a pre-defined elliptic curve (e.g. secp521r1)
 * @param provider the JCE provider to use
 * @return a new ECDSA key pair//from  www. j a  va2  s .  c  o  m
 */
public static KeyPair newECDSAKeyPair(String curveName, String provider) {
    KeyPair ret;
    try {
        ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(curveName);
        KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", provider);
        g.initialize(ecGenSpec, new SecureRandom());
        ret = g.generateKeyPair();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException ex) {
        Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex);
        ret = null;
    }

    return ret;
}

From source file:org.panbox.core.crypto.CryptCore.java

public static KeyPair generateKeypair() {
    try {//from  w w w .j  a v  a  2  s.  c  om
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(KeyConstants.KEY_FACTORY, KeyConstants.PROV_BC);
        kpg.initialize(KeyConstants.ASYMMETRIC_KEYSIZE);
        KeyPair kp = kpg.generateKeyPair();
        return kp;
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error during asymmetric key pair generation: " + e);
    } catch (NoSuchProviderException e) {
        logger.error("Error during asymmetric key pair generation: " + e);
    }
    return null;
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?//  ww w  .  j  a  v a  2  s  .  c o  m
 *
 * @return Map Map
 * @throws Exception
 */
public static Map<String, String> initKey(UUID licenseCode) throws Exception {
    // ?
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

    // ??
    //        keyPairGen.initialize(KEY_SIZE);
    keyPairGen.initialize(KEY_SIZE, new SecureRandom(licenseCode.toString().getBytes(UTF_8)));

    // ?
    KeyPair keyPair = keyPairGen.generateKeyPair();

    // 
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

    // ?
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    // ?
    Map<String, String> keyMap = new HashMap<String, String>();

    keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
    keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));
    keyMap.put(LICENSE_CODE, licenseCode.toString());

    return keyMap;
}

From source file:com.peterphi.std.crypto.keygen.CaHelper.java

public static KeyPair generateKeyPair(int bits) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(bits, new SecureRandom());
    return keyGen.generateKeyPair();
}

From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java

public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // ???/*from ww  w  .  j  a v  a 2 s.c o m*/
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed(seed); // ??
    // ??, ??keysize ?. 512  1024  64 ?
    keygen.initialize(512, secrand);
    // ?pubkey?prikey
    KeyPair keys = keygen.generateKeyPair(); // ?
    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded());
    OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME));
    ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();

    byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded());
    os = new FileOutputStream(new File(PRIKEY_FILE_NAME));
    bais = new ByteArrayInputStream(prikeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>//  w w  w .  ja  v a 2  s  . co m
 * ?(?)
 * </p>
 * 
 * @return
 * @throws Exception
 */
public static Map<String, Object> genKeyPair() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(512);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    Map<String, Object> keyMap = new HashMap<String, Object>(2);
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}