List of usage examples for java.security KeyPairGenerator getInstance
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.streamreduce.util.CAGenerator.java
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024);//from ww w. j ava2s .com return keyGen.generateKeyPair(); }
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ???// w w w .ja va 2s . c om * ????hacker??license * @throws Exception */ public static void generateKey() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyGen.initialize(1024, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); log.info("?"); DataOutputStream out = new DataOutputStream(new FileOutputStream(PUBLIC_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(pub.getEncoded())); out.close(); log.info("??" + PUBLIC_KEY_FILE); out = new DataOutputStream(new FileOutputStream(PRIVATE_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(priv.getEncoded())); out.close(); log.info("??" + PRIVATE_KEY_FILE); }
From source file:im.whistle.crypt.Crypt.java
/** * Generates a private/public key pair.// w w w . j av a 2 s . c o m * @param args Arguments, element at 0 is the key size * @param callback Callback */ public static void genkeys(JSONArray args, AsyncCallback<JSONArray> callback) { try { Log.i("whistle", "Generating key pair ..."); PRNGProvider.init(); // Ensure OpenSSL fix KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); int bits = args.getInt(0); int exp = args.getInt(1); keyPairGenerator.initialize(new RSAKeyGenParameterSpec(bits, BigInteger.valueOf(exp))); KeyPair keyPair = keyPairGenerator.genKeyPair(); String priv = "-----BEGIN RSA PRIVATE KEY-----\n" + Base64.encodeToString(keyPair.getPrivate().getEncoded(), Base64.DEFAULT).trim() + "\n-----END RSA PRIVATE KEY-----"; String pub = "-----BEGIN PUBLIC KEY-----\n" + Base64.encodeToString(keyPair.getPublic().getEncoded(), Base64.DEFAULT).trim() + "\n-----END PUBLIC KEY-----"; JSONArray res = new JSONArray(); res.put(priv); res.put(pub); callback.success(res); } catch (Exception ex) { Log.w("whistle", "Key pair generation failed: " + ex.getMessage()); callback.error(ex); } }
From source file:bftsmart.tom.util.RSAKeyPairGenerator.java
/** * Generate the key pair for the process with id = <id> and put it on the * files config/keys/publickey<id> and config/keys/privatekey<id> * * @param id the id of the process to generate key * @throws Exception something goes wrong when writing the files *///from w w w .j a v a 2 s . co m public void run(int id, int size) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(size); KeyPair kp = keyGen.generateKeyPair(); PublicKey puk = kp.getPublic(); PrivateKey prk = kp.getPrivate(); saveToFile(id, puk, prk); }
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 .ja va2 s .co m*/ 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:edu.vt.middleware.crypt.util.CryptReaderWriterTest.java
/** * @return Public key test data./* w ww . j a va 2s . c o m*/ * * @throws Exception On test data generation failure. */ @DataProvider(name = "pubkeydata") public Object[][] createPubKeyTestData() throws Exception { final KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA"); final KeyPair rsaKeys = rsaKeyGen.generateKeyPair(); final KeyPairGenerator dsaKeyGen = KeyPairGenerator.getInstance("DSA"); final KeyPair dsaKeys = dsaKeyGen.generateKeyPair(); return new Object[][] { { rsaKeys.getPublic() }, { dsaKeys.getPublic() }, }; }
From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java
/** * // ww w. j a v a 2s .co m */ public void init() { try { this.keyPair = KeyPairGenerator.getInstance(this.algorithm).generateKeyPair(); } catch (NoSuchAlgorithmException e) { log.error(e.getMessage(), e); throw new IllegalStateException(e.getMessage(), e); } }
From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java
/** * Generate key which contains a pair of privae and public key using 1024 bytes * @return key pair// w ww. j av a 2 s .c o m * @throws NoSuchAlgorithmException */ public static KeyPair generateKey(String keyPhrase) throws GeneralSecurityException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM); SecureRandom randomAlg = SecureRandom.getInstance("SHA1PRNG", "SUN"); randomAlg.setSeed(keyPhrase.getBytes()); keyGen.initialize(1024, randomAlg); KeyPair key = keyGen.generateKeyPair(); return key; }
From source file:org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator.java
@VisibleForTesting public static String generate() throws NoSuchAlgorithmException, IOException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); SecureRandom sr = new SecureRandom(); keyGen.initialize(KEY_SIZE, sr);//w ww . ja va 2 s . c o m KeyPair keypair = keyGen.generateKeyPair(); return toString(keypair.getPrivate()); }
From source file:org.apache.xml.security.test.algorithms.SignatureAlgorithmTest.java
public void testSameKeySeveralAlgorithmSigning() throws Exception { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); SignatureAlgorithm signatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1); PrivateKey pk = KeyPairGenerator.getInstance("RSA").genKeyPair().getPrivate(); signatureAlgorithm.initSign(pk);//from w ww.jav a 2 s. c o m signatureAlgorithm.update((byte) 2); signatureAlgorithm.sign(); SignatureAlgorithm otherSignatureAlgorithm = new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256); try { otherSignatureAlgorithm.initSign(pk); } catch (XMLSecurityException ex) { log.warn("Test testSameKeySeveralAlgorithmSigning skipped as necessary algorithms not available"); return; } otherSignatureAlgorithm.update((byte) 2); otherSignatureAlgorithm.sign(); }