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) throws InvalidAlgorithmParameterException 

Source Link

Document

Initializes the key pair generator using the specified parameter set and the SecureRandom implementation of the highest-priority installed provider as the source of randomness.

Usage

From source file:org.apache.abdera2.common.security.DHBase.java

private void init() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidParameterSpecException, InvalidKeySpecException {
    AlgorithmParameterGenerator pgen = AlgorithmParameterGenerator.getInstance("DH");
    pgen.init(512);/*from  w w w .  j  a  v a  2s  .  c o  m*/
    AlgorithmParameters params = pgen.generateParameters();
    DHParameterSpec dhspec = (DHParameterSpec) params.getParameterSpec(DHParameterSpec.class);
    KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("DH");
    keypairgen.initialize(dhspec);
    keyPair = keypairgen.generateKeyPair();
    p = dhspec.getP();
    g = dhspec.getG();
    l = dhspec.getL();
}

From source file:SecureConnection.java

private byte[] getPublicKeyStep2(DHParameterSpec dhParamSpec) throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH");
    keyPairGen.initialize(dhParamSpec);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    this.keyAgree = KeyAgreement.getInstance("DH");
    this.keyAgree.init(keyPair.getPrivate());
    return keyPair.getPublic().getEncoded();
}

From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java

public KeyPair generateKeyPair(int keyLength) {
    KeyPair kp = null;/*from  w  w w  . j a  v a  2  s . c  om*/
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(getAlgorithm());
        kpg.initialize(keyLength);
        kp = kpg.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        LOG.error("Error generating the key pair.", e);
    }
    return kp;
}

From source file:com.titilink.common.app.EncryptDecryptUtil.java

public void testRSA() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        BadPaddingException, IllegalBlockSizeException, SignatureException {
    ////from w  ww.  j  a  va2s.c  o  m
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(1024);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    //?
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    //??
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, privateKey, new SecureRandom());
    byte[] cipherData = cipher
            .doFinal("this is a security text from server".getBytes(Charset.forName("UTF-8")));

    //
    Cipher cipher1 = Cipher.getInstance("RSA");
    cipher1.init(Cipher.DECRYPT_MODE, publicKey, new SecureRandom());
    byte[] plainData = cipher1.doFinal(cipherData);
    System.out.println(new String(plainData, Charset.forName("UTF-8")));

    //???????
    Signature signature = Signature.getInstance("MD5withRSA");
    signature.initSign(privateKey);
    signature.update(cipherData);
    byte[] signData = signature.sign();

    //?????
    Signature signature1 = Signature.getInstance("MD5withRSA");
    signature1.initVerify(publicKey);
    signature1.update(cipherData);
    System.out.println(signature1.verify(signData));

}

From source file:org.kde.kdeconnect.NetworkPackageTest.java

public void testEncryption() throws JSONException {
    NetworkPackage original = new NetworkPackage("com.test");
    original.set("hello", "hola");

    NetworkPackage copy = NetworkPackage.unserialize(original.serialize());

    NetworkPackage decrypted = new NetworkPackage("");

    KeyPair keyPair;//from ww  w  .  j  a v a 2s. c om
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        keyPair = keyGen.genKeyPair();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("KDE/initializeRsaKeys", "Exception");
        return;
    }

    PrivateKey privateKey = keyPair.getPrivate();
    assertNotNull(privateKey);

    PublicKey publicKey = keyPair.getPublic();
    assertNotNull(publicKey);

    // Encrypt and decrypt np
    assertEquals(original.getType(), "com.test");
    try {
        NetworkPackage encrypted = RsaHelper.encrypt(original, publicKey);
        assertEquals(encrypted.getType(), NetworkPackage.PACKAGE_TYPE_ENCRYPTED);

        decrypted = RsaHelper.decrypt(encrypted, privateKey);
        assertEquals(decrypted.getType(), "com.test");

    } catch (Exception e) {
        e.printStackTrace();
    }

    // np should be equal to np2
    assertEquals(decrypted.getLong("id"), copy.getLong("id"));
    assertEquals(decrypted.getType(), copy.getType());
    assertEquals(decrypted.getJSONArray("body"), copy.getJSONArray("body"));

    String json = "{\"body\":{\"nowPlaying\":\"A really long song name - A really long artist name\",\"player\":\"A really long player name\",\"the_meaning_of_life_the_universe_and_everything\":\"42\"},\"id\":945945945,\"type\":\"kdeconnect.a_really_really_long_package_type\"}\n";
    NetworkPackage longJsonNp = NetworkPackage.unserialize(json);
    try {
        NetworkPackage encrypted = RsaHelper.encrypt(longJsonNp, publicKey);
        decrypted = RsaHelper.decrypt(encrypted, privateKey);
        String decryptedJson = decrypted.serialize();
        JSONAssert.assertEquals(json, decryptedJson, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.metaeffekt.dcc.agent.AuthenticationKeyGenerator.java

private KeyPair generateKey() throws NoSuchAlgorithmException {
    final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyGenerator.initialize(DEFAULT_KEY_SIZE);

    return keyGenerator.generateKeyPair();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions.java

@Override
public void init() throws MalformedURLException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM, "BC");
    kpg.initialize(KEY_SIZE);
    caKeyPair = kpg.genKeyPair();// www . j a v  a 2  s. c  o  m

    X500NameBuilder subjectBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    subjectBuilder.addRDN(BCStyle.CN, "RootCA");

    try {
        sigGen = new JcaContentSignerBuilder(SIGNATURE_ALGORITHM).setProvider("BC")
                .build(caKeyPair.getPrivate());
        X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(subjectBuilder.build(),
                BigInteger.ONE, new Date(), new Date(System.currentTimeMillis() + 600000),
                subjectBuilder.build(), caKeyPair.getPublic());
        caCert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certGen.build(sigGen));

        caCert.checkValidity();
        caCert.verify(caKeyPair.getPublic());
        caCert.verify(caCert.getPublicKey());
    } catch (OperatorCreationException ex) {
        throw new GeneralSecurityException(ex);
    }
}

From source file:ru.jts_dev.authserver.config.KeyGenerationConfig.java

@Bean
public KeyPairGenerator RSAKeyPairGenerator()
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    KeyPairGenerator keygen;

    keygen = KeyPairGenerator.getInstance("RSA");
    RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
    keygen.initialize(spec);

    return keygen;
}

From source file:org.cogroo.addon.util.SecurityUtil.java

public KeyPair genKeyPair() {
    KeyPair kpr = null;//  ww w . java  2s  . c  o  m
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(new RSAKeyGenParameterSpec(RSAKEYSIZE, RSAKeyGenParameterSpec.F4));
        kpr = kpg.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        LOG.log(Level.SEVERE, "Error generating key pair", e);
    } catch (InvalidAlgorithmParameterException e) {
        LOG.log(Level.SEVERE, "Error generating key pair", e);
    }
    return kpr;
}

From source file:com.streamsets.pipeline.lib.remote.SSHDUnitTest.java

protected KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(2048);
    return keyGen.generateKeyPair();
}