List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulous = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec); KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1);/*w w w . j a v a 2s . c o m*/ System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + "for its public key"); // Use the KeyAgreement object of 1 to generate its shared key ka1.doPhase(publicKey2, true); SecretKey sharedKey1 = ka1.generateSecret("DES"); System.out.println("1 is using " + new String(sharedKey1.getEncoded()) + " as its DES session key"); // Use the KeyAgreement object of 2 to generate its shared key ka2.doPhase(publicKey1, true); SecretKey sharedKey2 = ka2.generateSecret("DES"); System.out.println("2 is using " + new String(sharedKey2.getEncoded()) + "as its DES session key"); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String password = "password"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.genKeyPair(); String publicKeyFilename = "public"; byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); FileOutputStream fos = new FileOutputStream(publicKeyFilename); fos.write(publicKeyBytes);/*from ww w.ja v a2 s. c om*/ 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:com.cliqset.magicsig.util.KeyGen.java
public static void main(String[] args) { try {/* w w w .j a v a 2s . 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); 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()); }//w ww . j av a 2s . c o m 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:MainClass.java
public static void createKey() throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman"); kpg.initialize(512); KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class); }
From source file:Main.java
public static KeyPair generateRSAKeyPair(int keyLength) { try {//from www . j ava2s. c o m KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA); kpg.initialize(keyLength); return kpg.genKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:com.java.demo.RsaDemo.java
public static void Init() { try {/* w w w. j a va 2 s . c om*/ 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:com.streamreduce.util.CAGenerator.java
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); return keyGen.generateKeyPair(); }
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 w w. ja va2 s . co 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); KeyPair kp = kpg.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman"); DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), DHPublicKeySpec.class); }