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:org.kse.gui.dialogs.sign.DSignJar.java

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    Security.addProvider(new BouncyCastleProvider());

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override//  w ww  . j  a  va2  s  .  c  o  m
        public void run() {
            try {
                KeyPairGenerator kpg = KeyPairGenerator.getInstance(KeyPairType.RSA.jce(), "BC");
                kpg.initialize(1024, new SecureRandom());
                KeyPair kp = kpg.generateKeyPair();
                DSignJar dialog = new DSignJar(new JFrame(), kp.getPrivate(), KeyPairType.RSA, "signature name",
                        null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }

                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC");
    EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16));

    ECParameterSpec ecSpec = new ECParameterSpec(curve,
            new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
                    new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1);

    keyGen.initialize(ecSpec, new SecureRandom());

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair aPair = keyGen.generateKeyPair();
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());//from w w  w .j a  va2 s . c om
    bKeyAgree.init(bPair.getPrivate());

    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);

    MessageDigest hash = MessageDigest.getInstance("SHA1", "BC");

    System.out.println(new String(hash.digest(aKeyAgree.generateSecret())));
    System.out.println(new String(hash.digest(bKeyAgree.generateSecret())));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDH", "BC");
    EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(new BigInteger("fffffffffffffffffffffffffffffffeffffffffffffffff", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16));

    ECParameterSpec ecSpec = new ECParameterSpec(curve,
            new ECPoint(new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16),
                    new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16)),
            new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), 1);

    keyGen.initialize(ecSpec, new SecureRandom());

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair aPair = keyGen.generateKeyPair();
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());//  w  ww  .j  ava  2  s.  c  om
    bKeyAgree.init(bPair.getPrivate());

    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);

    MessageDigest hash = MessageDigest.getInstance("SHA1", "BC");

    System.out.println(new String(hash.digest(aKeyAgree.generateSecret())));
    System.out.println(new String(hash.digest(bKeyAgree.generateSecret())));
}

From source file:MainClass.java

public static void createKey() throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");

    kpg.initialize(512);//from   w w  w  .  j a v a 2 s.  com
    KeyPair kp = kpg.generateKeyPair();
    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

    DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class);
}

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.streamreduce.util.CAGenerator.java

public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(1024);//  w  w w .  j a  v a2s. co m
    return keyGen.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:com.java.demo.RsaDemo.java

public static void Init() {
    try {//w ww  .  ja  va2 s . c o m
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair kp = keyPairGenerator.generateKeyPair();
        rSAPublicKey = (RSAPublicKey) kp.getPublic();
        rSAPrivateKey = (RSAPrivateKey) kp.getPrivate();
    } catch (Exception ex) {
        System.out.println("com.Java.Demo.RsaDemo.Init()" + ex.getMessage());
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static KeyPair generateRsaPairWithGenerator(Context ctx, String alais)
        throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
    Calendar notBefore = Calendar.getInstance();
    Calendar notAfter = Calendar.getInstance();
    notAfter.add(1, Calendar.YEAR);
    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx).setAlias(alais)
            .setSubject(new X500Principal(String.format("CN=%s, OU=%s", alais, ctx.getPackageName())))
            .setSerialNumber(BigInteger.ONE).setStartDate(notBefore.getTime()).setEndDate(notAfter.getTime())
            .build();//from   w ww .  ja v a 2  s . c  o m

    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
    kpGenerator.initialize(spec);
    KeyPair kp = kpGenerator.generateKeyPair();

    return kp;
}

From source file:MainClass.java

public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");

    DHParameterSpec param = new DHParameterSpec(p, g);
    kpg.initialize(param);/*w  w w .  jav a 2  s .  c  o m*/
    KeyPair kp = kpg.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

    DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class);
}