List of usage examples for javax.crypto KeyGenerator init
public final void init(int keysize)
From source file:com.ironchain.common.kits.DigestKit.java
/** * ?HMAC-SHA1,,160?(20). HMAC-SHA1?,/* w ww .j ava 2 s. c o m*/ * RFC2401160?(20). */ public static byte[] generateHmacSha1Key() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1); keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:org.jsecurity.crypto.BlowfishCipher.java
/** * Generates a new {@link Key Key} of the specified size suitable for this Cipher * (based on the {@link #ALGORITHM ALGORITHM} using the JDK {@link KeyGenerator KeyGenerator}. * @param keyBitSize the bit size of the key to create * @return the created key suitable for use with this Cipher. *///from www . j a v a 2 s . co m public static Key generateNewKey(int keyBitSize) { KeyGenerator kg; try { kg = KeyGenerator.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException e) { String msg = "Unable to acquire " + ALGORITHM + " algorithm. This is required to function."; throw new IllegalStateException(msg, e); } kg.init(keyBitSize); return kg.generateKey(); }
From source file:org.panbox.core.crypto.CryptCore.java
public static SecretKey generateSymmetricKey() { KeyGenerator generator; try {//from w ww . j ava 2s .c om generator = KeyGenerator.getInstance(KeyConstants.SYMMETRIC_ALGORITHM, KeyConstants.PROV_BC); generator.init(KeyConstants.SYMMETRIC_KEY_SIZE); return generator.generateKey(); } catch (NoSuchAlgorithmException e) { logger.error("Error during symmetric key generation: " + e); } catch (NoSuchProviderException e) { logger.error("Error during symmetric key generation: " + e); } return null; }
From source file:org.apache.ws.security.processor.EncryptedKeyProcessor.java
/** * Generates a random secret key using the algorithm specified in the * first DataReference URI/*w w w .ja v a 2 s . c o m*/ * * @param dataRefURIs * @param doc * @param wsDocInfo * @return * @throws WSSecurityException */ private static byte[] getRandomKey(List<String> dataRefURIs, Document doc, WSDocInfo wsDocInfo) throws WSSecurityException { try { String alg = "AES"; int size = 16; if (!dataRefURIs.isEmpty()) { String uri = dataRefURIs.iterator().next(); Element ee = ReferenceListProcessor.findEncryptedDataElement(doc, uri); String algorithmURI = X509Util.getEncAlgo(ee); alg = JCEMapper.getJCEKeyAlgorithmFromURI(algorithmURI); size = WSSecurityUtil.getKeyLength(algorithmURI); } KeyGenerator kgen = KeyGenerator.getInstance(alg); kgen.init(size * 8); SecretKey k = kgen.generateKey(); return k.getEncoded(); } catch (Throwable ex) { // Fallback to just using AES to avoid attacks on EncryptedData algorithms try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey k = kgen.generateKey(); return k.getEncoded(); } catch (NoSuchAlgorithmException e) { throw new WSSecurityException(WSSecurityException.FAILED_CHECK, null, null, e); } } }
From source file:org.openTwoFactor.clientExt.edu.internet2.middleware.morphString.Crypto.java
/** * Generate a key.//from w w w . j a va2 s. com * @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:org.openmrs.util.Security.java
/** * generate a new secret key; should only be called once in order to not invalidate all * encrypted data//from w w w . j a v a 2 s . co 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:net.jmhertlein.core.crypto.Keys.java
/** * Generates a new AES key using the system's default SecureRandom * * @param bits/*from w ww. j a v a 2 s. c om*/ * * @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. * //from w ww .j av a2s.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:com.owncloud.android.utils.EncryptionUtils.java
public static byte[] generateKey() { KeyGenerator keyGenerator; try {//from ww w. j av a2 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. * // ww w . j a v a2 s .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 { 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(); }