List of usage examples for java.security KeyPairGenerator genKeyPair
public final KeyPair genKeyPair()
From source file:MainClass.java
public static void main(String[] args) throws Exception { String password = "password"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024);//from ww w. j av a 2 s .co m KeyPair keyPair = keyPairGenerator.genKeyPair(); String publicKeyFilename = "public"; byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); FileOutputStream fos = new FileOutputStream(publicKeyFilename); fos.write(publicKeyBytes); fos.close(); String privateKeyFilename = "privateKeyFilename"; byte[] privateKeyBytes = keyPair.getPrivate().getEncoded(); byte[] encryptedPrivateKeyBytes = passwordEncrypt(password.toCharArray(), privateKeyBytes); fos = new FileOutputStream(privateKeyFilename); fos.write(encryptedPrivateKeyBytes); fos.close(); }
From source file:cn.lynx.emi.license.GenerateKeyPairs.java
public static void main(String[] args) throws NoSuchAlgorithmException { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); SecureRandom secrand = new SecureRandom(); secrand.setSeed("cn.lynx.emi".getBytes()); keygen.initialize(4096, secrand);// w w w .j a v a2s .c om KeyPair keys = keygen.genKeyPair(); PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate(); String myPubKey = Base64.encodeBase64String(pubkey.getEncoded()); String myPriKey = Base64.encodeBase64String(prikey.getEncoded()); System.out.println(prikey); System.out.println("pubKey=" + myPubKey); System.out.println("priKey=" + myPriKey); }
From source file:com.cliqset.magicsig.util.KeyGen.java
public static void main(String[] args) { try {//from w w w.j a v a 2 s.c o m FileOutputStream fos = new FileOutputStream(fileName); for (int x = 0; x < numKeys; x++) { fos.write((x + " RSA.").getBytes("ASCII")); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(keySize); KeyPair keyPair = keyPairGenerator.genKeyPair(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec publicSpec = keyFactory.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec privateSpec = keyFactory.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class); fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getModulus()))); fos.write(".".getBytes("ASCII")); fos.write(Base64.encodeBase64URLSafe(getBytes(publicSpec.getPublicExponent()))); fos.write(".".getBytes("ASCII")); fos.write(Base64.encodeBase64URLSafe(getBytes(privateSpec.getPrivateExponent()))); fos.write("\n".getBytes("ASCII")); System.out.println(x); } fos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:ai.susi.tools.JsonSignature.java
public static void main(String[] args) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048);// w ww . j a va2 s.c o m KeyPair keyPair = keyGen.genKeyPair(); String jsonString = "{\n" + " \"_id\": \"57b44e738d9af9fa2df13b27\",\n" + " \"index\": 0,\n" + " \"guid\": \"13af6838-08c8-4709-8dff-5ecb20bbaaa7\",\n" + " \"isActive\": false,\n" + " \"balance\": \"$2,092.08\",\n" + " \"picture\": \"http://placehold.it/32x32\",\n" + " \"age\": 22,\n" + " \"eyeColor\": \"blue\",\n" + " \"name\": \"Wyatt Jefferson\",\n" + " \"gender\": \"male\",\n" + " \"company\": \"GEEKFARM\",\n" + " \"email\": \"wyattjefferson@geekfarm.com\",\n" + " \"phone\": \"+1 (855) 405-2375\",\n" + " \"address\": \"506 Court Street, Gambrills, Minnesota, 8953\",\n" + " \"about\": \"Ea sunt quis non occaecat aliquip sint eiusmod. Aliquip id non ut sunt est laboris proident reprehenderit incididunt velit. Quis deserunt dolore aliqua voluptate magna laborum minim. Pariatur voluptate ad consequat culpa sit veniam eiusmod et ex ipsum.\\r\\n\",\n" + " \"registered\": \"2015-08-08T03:21:53 -02:00\",\n" + " \"latitude\": -39.880621,\n" + " \"longitude\": 44.053688,\n" + " \"tags\": [\n" + " \"non\",\n" + " \"cupidatat\",\n" + " \"in\",\n" + " \"Lorem\",\n" + " \"tempor\",\n" + " \"fugiat\",\n" + " \"aliqua\"\n" + " ],\n" + " \"friends\": [\n" + " {\n" + " \"id\": 0,\n" + " \"name\": \"Gail Blevins\"\n" + " },\n" + " {\n" + " \"id\": 1,\n" + " \"name\": \"Tricia Francis\"\n" + " },\n" + " {\n" + " \"id\": 2,\n" + " \"name\": \"Letitia Winters\"\n" + " }\n" + " ],\n" + " \"greeting\": \"Hello, Wyatt Jefferson! You have 1 unread messages.\",\n" + " \"favoriteFruit\": \"strawberry\"\n" + " }"; String jsonStringSimple = "{\n" + " \"_id\": \"57b44e738d9af9fa2df13b27\",\n" + " \"index\": 0,\n" + " \"guid\": \"13af6838-08c8-4709-8dff-5ecb20bbaaa7\",\n" + " \"isActive\": false,\n" + " \"balance\": \"$2,092.08\",\n" + " \"picture\": \"http://placehold.it/32x32\",\n" + " \"age\": 22,\n" + " \"eyeColor\": \"blue\",\n" + " \"name\": \"Wyatt Jefferson\",\n" + " \"gender\": \"male\",\n" + " \"company\": \"GEEKFARM\",\n" + " \"email\": \"wyattjefferson@geekfarm.com\",\n" + " \"phone\": \"+1 (855) 405-2375\",\n" + " \"address\": \"506 Court Street, Gambrills, Minnesota, 8953\",\n" + " \"about\": \"Ea sunt quis non occaecat aliquip sint eiusmod. Aliquip id non ut sunt est laboris proident reprehenderit incididunt velit. Quis deserunt dolore aliqua voluptate magna laborum minim. Pariatur voluptate ad consequat culpa sit veniam eiusmod et ex ipsum.\\r\\n\",\n" + " \"registered\": \"2015-08-08T03:21:53 -02:00\",\n" + " \"latitude\": -39.880621,\n" + " \"longitude\": 44.053688,\n" + " }"; JSONObject randomObj = new JSONObject(jsonString); JSONObject tmp = new JSONObject(jsonStringSimple); Map<String, byte[]> randomObj2 = new HashMap<String, byte[]>(); for (String key : tmp.keySet()) { Object value = tmp.get(key); randomObj2.put(key, value.toString().getBytes()); } addSignature(randomObj, keyPair.getPrivate()); addSignature(randomObj2, keyPair.getPrivate()); if (hasSignature(randomObj)) System.out.println("Verify 1: " + verify(randomObj, keyPair.getPublic())); if (hasSignature(randomObj2)) System.out.println("Verify 2: " + verify(randomObj, keyPair.getPublic())); removeSignature(randomObj); removeSignature(randomObj2); }
From source file:Main.java
public static KeyPair generateRSAKeyPair(int keyLength) { try {//from w w w. j a v a 2 s. com KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA); kpg.initialize(keyLength); return kpg.genKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static KeyPair genKeyPair() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, InvalidKeyException, SignatureException { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGen.initialize(1024, new SecureRandom()); KeyPair keypair = keyPairGen.genKeyPair(); return keypair; }
From source file:love.sola.netsupport.util.RSAUtil.java
public static void genKeyPair() { try {/*w ww. j a v a 2s . c o m*/ KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.getPrivate(); publicKey_s = Base64.encodeBase64String(publicKey.getEncoded()); privateKey_s = Base64.encodeBase64String(privateKey.getEncoded()); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.sf.zekr.common.util.CryptoUtils.java
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { // Generate a 1024-bit Digital Signature Algorithm (DSA) key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);//from w w w . j a v a2 s .co m KeyPair keypair = keyGen.genKeyPair(); return keypair; }
From source file:models.logic.CipherDecipher.java
public static KeyPair publicPrivateKeyGenerator() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { //Generation//from w w w . ja v a2 s .c o m KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048); return kpg.genKeyPair(); }
From source file:com.leclercb.commons.api.license.LicenseManager.java
public static KeyPair generateKeys(String algorithm, int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(keySize);/* w ww . j a va2s. c o m*/ return keyGen.genKeyPair(); }