Search in sources :

Example 96 with KeyPairGenerator

use of java.security.KeyPairGenerator in project cdap by caskdata.

the class KeyStores method generatedCertKeyStore.

/**
   * Create a Java key store with a stored self-signed certificate.
   * @return Java keystore which has a self signed X.509 certificate
   */
public static KeyStore generatedCertKeyStore(SConfiguration sConf, String password) {
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM);
        SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM, SECURE_RANDOM_PROVIDER);
        keyGen.initialize(KEY_SIZE, random);
        // generate a key pair
        KeyPair pair = keyGen.generateKeyPair();
        int validity = sConf.getInt(Constants.Security.SSL.CERT_VALIDITY, VALIDITY);
        X509Certificate cert = getCertificate(DISTINGUISHED_NAME, pair, validity, SIGNATURE_ALGORITHM);
        KeyStore keyStore = KeyStore.getInstance(SSL_KEYSTORE_TYPE);
        keyStore.load(null, password.toCharArray());
        keyStore.setKeyEntry(CERT_ALIAS, pair.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert });
        return keyStore;
    } catch (Exception e) {
        throw new RuntimeException("SSL is enabled but a key store file could not be created. A keystore is required " + "for SSL to be used.", e);
    }
}
Also used : KeyPair(java.security.KeyPair) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 97 with KeyPairGenerator

use of java.security.KeyPairGenerator in project JustAndroid by chinaltz.

the class AbRsa method generateRSAKeyPair.

/**
	 * 随机生成RSA密钥对
	 * @param keyLength 密钥长度,范围:512~2048 一般1024
	 * @return
	 */
public static KeyPair generateRSAKeyPair(int keyLength) {
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
        kpg.initialize(keyLength);
        return kpg.genKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 98 with KeyPairGenerator

use of java.security.KeyPairGenerator in project ddf by codice.

the class PkiTools method generateRsaKeyPair.

/**
     * Generate new RSA public/private key pair with 2048 bit key
     *
     * @return new generated key pair
     * @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);
    }
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 99 with KeyPairGenerator

use of java.security.KeyPairGenerator in project hbase by apache.

the class KeyStoreTestUtil method generateKeyPair.

public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
    keyGen.initialize(1024);
    return keyGen.genKeyPair();
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator)

Example 100 with KeyPairGenerator

use of java.security.KeyPairGenerator in project camel by apache.

the class XmlSignatureTest method getKeyPair.

public static KeyPair getKeyPair(String algorithm, int keylength) {
    KeyPairGenerator keyGen;
    try {
        keyGen = KeyPairGenerator.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    keyGen.initialize(keylength, new SecureRandom());
    return keyGen.generateKeyPair();
}
Also used : SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

KeyPairGenerator (java.security.KeyPairGenerator)197 KeyPair (java.security.KeyPair)145 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)43 SecureRandom (java.security.SecureRandom)39 PublicKey (java.security.PublicKey)27 PrivateKey (java.security.PrivateKey)26 X509Certificate (java.security.cert.X509Certificate)23 KeyFactory (java.security.KeyFactory)21 IOException (java.io.IOException)19 BigInteger (java.math.BigInteger)17 GeneralSecurityException (java.security.GeneralSecurityException)15 Signature (java.security.Signature)15 Date (java.util.Date)15 Cipher (javax.crypto.Cipher)15 KeyAgreement (javax.crypto.KeyAgreement)15 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 X500Principal (javax.security.auth.x500.X500Principal)13 ECPrivateKey (java.security.interfaces.ECPrivateKey)12 ECPublicKey (java.security.interfaces.ECPublicKey)12 HashMap (java.util.HashMap)11