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) 

Source Link

Document

Initializes this key generator for a certain keysize.

Usage

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

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

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 ww w . j  ava  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:pepperim.util.IMCrypt.java

/**
 * Generates a random string to be used as AES encryption key
 * @return random AES encryption key//  w  w w.j  a v  a  2 s .  c om
 */
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:com.lling.qiqu.utils.AesUtils.java

/**
 * Returns an AES nbit Base64 key//from  w  w w  . j ava  2 s. c  o  m
 *
 * @param keySize Size of the key
 * @return AES 128bit Base64 key
 */
public static String generateKey(int keySize) {
    String key = "";
    KeyGenerator kgen = null;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keySize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        key = new String(Base64.encodeBase64(raw));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return key;
}

From source file:configuration.Key.java

/**
 * Creates a random key with the given parameters.
 * /*from  w w w.  j  a  v a 2s.c o m*/
 * @param keySize
 *            the length of the key in bits.
 * @param version
 *            the version of the key.
 * @param algorithm
 *            the string representation of the cipher or HMAC algorithm
 *            used.
 * @return the random key with the specified parameters or <code>null</code>
 *         , if the key cannot be generated.
 */
public static Key randomKey(int keySize, int version, String algorithm) {
    if (keySize < 1) {
        throw new IllegalArgumentException("keySize has to be at least one!");
    }
    if (version < 1) {
        throw new IllegalArgumentException("version must be at least one!");
    }
    if (algorithm == null) {
        throw new NullPointerException("algorithm may not be null!");
    }

    SecretKey secretKey = null;

    try {
        KeyGenerator generator = KeyGenerator.getInstance(getSecretKeyAlgorithm(algorithm));
        generator.init(keySize);
        secretKey = generator.generateKey();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return (secretKey != null) ? new Key(secretKey, version, algorithm) : null;
}

From source file:org.paxml.util.CryptoUtils.java

public static String decrypt(byte[] data, String password) {
    SecretKey SecKey = getSecretKey(password);
    try {//www  .ja  va2 s .  c  o  m
        KeyGenerator KeyGen = KeyGenerator.getInstance(KEY_TYPE);
        KeyGen.init(KEY_LENGTH_BITS);

        Cipher cipher = Cipher.getInstance(KEY_TYPE);

        cipher.init(Cipher.DECRYPT_MODE, SecKey);
        byte[] clear = cipher.doFinal(data);
        return new String(clear, KEY_VALUE_ENCODING);
    } catch (Exception e) {
        throw new PaxmlRuntimeException(e);
    }
}

From source file:org.paxml.util.CryptoUtils.java

public static byte[] encrypt(String data, String password) {

    SecretKey SecKey = getSecretKey(password);
    try {//  ww w.ja  va 2 s . c o m
        KeyGenerator KeyGen = KeyGenerator.getInstance(KEY_TYPE);
        KeyGen.init(KEY_LENGTH_BITS);

        Cipher cipher = Cipher.getInstance(KEY_TYPE);

        byte[] clear = data.getBytes(KEY_VALUE_ENCODING);

        cipher.init(Cipher.ENCRYPT_MODE, SecKey);
        return cipher.doFinal(clear);
    } catch (Exception e) {
        throw new PaxmlRuntimeException(e);
    }

}

From source file:org.mozilla.android.sync.Cryptographer.java

public static KeyBundle generateKeys() {
    KeyGenerator keygen;
    try {/*from w  w w. java 2s  .c o m*/
        keygen = KeyGenerator.getInstance(KEY_ALGORITHM_SPEC);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    keygen.init(KEY_SIZE);
    byte[] encryptionKey = keygen.generateKey().getEncoded();
    byte[] hmacKey = keygen.generateKey().getEncoded();
    return new KeyBundle(encryptionKey, hmacKey);
}

From source file:org.mozilla.android.sync.crypto.Cryptographer.java

/**
 * Make 2 random 256 bit keys (encryption and HMAC).
 * @return a KeyBundle// ww  w.  ja v  a2s  . com
 * @throws CryptoException on error
 */
public static KeyBundle generateKeys() throws CryptoException {
    KeyGenerator keygen;
    try {
        keygen = KeyGenerator.getInstance(KEY_ALGORITHM_SPEC);
    } catch (NoSuchAlgorithmException e) {
        throw new CryptoException(e);
    }

    keygen.init(KEY_SIZE);
    byte[] encryptionKey = keygen.generateKey().getEncoded();
    byte[] hmacKey = keygen.generateKey().getEncoded();
    return new KeyBundle(encryptionKey, hmacKey);
}

From source file:com.ironchain.common.kits.DigestKit.java

/**
 * ?AES,?128,192,256?.//from w w  w.  j a va  2 s. co 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 new RuntimeException(e);
    }
}