List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java
/** * RSA???/*from w w w . ja v a 2 s . c o m*/ * <dl> * <dt>? * <dd>RSA??????2048?????? * </dl> * @return RSA? */ public static KeyPair createKeyPair() { try { final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); final KeyPair pair = generator.generateKeyPair(); if (LOG.isDebugEnabled()) { final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic(); final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate(); LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray())); LOG.debug("public-exponent={}", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray())); LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray())); LOG.debug("private-exponent={}", Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray())); } return pair; } catch (final NoSuchAlgorithmException e) { throw new StandardRuntimeException(e); } }
From source file:com.aaasec.sigserv.cssigapp.KeyStoreFactory.java
/** * Generate a 2048 bit RSA KeyPair./* w w w .j av a 2 s .com*/ * * @param algorithm the algorithm to use * @param bits the length of the key (modulus) in bits * * @return the KeyPair * * @exception NoSuchAlgorithmException if no KeyPairGenerator is available * for the requested algorithm */ private static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPair kp = null; KeyPairGenerator generator; generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); kp = generator.generateKeyPair(); return kp; }
From source file:com.zxy.commons.codec.rsa.RSAUtils.java
/** * <p>//from www.ja v a 2s .c o m * ?(?) * </p> * * @return Map<String, Object> * @throws Exception Exception */ public static Map<String, Object> genKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
From source file:com.ligadata.EncryptUtils.EncryptionUtil.java
/** * Generate key which contains a pair of private and public key using 1024 * bytes. Store the set of keys in given files publicKeyFile,privateKeyFile * @param algorithm/*from w w w . ja v a2 s .co m*/ * : algorithm used * @param publicKeyFile * :The file containing public key * @param privateKeyFile * :The file containing private key */ public static void generateSampleKeys(String algorithm, String publicKeyFile, String privateKeyFile) { try { if (areKeysPresent(publicKeyFile, privateKeyFile)) { return; } final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(1024); final KeyPair key = keyGen.generateKeyPair(); File privateKeyFD = new File(privateKeyFile); File publicKeyFD = new File(publicKeyFile); // Create files to store public and private key if (privateKeyFD.getParentFile() != null) { privateKeyFD.getParentFile().mkdirs(); } privateKeyFD.createNewFile(); if (publicKeyFD.getParentFile() != null) { publicKeyFD.getParentFile().mkdirs(); } publicKeyFD.createNewFile(); // Saving the Public key in a file ObjectOutputStream publicKeyOS = new ObjectOutputStream(new FileOutputStream(publicKeyFD)); publicKeyOS.writeObject(key.getPublic()); publicKeyOS.close(); // Saving the Private key in a file ObjectOutputStream privateKeyOS = new ObjectOutputStream(new FileOutputStream(privateKeyFD)); privateKeyOS.writeObject(key.getPrivate()); privateKeyOS.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.codice.ddf.security.certificate.generator.PkiTools.java
/** * Generate new RSA public/private key pair with 2048 bit key * * @return new generated key pair/*from w w w . j a va2 s. com*/ * @throws CertificateGeneratorException */ public static KeyPair generateRsaKeyPair() { try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM, BouncyCastleProvider.PROVIDER_NAME); keyGen.initialize(RSA_KEY_LENGTH); return keyGen.generateKeyPair(); } catch (Exception e) { throw new CertificateGeneratorException("Failed to generate new public/private key pair.", e); } }
From source file:com.owncloud.android.utils.PushUtils.java
private static int generateRsa2048KeyPair() { String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator + KEYPAIR_FOLDER;//from ww w . j av a2 s . c o m String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION; String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION; File keyPathFile = new File(keyPath); if (!new File(privateKeyPath).exists() && !new File(publicKeyPath).exists()) { try { if (!keyPathFile.exists()) { keyPathFile.mkdir(); } KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair pair = keyGen.generateKeyPair(); int statusPrivate = saveKeyToFile(pair.getPrivate(), privateKeyPath); int statusPublic = saveKeyToFile(pair.getPublic(), publicKeyPath); if (statusPrivate == 0 && statusPublic == 0) { // all went well return 0; } else { return -2; } } catch (NoSuchAlgorithmException e) { Log_OC.d(TAG, "RSA algorithm not supported"); } } else { // we already have the key return -1; } // we failed to generate the key return -2; }
From source file:no.digipost.api.client.ApiServiceMock.java
static EncryptionKey createFakeEncryptionKey() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (Writer osWriter = new OutputStreamWriter(baos); PEMWriter writer = new PEMWriter(osWriter)) { KeyPairGenerator factory = KeyPairGenerator.getInstance("RSA"); factory.initialize(2048); KeyPair keyPair = factory.generateKeyPair(); writer.writeObject(keyPair.getPublic()); } catch (Exception e) { throw new RuntimeException("Failed creation of fake encryption key.", e); }/* w ww . ja va 2s .c o m*/ EncryptionKey fakeKey = new EncryptionKey(); fakeKey.setKeyId("fake-hash"); fakeKey.setValue(new String(baos.toByteArray())); return fakeKey; }
From source file:im.whistle.crypt.Crypt.java
/** * Generates a private/public key pair./*from w w w . j ava 2 s. c om*/ * @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:fi.okm.mpass.idp.authn.impl.ValidateOIDCIDTokenSignatureTest.java
protected static KeyPair generateKeyPair(final int keysize) throws NoSuchAlgorithmException { final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA"); keyGenerator.initialize(keysize); return keyGenerator.generateKeyPair(); }
From source file:cn.mrdear.pay.util.RSAUtils.java
/** * ?//from w w w .j a va 2s. c o m * * @param keySize * ? * @return */ public static KeyPair generateKeyPair(int keySize) { Assert.state(keySize > 0); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, PROVIDER); keyPairGenerator.initialize(keySize); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } }