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

/** Generate a secret TripleDES encryption/decryption key */
public static SecretKey generateKey() throws NoSuchAlgorithmException {
    // Get a key generator for Triple DES (a.k.a DESede)
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    // Use it to generate a key
    return keygen.generateKey();
}

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  w  w  .  j  a v  a  2  s . c om*/
    kg.init(128, sr);
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static SecretKey generateSecretKey(String type, int size)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(type);
    keyGenerator.init(size);/*from  w  w w.  j  a v a  2  s. com*/
    return keyGenerator.generateKey();
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static Key generateKey(String type) throws NoSuchAlgorithmException {
    KeyGenerator keygen = KeyGenerator.getInstance(type);
    keygen.init(new SecureRandom());
    return keygen.generateKey();
}

From source file:org.apache.xml.security.samples.encryption.Encrypter.java

private static SecretKey GenerateDataEncryptionKey() throws Exception {

    String jceAlgorithmName = "AES";
    KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName);
    keyGenerator.init(128);//  ww w  . ja v  a  2s  .  c om
    return keyGenerator.generateKey();
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static SecretKey generateSecretKey(String type, int size, String provider)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(type, provider);
    keyGenerator.init(size);//from   ww w.jav  a  2 s  .c  o m
    return keyGenerator.generateKey();
}

From source file:Main.java

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:Main.java

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(decryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);

    return new String(decryptBytes);
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

private static void generateKey() {
    try {/*from  w  w w. j  a v a  2s  . com*/
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        final SecretKey skey = kgen.generateKey();
        System.out.println("Key: " + Hex.encodeHexString(skey.getEncoded()));
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Generates a new symmetric key of a given type.
 * @param type Type of key to generate (algorithm).
 * @return Generated new SecretKey.//  ww  w  . j a  va 2s. co  m
 * @throws NoSuchAlgorithmException
 */
public static SecretKey generateSymmetricKey(String type) throws NoSuchAlgorithmException {
    int keySize = 192;
    /* SecureRandom seeded automatically */
    SecureRandom secureRandom = new SecureRandom();
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    //128, 192, 256
    /* This is the correct way to initialize the keygenerator */
    keyGenerator.init(keySize, secureRandom);
    SecretKey key = keyGenerator.generateKey();
    return key;
}