List of usage examples for java.security KeyPairGenerator generateKeyPair
public KeyPair generateKeyPair()
From source file:Main.java
public static KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYGEN_ALGORITHM/* , PROVIDER_NAME */); SecureRandom secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM/* , PROVIDER_NAME */); generator.initialize(keySize, secureRandom); return generator.generateKeyPair(); }
From source file:com.axelor.apps.account.ebics.certificate.KeyUtil.java
/** * Generates a <code>KeyPair</code> in RSA format. * * @param keyLen - key size//from w w w .ja v a2 s.c o m * @return KeyPair the key pair * @throws NoSuchAlgorithmException */ public static KeyPair makeKeyPair(int keyLen) throws NoSuchAlgorithmException { KeyPairGenerator keyGen; keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(keyLen, new SecureRandom()); KeyPair keypair = keyGen.generateKeyPair(); return keypair; }
From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java
private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed("VimTech".getBytes("UTF-8")); keyGen.initialize(1024, random);//from ww w.j a v a 2 s. c om KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); System.out.println(priv); System.out.println(pub); byte[] encoded = pub.getEncoded(); byte[] encodeBase64 = Base64.encodeBase64(encoded); System.out.println("Public Key:" + new String(encodeBase64)); byte[] encodedPrv = priv.getEncoded(); byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv); System.out.println("Private Key:" + new String(encodeBase64Prv)); byte[] decodeBase64 = Base64.decodeBase64(encodeBase64); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub)); }
From source file:oscar.oscarLab.ca.all.util.KeyPairGen.java
/** * Create a key pair for the specified service and store it in the database *//* w ww.j ava 2 s. co m*/ public static String createKeys(String name, String type) { byte[] pubKey; byte[] privKey; Base64 base64 = new Base64(); // Generate an RSA key try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair key = keyGen.generateKeyPair(); pubKey = base64.encode(key.getPublic().getEncoded()); privKey = base64.encode(key.getPrivate().getEncoded()); if (name.equals("oscar")) { // the primary key is name, therefore this statement will only // be able to run once and the oscar key pair will not be overwritten OscarKeyDao oscarKeyDao = (OscarKeyDao) SpringUtils.getBean("oscarKeyDao"); OscarKey oscarKey = new OscarKey(); oscarKey.setName("oscar"); oscarKey.setPublicKey(new String(pubKey, MiscUtils.ENCODING)); oscarKey.setPrivateKey(new String(privKey, MiscUtils.ENCODING)); oscarKeyDao.persist(oscarKey); // no keys need to be returned so return "success" instead to // indicate the operation completed successfully return ("success"); } else { PublicKeyDao publicKeyDao = (PublicKeyDao) SpringUtils.getBean("publicKeyDao"); PublicKey publicKeyObject = new PublicKey(); publicKeyObject.setService(name); publicKeyObject.setType(type); publicKeyObject.setBase64EncodedPublicKey(new String(pubKey, MiscUtils.ENCODING)); publicKeyObject.setBase64EncodedPrivateKey(new String(privKey, MiscUtils.ENCODING)); publicKeyDao.persist(publicKeyObject); return (publicKeyObject.getBase64EncodedPrivateKey()); } } catch (NoSuchAlgorithmException e) { logger.error("Could not create RSA key", e); return null; } catch (Exception e) { logger.error("Could not save keys", e); return null; } }
From source file:com.iterzp.momo.utils.RSAUtils.java
/** * ?/*w ww . ja v a2 s .co m*/ * * @return */ public static KeyPair generateKeyPair() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER); keyPairGenerator.initialize(KEY_SIZE, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?//from www . j a va 2s. co m * * @throws Exception */ static void generatorKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); byte[] keyBs = rsaPublicKey.getEncoded(); publicKey = encodeBase64(keyBs); System.out.println("?\r\n" + publicKey); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); keyBs = rsaPrivateKey.getEncoded(); privateKey = encodeBase64(keyBs); System.out.println("??\r\n" + privateKey); }
From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java
public static KeyPair getRSAKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "SC"); g.initialize(2048);/*ww w.ja v a2 s .c o m*/ return g.generateKeyPair(); }
From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java
public static KeyPair generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { SecureRandom random = new SecureRandom(); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA"); g.initialize(ecSpec, random);/*from ww w . j a v a2 s .c om*/ return g.generateKeyPair(); }
From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java
public static KeyPair getKeyPair() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException { //ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("prime192v1"); ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("secp256r1"); KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "SC"); g.initialize(ecGenSpec, new SecureRandom()); return g.generateKeyPair(); }
From source file:org.wso2.carbon.identity.test.common.testng.utils.ReadCertStoreSampleUtil.java
public static KeyPair getSampleKeyPair() throws CertificateException, NoSuchAlgorithmException, IOException, InvalidKeyException, KeyStoreException, NoSuchProviderException, SignatureException { KeyStore keyStore = KeyStore.getInstance("JKS"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "SHA1WithRSA"); SecureRandom random = SecureRandom.getInstance("RSA", "SHA1WithRSA"); keyGen.initialize(1024, random);/*from w w w. j a v a2s .c om*/ KeyPair keypair = keyGen.generateKeyPair(); return keypair; }