Example usage for javax.crypto KeyGenerator generateKey

List of usage examples for javax.crypto KeyGenerator generateKey

Introduction

In this page you can find the example usage for javax.crypto KeyGenerator generateKey.

Prototype

public final SecretKey generateKey() 

Source Link

Document

Generates a secret key.

Usage

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);/*from w w  w. j a  v  a  2  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:com.keybox.manage.util.KeyStoreUtil.java

/**
 * create new keystore//  w ww  . j av a  2s .  c  o m
 */
private static void initializeKeyStore() {
    try {
        keyStore = KeyStore.getInstance("JCEKS");
        //create keystore
        keyStore.load(null, KEYSTORE_PASS);

        //set encryption key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        KeyStoreUtil.setSecret(KeyStoreUtil.ENCRYPTION_KEY_ALIAS, keyGenerator.generateKey().getEncoded());

        //write keystore
        FileOutputStream fos = new FileOutputStream(keyStoreFile);
        keyStore.store(fos, KEYSTORE_PASS);
        fos.close();
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
}

From source file:org.silverpeas.core.security.encryption.ContentEncryptionServiceTest.java

/**
 * Generates a key for an AES enciphering.
 * @return the key in hexadecimal.//from  ww w  . j ava 2 s  . co m
 * @throws Exception if the key cannot be generated.
 */
public static String generateAESKey() throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(256);
    SecretKey skey = keyGenerator.generateKey();
    return EncodingUtil.asHex(skey.getEncoded());
}

From source file:org.sakaiproject.linktool.LinkToolUtil.java

/**
 * Generate a random salt, and write it to a file
 * // w w w .  ja v  a 2s  .c o m
 * @param dirname
 *        writes to file saltname in this 
 *        directory. dirname assumed to end in /
 */

private static void gensalt(String dirname) {
    try {
        // Generate a key for the HMAC-SHA1 keyed-hashing algorithm
        KeyGenerator keyGen = KeyGenerator.getInstance("HmacSHA1");
        SecretKey key = keyGen.generateKey();
        writeKey(key, dirname + saltname);
    } catch (Exception e) {
        M_log.warn("Error generating salt", e);
    }
}

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);/*from w w  w . j a va  2  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:com.joyent.manta.client.crypto.SecretKeyUtils.java

/**
 * Generates a new symmetric key using the specified cipher.
 *
 * @param algorithm cipher to generate key for
 * @param bits number of bits of key//from  www  .  ja v  a 2 s  .  c o  m
 * @return new instance of key
 * @throws NoSuchAlgorithmException thrown when no cipher is available by the passed name
 */
public static SecretKey generate(final String algorithm, final int bits) throws NoSuchAlgorithmException {
    Validate.notNull(algorithm, "Cipher must not be null");
    Validate.isTrue(bits > 0, "Cipher bits must be greater than zero");

    KeyGenerator symKeyGenerator = KeyGenerator.getInstance(algorithm,
            ExternalSecurityProviderLoader.getPreferredProvider());
    symKeyGenerator.init(bits);
    return symKeyGenerator.generateKey();
}

From source file:com.bluepixel.security.manager.ServerClient_test.java

private static SecretKey getSecretKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);// w w w .  j  av  a2 s  .c  o m
    SecretKey key = keyGenerator.generateKey();
    return key;
}

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);//from   ww w.  j  a  v a2s  . c o m
    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);
    return new String(Hex.encode(crypted));
}

From source file:pepperim.util.IMCrypt.java

/**
 * Generates a random string to be used as AES encryption key
 * @return random AES encryption key//w w  w .ja  v a 2s.co m
 */
public static String AES_genKey() {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");

        kgen.init(128);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();

        return binToHex(raw);

    } catch (NoSuchAlgorithmException e) {
        Main.log(e.getMessage());
        return "";
    }
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kGen = KeyGenerator.getInstance("AES");
    SecureRandom sr;//  ww  w .  j  av a2s.c  o m
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kGen.init(128, sr);
    SecretKey sKey = kGen.generateKey();
    return sKey.getEncoded();
}