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:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);/*from  w  ww .  ja  v  a  2s  . 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.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java

private static SecretKey createSecretKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);/*from w ww. j a  va  2 s  . c om*/
    return (keyGenerator.generateKey());
}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String getKey() {
    String key = null;/*ww w.java 2 s.c  o  m*/
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
        kgen.init(KEY_SIZE);
        // Generate the secret key specs.
        final SecretKey skey = kgen.generateKey();
        final byte[] raw = skey.getEncoded();
        key = new Base64().encodeAsString(raw);
    } catch (final NoSuchAlgorithmException e) {
        // should never happen
        LOG.error("System was unable to generate the key.", e);
    }
    return key;
}

From source file:com.hernandez.rey.crypto.TripleDES.java

/**
 * Generate a secret TripleDES encryption/decryption key
 * //from  w w w. j a  va2s  .c o  m
 * @return the secret key generated
 * @throws NoSuchAlgorithmException
 */
public static SecretKey generateKey() throws NoSuchAlgorithmException {
    // Get a key generator for Triple DES (a.k.a DESede)
    final KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    // Use it to generate a key
    return keygen.generateKey();
}

From source file:com.wabacus.util.DesEncryptTools.java

public static boolean createEncryptKey(File file) {
    SecretKey deskey = null;//from w  w  w  .  ja  va 2 s  . co  m
    try {
        KeyGenerator keygen = KeyGenerator.getInstance("DESede");
        deskey = keygen.generateKey();
    } catch (NoSuchAlgorithmException e) {
        throw new WabacusConfigLoadingException("?", e);
    }
    if (deskey == null)
        return false;
    FileOutputStream fos = null;
    ObjectOutputStream oos = null;
    try {
        fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(deskey);
    } catch (FileNotFoundException e) {
        throw new WabacusConfigLoadingException(
                "?" + file.getPath(), e);
    } catch (IOException e) {
        throw new WabacusConfigLoadingException(
                "?" + file.getPath() + "", e);
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        try {
            if (oos != null)
                oos.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}

From source file:org.apache.juddi.v3.client.cryptor.TripleDESCrytor.java

/**
 * generates a new key/*from w  ww . jav a  2 s. c o m*/
 * @return a new key
 */
public static String GEN() {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
        kgen.init(168);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new String(Base64.encodeBase64(raw));
    } catch (Exception ex) {
        ex.printStackTrace();
        ;
    }
    return null;
}

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.shenit.commons.codec.CodecUtils.java

/**
 * ??//from  w  w  w .  ja  va2s. c o  m
 * 
 * @return
 */
public static byte[] generateRandomKey(String codec) {
    SecureRandom sr = new SecureRandom();
    // DES?KeyGenerator
    KeyGenerator kg;
    try {
        kg = KeyGenerator.getInstance(codec);
        kg.init(sr);
        // ?
        Key secret = kg.generateKey();
        // ??
        return secret.getEncoded();
    } catch (NoSuchAlgorithmException e) {
        LOG.warn("[generateKey] not supported algorithm -> " + codec);
    }
    return null;
}

From source file:yoyo.framework.standard.shared.SecurityUtils.java

/**
 * ???/*from w ww .ja v a  2  s. c om*/
 * <ul>
 * <li>AES256bit?????</li>
 * <li>SHA1PRNG???????URL?BASE64?????</li>
 * </ul>
 * @return ?(URL?BASE64)
 */
public static String createKey() {
    try {
        final KeyGenerator generator = KeyGenerator.getInstance(ALGO_NAME);
        generator.init(KEY_LENGTH, SecureRandom.getInstance(RAND_NAME));
        return Base64.encodeBase64URLSafeString(generator.generateKey().getEncoded());
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.miyue.util.Cryptos.java

/**
 * ?AES,?128,192,256?.//from  ww w.j  av a2s  .c o  m
 */
public static byte[] generateAesKey(int keysize) {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
        keyGenerator.init(keysize);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}