List of usage examples for javax.crypto KeyGenerator init
public final void init(int keysize, SecureRandom random)
From source file:Main.java
/** * Generates a new symmetric key of a given type. * @param type Type of key to generate (algorithm). * @return Generated new SecretKey./*from ww w . j a va 2s . co m*/ * @throws NoSuchAlgorithmException */ public static SecretKey generateSymmetricKey(String type) throws NoSuchAlgorithmException { int keySize = 192; /* SecureRandom seeded automatically */ SecureRandom secureRandom = new SecureRandom(); KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); //128, 192, 256 /* This is the correct way to initialize the keygenerator */ keyGenerator.init(keySize, secureRandom); SecretKey key = keyGenerator.generateKey(); return key; }
From source file:com.drisoftie.cwdroid.util.CredentialUtils.java
private static SecretKey generateKey() throws NoSuchAlgorithmException { // Generate a 256-bit key final int outputKeyLength = 256; SecureRandom secureRandom = new SecureRandom(); // Do *not* seed secureRandom! Automatically seeded from system entropy. KeyGenerator keyGenerator = KeyGenerator.getInstance(AES); keyGenerator.init(outputKeyLength, secureRandom); return keyGenerator.generateKey(); }
From source file:Main.java
private static byte[] getRawKey(byte[] paramArrayOfByte) throws Exception { KeyGenerator localKeyGenerator = KeyGenerator.getInstance("AES"); SecureRandom localSecureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto"); localSecureRandom.setSeed(paramArrayOfByte); localKeyGenerator.init(128, localSecureRandom); return localKeyGenerator.generateKey().getEncoded(); }
From source file:com.liferay.util.Encryptor.java
public static Key generateKey(String algorithm) throws EncryptorException { try {// w ww.j a v a 2s .c o m Security.addProvider(getProvider()); KeyGenerator generator = KeyGenerator.getInstance(algorithm); generator.init(56, new SecureRandom()); Key key = generator.generateKey(); return key; } catch (Exception e) { throw new EncryptorException(e); } }
From source file:com.healthcit.cacure.utils.PasswordService.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed);/* ww w . j a va2 s. c o m*/ kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); // SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);/* ww w . jav a2 s .c o m*/ kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java
public static String encryptAES(byte[] input, String key) throws Exception { byte[] crypted = null; javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(key.getBytes()); kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skey); crypted = cipher.doFinal(input);/*from w ww .jav a 2 s. co m*/ return new String(Hex.encode(crypted)); }
From source file:org.b3log.latke.util.Crypts.java
/** * Encrypts by AES./*from w w w. ja v a2 s . c o m*/ * * @param content the specified content to encrypt * @param key the specified key * @return encrypted content * @see #decryptByAES(java.lang.String, java.lang.String) */ public static String encryptByAES(final String content, final String key) { try { final KeyGenerator kgen = KeyGenerator.getInstance("AES"); final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(key.getBytes()); kgen.init(128, secureRandom); final SecretKey secretKey = kgen.generateKey(); final byte[] enCodeFormat = secretKey.getEncoded(); final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES"); final Cipher cipher = Cipher.getInstance("AES"); final byte[] byteContent = content.getBytes("UTF-8"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); final byte[] result = cipher.doFinal(byteContent); return Hex.encodeHexString(result); } catch (final Exception e) { LOGGER.log(Level.WARN, "Encrypt failed", e); return null; } }
From source file:org.b3log.latke.util.Crypts.java
/** * Decrypts by AES.// ww w . ja v a2s . c om * * @param content the specified content to decrypt * @param key the specified key * @return original content * @see #encryptByAES(java.lang.String, java.lang.String) */ public static String decryptByAES(final String content, final String key) { try { final byte[] data = Hex.decodeHex(content.toCharArray()); final KeyGenerator kgen = KeyGenerator.getInstance("AES"); final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(key.getBytes()); kgen.init(128, secureRandom); final SecretKey secretKey = kgen.generateKey(); final byte[] enCodeFormat = secretKey.getEncoded(); final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES"); final Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); final byte[] result = cipher.doFinal(data); return new String(result, "UTF-8"); } catch (final Exception e) { LOGGER.log(Level.WARN, "Decrypt failed"); return null; } }
From source file:net.theblackchamber.crypto.util.KeystoreUtils.java
/** * Method which will generate a random AES key and add it to a keystore with * the entry name provided.//from w w w . jav a2 s . c o m * * @param config * Configuration for generation of key. * @throws NoSuchAlgorithmException * @throws KeyStoreException * @throws CertificateException * @throws IOException */ public static void generateAESSecretKey(KeyConfig config) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { if (config == null || config.getKeyStoreFile() == null || StringUtils.isEmpty(config.getKeyEntryName()) || config.getAlgorithm() == null) { throw new KeyStoreException("Missing parameters, unable to create keystore."); } SecureRandom random = new SecureRandom(); KeyGenerator keygen = KeyGenerator.getInstance(config.getAlgorithm().toString(), new BouncyCastleProvider()); keygen.init(config.getKeySize(), random); SecretKey key = keygen.generateKey(); KeyStore keyStore = KeyStore.getInstance("JCEKS"); FileInputStream fis = null; if (config.getKeyStoreFile().exists() && FileUtils.sizeOf(config.getKeyStoreFile()) > 0) { fis = new FileInputStream(config.getKeyStoreFile()); } keyStore.load(fis, config.getKeyStorePassword().toCharArray()); KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection( config.getKeyStorePassword().toCharArray()); KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(key); keyStore.setEntry(config.getKeyEntryName(), secretKeyEntry, protectionParameter); if (fis != null) { fis.close(); } FileOutputStream fos = new FileOutputStream(config.getKeyStoreFile()); keyStore.store(fos, config.getKeyStorePassword().toCharArray()); fos.close(); }