Example usage for javax.crypto KeyGenerator init

List of usage examples for javax.crypto KeyGenerator init

Introduction

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

Prototype

public final void init(int keysize, SecureRandom random) 

Source Link

Document

Initializes this key generator for a certain keysize, using a user-provided source of randomness.

Usage

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);//from  w ww .  ja  va 2 s  . c  om
    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:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);//from  w  w w  . j a va 2s  .co  m
    kgen.init(128, sr);
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

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 av  a  2 s .com
    kgen.init(128, sr);
    SecretKey sKey = kgen.generateKey();
    byte[] raw = sKey.getEncoded();

    return raw;
}

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

/**
 * ???//from   w  w w  .  j a  va  2s.  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:Main.java

public static byte[] generateKey(String password) throws Exception {
    byte[] keyStart = password.getBytes("UTF-8");

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(keyStart);/*from  w w w.  j a  v  a  2 s.  com*/
    kgen.init(128, sr);
    SecretKey skey = kgen.generateKey();
    return skey.getEncoded();
}

From source file:Main.java

public static byte[] getRandomKey(byte[] seed) throws Exception {

    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); //random
    sr.setSeed(seed);//from  w ww .j a  v  a2 s. com
    kg.init(128, sr);
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}

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: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 .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;
}