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:no.digipost.api.client.ApiServiceMock.java

static EncryptionKey createFakeEncryptionKey() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (Writer osWriter = new OutputStreamWriter(baos); PEMWriter writer = new PEMWriter(osWriter)) {

        KeyPairGenerator factory = KeyPairGenerator.getInstance("RSA");
        factory.initialize(2048);//w w w  .  j  ava  2s  .  c  om
        KeyPair keyPair = factory.generateKeyPair();

        writer.writeObject(keyPair.getPublic());

    } catch (Exception e) {
        throw new RuntimeException("Failed creation of fake encryption key.", e);
    }

    EncryptionKey fakeKey = new EncryptionKey();
    fakeKey.setKeyId("fake-hash");
    fakeKey.setValue(new String(baos.toByteArray()));

    return fakeKey;
}

From source file:pepperim.util.IMCrypt.java

/**
 * Generates a new 2048 bit RSA keypair.
 * @return String array containing: [Base64-encoded public key, Base64-encoded private key]
 *//*from  w ww  .ja  v  a 2s  .  com*/
public static String[] RSA_genKeypair() {
    try {
        KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = new SecureRandom();
        pairgen.initialize(2048, random);
        KeyPair keyPair = pairgen.generateKeyPair();
        String[] keypair = new String[2];
        keypair[0] = B64_Enc(keyPair.getPublic().getEncoded());
        keypair[1] = B64_Enc(keyPair.getPrivate().getEncoded());
        return keypair;
    } catch (GeneralSecurityException e) {
        Main.log(e.getMessage());
        return null;
    }
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ??/*from  w  w w  .j a  v  a2s.c  om*/
 * 
 * @param seed
 *            ??
 * @return
 * @throws Exception
 */
public static Map<String, String> generateKey(String seed) throws Exception {
    Map<String, String> map = new HashMap<String, String>(2);
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    Base64 base64 = new Base64();
    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    // BASE64Encoder encoder = new BASE64Encoder();
    // map.put("public", encoder.encode(publicKey.getEncoded()));
    // map.put("private", encoder.encode(privateKey.getEncoded()));
    map.put("public", publicKeyString);
    map.put("private", privateKeyString);

    System.out.println("publicKey: " + map.get("public"));
    System.out.println("privateKey: " + map.get("private"));
    return map;
}

From source file:com.intuit.s3encrypt.S3Encrypt.java

private static KeyPair createKeyFile(String filename)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
    keyGenerator.initialize(2048, new SecureRandom());
    KeyPair myKeyPair = keyGenerator.generateKeyPair();

    PublicKey publicKey = myKeyPair.getPublic();
    PrivateKey privateKey = myKeyPair.getPrivate();

    System.out.println("keys created... " + filename);

    saveKeyPair(filename, myKeyPair);// w w  w  .j a v a 2 s .  c o m
    return myKeyPair;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.M)
private static void generateNewKey() {
    try {//ww w.  j  a va2  s  .  c  o  m
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
                KEY_PROVIDER);

        keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).build());
        keyPairGenerator.generateKeyPair();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ?DSA??p,q,g,j,x,y//from   ww w  .  j a v a 2  s  .c  om
 * 
 * @param seed
 *            ??
 * @throws Exception
 * @author huangbuji
 *         <p />
 *         Create at 2014-2-8 ?4:45:26
 */
@SuppressWarnings("restriction")
public static void genKey(String seed) throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    DSAPublicKeyImpl publicKey = (DSAPublicKeyImpl) keyPair.getPublic();
    DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
    DSAParams dsaParams = privateKey.getParams();
    Base64 base64 = new Base64();
    String p = new String(base64.encode(dsaParams.getP().toByteArray()), "utf8");
    String q = new String(base64.encode(dsaParams.getQ().toByteArray()), "utf8");
    String g = new String(base64.encode(dsaParams.getG().toByteArray()), "utf8");
    String x = new String(base64.encode(privateKey.getX().toByteArray()), "utf8");
    String y = new String(base64.encode(publicKey.getY().toByteArray()), "utf8");
    System.out.println("P: " + p);
    System.out.println("Q: " + q);
    System.out.println("G: " + g);
    System.out.println("X: " + x);
    System.out.println("Y: " + y);

    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    System.err.println("public: " + publicKeyString);
    System.err.println("private: " + privateKeyString);

    File publicFile = new File("D:/binPublic.ky");
    File privateFile = new File("D:/binPrivate.ky");
    FileOutputStream out = new FileOutputStream(publicFile);
    out.write(publicKey.getEncoded());
    out.flush();
    out.close();
    out = new FileOutputStream(privateFile);
    out.write(privateKey.getEncoded());
    out.flush();
    out.close();
}

From source file:fi.okm.mpass.idp.authn.impl.ValidateOIDCIDTokenSignatureTest.java

protected static KeyPair generateKeyPair(final int keysize) throws NoSuchAlgorithmException {
    final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
    keyGenerator.initialize(keysize);//from  w  w w. j av a 2  s  .  c  o  m
    return keyGenerator.generateKeyPair();
}

From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.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/*  ww  w .  j a v a 2 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(SSLUtil.class.getName()).log(Level.SEVERE, null, ex);
        ex.printStackTrace(System.err);
        ret = null;
    }

    return ret;
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ?/*from  ww  w  .j  a va  2 s.  c o m*/
 * 
 * @param keySize
 *            ?
 * @return 
 */
public static KeyPair generateKeyPair(int keySize) {
    Assert.state(keySize > 0);

    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, PROVIDER);
        keyPairGenerator.initialize(keySize);
        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:cn.util.RSAUtils.java

/**
 * ??/* ww w .  j ava2 s.c  o  m*/
 * @throws NoSuchAlgorithmException 
 *
 */
public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException {
    HashMap<String, Object> map = new HashMap<String, Object>();
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    map.put("public", publicKey);
    map.put("private", privateKey);
    return map;
}