List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:eap.util.EDcodeUtil.java
private static byte[] genHmacKey(String algorithm) { try {// www .ja va 2 s . c om KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm, provider); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("No such algorithm [" + algorithm + "]"); } }
From source file:net.jmhertlein.core.crypto.Keys.java
/** * Generates a new AES key using the system's default SecureRandom * * @param bits/*w ww. j a va 2s. c o m*/ * * @return the AES key, or null if the AES algorithm is not available on the system */ public static SecretKey newAESKey(int bits) { KeyGenerator keyGen; try { keyGen = KeyGenerator.getInstance("AES"); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex); return null; } keyGen.init(bits); return keyGen.generateKey(); }
From source file:org.opensaml.xml.security.XMLSecurityHelper.java
/** * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI. * /* w w w . ja v a 2s.co m*/ * @param algoURI The XML Encryption algorithm URI * @return a randomly-generated symmetric Key * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid * @throws KeyException thrown if the length of the key to generate could not be determined */ public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException { Logger log = getLogger(); String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI); if (StringSupport.isNullOrEmpty(jceAlgorithmName)) { log.error("Mapping from algorithm URI '" + algoURI + "' to key algorithm not available, key generation failed"); throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation"); } Integer keyLength = getKeyLengthFromURI(algoURI); if (keyLength == null) { log.error("Key length could not be determined from algorithm URI, can't generate key"); throw new KeyException("Key length not determinable from algorithm URI, could not generate new key"); } KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); keyGenerator.init(keyLength); return keyGenerator.generateKey(); }
From source file:org.openmrs.util.Security.java
/** * generate a new secret key; should only be called once in order to not invalidate all * encrypted data// ww w .j av a2 s . c o m * * @return generated secret key byte array * @since 1.9 */ public static byte[] generateNewSecretKey() { // Get the KeyGenerator KeyGenerator kgen = null; try { kgen = KeyGenerator.getInstance(OpenmrsConstants.ENCRYPTION_KEY_SPEC); } catch (NoSuchAlgorithmException e) { throw new APIException("could.not.generate.cipher.key", null, e); } kgen.init(128); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey skey = kgen.generateKey(); return skey.getEncoded(); }
From source file:org.openTwoFactor.clientExt.edu.internet2.middleware.morphString.Crypto.java
/** * Generate a key./* ww w . j a v a 2 s.c om*/ * @param cipherName the name of the cipher, if null will default to "AES" * @param keybits the number of bits in the key, if null will default to 128 * @return the bytes comprising the key */ public static byte[] generateKeyBytes(String cipherName, Integer keybits) { KeyGenerator keyGenerator = null; cipherName = cipherName == null ? "AES" : cipherName; try { keyGenerator = KeyGenerator.getInstance(cipherName); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Failed to create KeyGenerator for cipherName=" + cipherName, e); } keyGenerator.init(keybits == null ? 128 : keybits); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey secretKey = keyGenerator.generateKey(); byte[] keyBytes = secretKey.getEncoded(); return keyBytes; }
From source file:com.owncloud.android.utils.EncryptionUtils.java
public static byte[] generateKey() { KeyGenerator keyGenerator; try {//from www . j a v a 2 s. com keyGenerator = KeyGenerator.getInstance(AES); keyGenerator.init(128); return keyGenerator.generateKey().getEncoded(); } catch (NoSuchAlgorithmException e) { Log_OC.e(TAG, e.getMessage()); } return null; }
From source file:org.opensaml.xml.security.SecurityHelper.java
/** * Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI. * //w ww . ja v a 2 s.c om * @param algoURI The XML Encryption algorithm URI * @return a randomly-generated symmetric Key * @throws NoSuchAlgorithmException thrown if the specified algorithm is invalid * @throws KeyException thrown if the length of the key to generate could not be determined */ public static SecretKey generateSymmetricKey(String algoURI) throws NoSuchAlgorithmException, KeyException { String jceAlgorithmName = getKeyAlgorithmFromURI(algoURI); if (DatatypeHelper.isEmpty(jceAlgorithmName)) { log.error("Mapping from algorithm URI '" + algoURI + "' to key algorithm not available, key generation failed"); throw new NoSuchAlgorithmException("Algorithm URI'" + algoURI + "' is invalid for key generation"); } Integer keyLength = getKeyLengthFromURI(algoURI); if (keyLength == null) { log.error("Key length could not be determined from algorithm URI, can't generate key"); throw new KeyException("Key length not determinable from algorithm URI, could not generate new key"); } KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); keyGenerator.init(keyLength); return keyGenerator.generateKey(); }
From source file:eap.util.EDcodeUtil.java
private static byte[] aes(byte[] data, byte[] key, int keyLen, int opMode) { try {//from w w w . ja v a2 s .c o m KeyGenerator kgen = KeyGenerator.getInstance("AES", provider); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // provider secureRandom.setSeed(key); kgen.init(keyLen, secureRandom); SecretKey secretKey = kgen.generateKey(); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); /* mode: ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128<br/> * padding: Nopadding/PKCS5Padding/ISO10126Padding */ Cipher cipher = Cipher.getInstance("AES", provider); // ECB/PKCS5Padding cipher.init(opMode, keySpec); return cipher.doFinal(data); } catch (Exception e) { throw new IllegalArgumentException(e.getMessage(), e); } // // we're using Bouncy Castle // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()) // // // create our key specification // val secretKeySpec = new SecretKeySpec(hexStringToByteArray(hexEncodedKey), "AES") // // // create an AES engine in CTR mode (no padding) // val aes = Cipher.getInstance("AES/CTR/NoPadding", BouncyCastleProvider.PROVIDER_NAME) // // // initialize the AES engine in encrypt mode with the key and IV // aes.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(hexStringToByteArray(hexEncodedIv))) // // // encrypt the message and return the encrypted byte array // aes.doFinal(hexStringToByteArray(hexEncodedMessage)) }
From source file:org.apache.myfaces.shared_ext202patch.util.StateUtils.java
private static byte[] findSecret(String secret, String algorithm) { byte[] bytes = null; if (secret == null) { try {/* w ww . j a v a 2 s .c om*/ KeyGenerator kg = KeyGenerator.getInstance(algorithm); bytes = kg.generateKey().getEncoded(); if (log.isLoggable(Level.FINE)) log.fine("generated random password of length " + bytes.length); } catch (NoSuchAlgorithmException e) { // Generate random password length 8, int length = 8; bytes = new byte[length]; new Random().nextBytes(bytes); if (log.isLoggable(Level.FINE)) log.fine("generated random password of length " + length); } } else { bytes = new Base64().decode(secret.getBytes()); } return bytes; }
From source file:org.apache.myfaces.shared_ext202patch.util.StateUtils.java
private static byte[] findMacSecret(String secret, String algorithm) { byte[] bytes = null; if (secret == null) { try {/*from w ww. j a va2s . co m*/ KeyGenerator kg = KeyGenerator.getInstance(algorithm); bytes = kg.generateKey().getEncoded(); if (log.isLoggable(Level.FINE)) log.fine("generated random mac password of length " + bytes.length); } catch (NoSuchAlgorithmException e) { // Generate random password length 8, int length = 8; bytes = new byte[length]; new Random().nextBytes(bytes); if (log.isLoggable(Level.FINE)) log.fine("generated random mac password of length " + length); } } else { bytes = new Base64().decode(secret.getBytes()); } return bytes; }