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:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * ?????/*from   w  w w. j av  a 2 s .c  o  m*/
 * <dl>
 * <dt>?
 * <dd>EC??????256??????
 * </dl>
 * @return ?(???)
 */
public static KeyPair createSignKeyPair() {
    try {
        final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(256);
        return keyPairGenerator.generateKeyPair();
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.aaasec.sigserv.cssigapp.KeyStoreFactory.java

private static KeyPair generateECDSAKeyPair()
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("P-256");

    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
    g.initialize(ecSpec, new SecureRandom());
    KeyPair pair = g.generateKeyPair();
    return pair;/*from w  w w .j av a 2s .  c o m*/
}

From source file:org.teknux.jettybootstrap.keystore.JettyKeystore.java

private static KeyPair generateKeyPair(String algorithm) throws JettyKeystoreException {
    try {//from  ww  w. j av a 2s . c  o  m
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new JettyKeystoreException(JettyKeystoreException.ERROR_CREATE_KEYS,
                "Can not generate private and public keys", e);
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * RSA???//from  www .j ava 2 s  . c  o  m
 * <dl>
 * <dt>?
 * <dd>RSA??????2048??????
 * </dl>
 * @return RSA?
 */
public static KeyPair createKeyPair() {
    try {
        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair pair = generator.generateKeyPair();
        if (LOG.isDebugEnabled()) {
            final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
            final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
            LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
            LOG.debug("public-exponent={}",
                    Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
            LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray()));
            LOG.debug("private-exponent={}",
                    Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray()));
        }
        return pair;
    } catch (final NoSuchAlgorithmException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <p>/*ww w  .ja  va  2  s  . c o m*/
 * ?(?)
 * </p>
 * 
 * @return Map<String, Object>
 * @throws Exception Exception
 */
public static Map<String, Object> genKeyPair() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(1024);
    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;
}

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

public static KeyPair genKeyPair(int bit) {
    try {//from  w  ww. j av a  2 s .co m
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM, JCE_PROVIDER);
        keyPairGen.initialize(bit, new SecureRandom());
        return keyPairGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.ligadata.EncryptUtils.EncryptionUtil.java

/**
 * Generate key which contains a pair of private and public key using 1024
 * bytes. Store the set of keys in given files publicKeyFile,privateKeyFile
 * @param algorithm/*from w  w w.  j a v a2  s.c  o m*/
 *          : algorithm used
 * @param publicKeyFile
 *          :The file containing public key
 * @param privateKeyFile
 *          :The file containing private key
 */
public static void generateSampleKeys(String algorithm, String publicKeyFile, String privateKeyFile) {
    try {
        if (areKeysPresent(publicKeyFile, privateKeyFile)) {
            return;
        }
        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File privateKeyFD = new File(privateKeyFile);
        File publicKeyFD = new File(publicKeyFile);

        // Create files to store public and private key
        if (privateKeyFD.getParentFile() != null) {
            privateKeyFD.getParentFile().mkdirs();
        }
        privateKeyFD.createNewFile();

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

        // Saving the Public key in a file
        ObjectOutputStream publicKeyOS = new ObjectOutputStream(new FileOutputStream(publicKeyFD));
        publicKeyOS.writeObject(key.getPublic());
        publicKeyOS.close();

        // Saving the Private key in a file
        ObjectOutputStream privateKeyOS = new ObjectOutputStream(new FileOutputStream(privateKeyFD));
        privateKeyOS.writeObject(key.getPrivate());
        privateKeyOS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.codice.ddf.security.certificate.generator.PkiTools.java

/**
 * Generate new RSA public/private key pair with 2048 bit key
 *
 * @return new generated key pair//from  www . ja  v  a2 s . com
 * @throws CertificateGeneratorException
 */
public static KeyPair generateRsaKeyPair() {
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
        keyGen.initialize(RSA_KEY_LENGTH);
        return keyGen.generateKeyPair();
    } catch (Exception e) {
        throw new CertificateGeneratorException("Failed to generate new public/private key pair.", e);
    }
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static KeyPair generateRandomKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
    keyPairGenerator.initialize(2048, new SecureRandom());
    return keyPairGenerator.generateKeyPair();
}

From source file:com.owncloud.android.utils.PushUtils.java

private static int generateRsa2048KeyPair() {
    String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator
            + KEYPAIR_FOLDER;/*w ww . j av a 2s .  c  om*/

    String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION;
    String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION;
    File keyPathFile = new File(keyPath);

    if (!new File(privateKeyPath).exists() && !new File(publicKeyPath).exists()) {
        try {
            if (!keyPathFile.exists()) {
                keyPathFile.mkdir();
            }
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);

            KeyPair pair = keyGen.generateKeyPair();
            int statusPrivate = saveKeyToFile(pair.getPrivate(), privateKeyPath);
            int statusPublic = saveKeyToFile(pair.getPublic(), publicKeyPath);

            if (statusPrivate == 0 && statusPublic == 0) {
                // all went well
                return 0;
            } else {
                return -2;
            }
        } catch (NoSuchAlgorithmException e) {
            Log_OC.d(TAG, "RSA algorithm not supported");
        }
    } else {
        // we already have the key
        return -1;
    }

    // we failed to generate the key
    return -2;
}