List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:org.craftercms.commons.crypto.CipherUtils.java
/** * Generates a random encryption key.//from w ww . ja v a2 s .c o m * * @param cipherAlgorithm the cipher algorithm the key will be used with. Will determine the key size * @return the generated key */ public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(cipherAlgorithm); keyGenerator.init(CryptoUtils.secureRandom); return keyGenerator.generateKey(); }
From source file:pl.kotcrab.crypto.CryptoUtils.java
/** Generates random AES key * @return random AES key *///from w w w . j a v a2 s .co m public static SecretKey getAESRandomKey() { try { KeyGenerator keyGen = KeyGenerator.getInstance("AES", "BC"); keyGen.init(256); return keyGen.generateKey(); } catch (NoSuchAlgorithmException | NoSuchProviderException e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);/* w w w. j a v a 2 s .c o m*/ kgen.init(256, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:org.craftercms.commons.crypto.CryptoUtils.java
/** * Generates a random encryption key./* ww w. j av a 2 s. c om*/ * * @param cipherAlgorithm the cipher algorithm the key will be used with. Will determine the key size * @return the generated key */ public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance(cipherAlgorithm); keyGenerator.init(secureRandom); return keyGenerator.generateKey(); }
From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java
public static byte[] encrypt(String input, String key, byte[] iv) throws Exception { javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(INIT_VECTOR, 0, INIT_VECTOR.length)); return cipher.doFinal(input.getBytes()); }
From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java
public static String encrypt(String input, String key) throws Exception { byte[] crypted = null; javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(INIT_VECTOR, 0, INIT_VECTOR.length)); crypted = cipher.doFinal(input.getBytes()); return new String(Hex.encode(crypted)); }
From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java
public static String decrypt(String input, String key, byte[] iv) throws Exception { byte[] output = null; javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv, 0, iv.length)); output = cipher.doFinal(Hex.decode(input)); return new String(output); }
From source file:org.demo.show.providers.ThreeDes2.java
/** * //from w w w . j ava2 s. c o m * ? * * @return byte[] * */ public static byte[] initkey() throws Exception { // ? KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); // ?? kg.init(168); // ? SecretKey secretKey = kg.generateKey(); // ??? return secretKey.getEncoded(); }
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:org.atricore.idbus.kernel.main.authn.util.CipherUtil.java
/** * This generates a 128 AES key./* ww w. j av a2s .c o m*/ * * @throws NoSuchAlgorithmException */ public static SecretKeySpec generateAESKey() throws NoSuchAlgorithmException { SecretKeySpec skeySpec; KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey skey = kgen.generateKey(); byte[] key = skey.getEncoded(); skeySpec = new SecretKeySpec(key, "AES"); return skeySpec; }