List of usage examples for java.security KeyPairGenerator generateKeyPair
public KeyPair generateKeyPair()
From source file:netinf.common.security.impl.CryptographyTest.java
@BeforeClass public static void classSetUp() throws Exception { final Properties properties = Utils.loadProperties(NETINFNODE_PROPERTIES); injector = Guice.createInjector(new LogModule(properties), new DatamodelImplModule(), new CommunicationModule(), new SecurityModule(), new AbstractModule() { @Override// w w w. j a v a 2s. c o m protected void configure() { bind(NetInfNodeConnection.class).annotatedWith(SecurityModule.Security.class) .to(RemoteNodeConnection.class).in(Singleton.class); Names.bindProperties(binder(), properties); } }); factory = injector.getInstance(DatamodelFactory.class); identityObject = factory.createIdentityObject(); Identifier id = factory.createIdentifier(); IdentifierLabel label = factory.createIdentifierLabel(); label.setLabelName(DefinedLabelName.UNIQUE_LABEL.getLabelName()); label.setLabelValue("Test-Identity"); id.addIdentifierLabel(label); identityObject.setIdentifier(id); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair pair = keyPairGenerator.generateKeyPair(); privateKey = pair.getPrivate(); publicKey = pair.getPublic(); String keyName = identityObject.getIdentifier().toString() + "?" + DefinedAttributeIdentification.PUBLIC_KEY.getURI(); publicKeys.put(keyName, publicKey); identityObject.setPublicMasterKey(pair.getPublic()); } catch (Exception e) { throw new NetInfUncheckedException("error creating keys"); } convenienceCommunicator = EasyMock.createMock(RemoteNodeConnection.class); convenienceCommunicator.setHostAndPort("localhost", 5000); EasyMock.expectLastCall().anyTimes(); convenienceCommunicator.setSerializeFormat(SerializeFormat.JAVA); EasyMock.expectLastCall().anyTimes(); EasyMock.expect(convenienceCommunicator.getIO((Identifier) EasyMock.anyObject())).andReturn(identityObject) .anyTimes(); EasyMock.replay(convenienceCommunicator); identityManager = EasyMock.createMock(IdentityManager.class); EasyMock.expect(identityManager.getPrivateKey((String) EasyMock.anyObject())).andReturn(privateKey) .anyTimes(); EasyMock.expect(identityManager.hasPrivateKey((String) EasyMock.anyObject())).andReturn(true).anyTimes(); EasyMock.expect(identityManager.getPrivateKey(((String) EasyMock.anyObject()), (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(privateKey).anyTimes(); EasyMock.expect(identityManager.hasPrivateKey(((String) EasyMock.anyObject()), (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(true).anyTimes(); EasyMock.replay(identityManager); crypto = new CryptographyImpl(identityManager, algorithm, factory, convenienceCommunicator); }
From source file:net.jmhertlein.core.crypto.Keys.java
/** * Generates a new RSA public/private key pair. * * The system's default SecureRandom is used * * @param bits the length of the keys// w w w. ja v a 2 s. c o m * * @return the new key pair, or null if the RSA algorithm is not supported */ public static KeyPair newRSAKeyPair(int bits) { KeyPairGenerator keyPairGen; try { keyPairGen = KeyPairGenerator.getInstance("RSA"); } catch (NoSuchAlgorithmException ex) { return null; } keyPairGen.initialize(bits); return keyPairGen.generateKeyPair(); }
From source file:test.integ.be.fedict.commons.eid.client.SSLTest.java
private static KeyPair generateKeyPair() throws Exception { final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); final SecureRandom random = new SecureRandom(); keyPairGenerator.initialize(new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4), random); final KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; }
From source file:Main.java
/** * Generates a public/private key pair that meets Thali's security requirements * @return// www . java 2 s .co m */ public static KeyPair GenerateThaliAcceptablePublicPrivateKeyPair() { KeyPairGenerator keyPairGenerator = null; try { keyPairGenerator = KeyPairGenerator.getInstance(KeyTypeIdentifier); // TODO: http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html talks about security // failures in Android caused by improperly initialized RNGs. It would appear that this issue doesn't // apply to the latest version of Android. But obviously this is something that has to be further investigated // to make sure we are doing this correctly. keyPairGenerator.initialize(KeySizeInBits, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(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//from w ww. j a v 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:Main.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static void generateNewKeyOld(Context context) { try {/* w ww. j a v a 2s. c o m*/ KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, KEY_PROVIDER); Calendar instance = Calendar.getInstance(); Date start = instance.getTime(); instance.add(Calendar.YEAR, 1); Date end = instance.getTime(); keyPairGenerator.initialize(new KeyPairGeneratorSpec.Builder(context).setAlias(KEY_ALIAS) .setSubject(new X500Principal("CN=" + KEY_ALIAS)).setSerialNumber(BigInteger.valueOf(20151021)) .setStartDate(start).setEndDate(end).build()); keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } }
From source file:com.owncloud.android.utils.EncryptionUtils.java
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(RSA); keyGen.initialize(2048, new SecureRandom()); return keyGen.generateKeyPair(); }
From source file:net.link.util.test.pkix.PkiTestUtils.java
public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); SecureRandom random = new SecureRandom(); if ("RSA".equals(keyPairGenerator.getAlgorithm())) keyPairGenerator.initialize(new RSAKeyGenParameterSpec(RSA_KEYSIZE, RSAKeyGenParameterSpec.F4), random); else if (keyPairGenerator instanceof DSAKeyPairGenerator) { DSAKeyPairGenerator dsaKeyPairGenerator = (DSAKeyPairGenerator) keyPairGenerator; dsaKeyPairGenerator.initialize(DSA_MODLEN, false, random); }//ww w . j a v a 2s.c o m return keyPairGenerator.generateKeyPair(); }
From source file:org.globus.gsi.util.CertificateUtil.java
/** * Generates a key pair of given algorithm and strength. * * @param algorithm the algorithm of the key pair. * @param bits the strength// w w w .ja v a 2s. c o m * @return <code>KeyPair</code> the generated key pair. * @exception GeneralSecurityException if something goes wrong. */ public static KeyPair generateKeyPair(String algorithm, int bits) throws GeneralSecurityException { KeyPairGenerator generator = null; if (provider == null) { generator = KeyPairGenerator.getInstance(algorithm); } else { generator = KeyPairGenerator.getInstance(algorithm, provider); } generator.initialize(bits); return generator.generateKeyPair(); }
From source file:cl.nic.dte.util.XMLUtil.java
public static AUTORIZACIONDocument generateAuthorization(AUTORIZACIONDocument template, PrivateKey pKey) throws NoSuchAlgorithmException, SignatureException, TransformerException, InvalidKeyException, IOException {/*from w w w .j a va2 s . com*/ // Generation of keys KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp = kpg.generateKeyPair(); CAFType caf = template.getAUTORIZACION().getCAF(); CAFType.DA.RSAPK rsapk = caf.getDA().addNewRSAPK(); rsapk.setM(((RSAPublicKey) kp.getPublic()).getModulus().toByteArray()); rsapk.setE(((RSAPublicKey) kp.getPublic()).getPublicExponent().toByteArray()); ResourceBundle labels = ResourceBundle.getBundle("cl.nic.dte.resources.VerifyResults"); Signature sig = null; if (pKey.getAlgorithm().equals("RSA")) { sig = Signature.getInstance("SHA1withRSA"); caf.addNewFRMA().setAlgoritmo("SHA1withRSA"); } else if (pKey.getAlgorithm().equals("DSA")) { sig = Signature.getInstance("SHA1withDSA"); caf.addNewFRMA().setAlgoritmo("SHA1withDSA"); } else { throw new NoSuchAlgorithmException( labels.getString("ALGORITHM_NOT_SUPPORTED").replaceAll("%1", pKey.getAlgorithm())); } template.getAUTORIZACION() .setRSASK("-----BEGIN RSA PRIVATE KEY-----\n" + new String(Base64.encodeBase64(kp.getPrivate().getEncoded(), true)) + "-----END RSA PRIVATE KEY-----\n"); template.getAUTORIZACION() .setRSAPUBK("-----BEGIN RSA PUBLIC KEY-----\n" + new String(Base64.encodeBase64(kp.getPublic().getEncoded(), true)) + "-----END RSA PUBLIC KEY-----\n"); sig.initSign(pKey); sig.update(XMLUtil.getCleaned(caf.getDA())); caf.getFRMA().setByteArrayValue(Base64.encodeBase64(sig.sign())); return template; }