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:cl.niclabs.tscrypto.common.messages.EncryptedData.java

private static SecretKeySpec generateAESKey() {
    SecretKeySpec skeySpec = null;
    try {/*from   www.j  a va  2  s  . c  om*/
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(KEYSIZE_AES);

        // Generate the secret key specs.
        SecretKey secretKey = keyGen.generateKey();

        skeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return skeySpec;
}

From source file:com.liferay.util.Encryptor.java

public static Key generateKey(String algorithm) throws EncryptorException {
    try {/* w w w . j a v  a  2s  . c o  m*/
        Security.addProvider(getProvider());

        KeyGenerator generator = KeyGenerator.getInstance(algorithm);
        generator.init(56, new SecureRandom());

        Key key = generator.generateKey();

        return key;
    } catch (Exception e) {
        throw new EncryptorException(e);
    }
}

From source file:org.apache.juddi.webconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize/* w ww .j  av a 2  s .c  o m*/
 * @return may return null if the key is not of a supported size by the
 * current jdk
 */
public static String GEN(int keysize) {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keysize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return Base64.encodeBase64String(raw);
    } catch (Exception ex) {
        log.fatal("error generating key", ex);
    }
    return null;
}

From source file:org.apache.juddi.adminconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize/*from  w  w  w  .  java  2 s .c  o  m*/
 * @return may return null if the key is not of a supported size by the
 * current jdk
 */
public static String GEN(int keysize) {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keysize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new String(Base64.encodeBase64(raw), Charset.defaultCharset());
    } catch (Exception ex) {
        log.fatal("error generating key", ex);
    }
    return null;
}

From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java

/**
 * //from   w w  w  .  j  a  v  a 2s.c o m
 * This method generates keys. This method is implementation specific and should not be present in any general purpose interface
 * extracted from this class.
 * 
 * @return
 * @throws Exception
 */
public static String generateEncodedKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();

    // Create the cipher
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init((Cipher.WRAP_MODE), desKey);

    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");
    DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class);
    byte[] rawDesKey = desSpec.getKey();

    return new String(Base64.encodeBase64(rawDesKey));
}

From source file:org.javaweb.utils.EncryptUtils.java

/**
 * ?AES /*from   w w  w.j  av  a 2s . c o  m*/
 *
 * @param length
 * @return
 * @throws NoSuchAlgorithmException
 */
public static String getAESKey(int length) throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(length);
    SecretKey secretKey = keyGenerator.generateKey();

    return HexUtils.bytes2HexString(secretKey.getEncoded());
}

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

/**
 * ?HMAC-SHA1,,160?(20)./* www  . ja va  2  s .  c o  m*/
 * HMAC-SHA1?, RFC2401160?(20).
 */
public static byte[] generateHmacSha1Key() {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1);
        keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:io.manasobi.utils.CryptoUtils.java

/**
 * ?   ?. ??  ?  ??  ?  ?.//from ww  w. j  a  va2  s .  c o  m
 *
 * @return ?? ??  Hex ?  
 */
public static String generateHexDESKey() {

    byte[] rawKey = null;

    try {

        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);

        SecretKey secretKey = keyGenerator.generateKey();

        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);

        DESedeKeySpec desEdeSpec = (DESedeKeySpec) secretKeyFactory.getKeySpec(secretKey,
                javax.crypto.spec.DESedeKeySpec.class);

        rawKey = desEdeSpec.getKey();

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return new String(Hex.encodeHex(rawKey));
}

From source file:com.credomatic.gprod.db2query2csv.Security.java

/**
 * Cifra una cadena de carateres utilizando el algoritmo AES y una llave (128, 256, o 512 bits). 
 * @param KeySize tamao de la llave autogenerada para relizar el cifrado
 * @param value cadena de caracteres que sera cifrada
 * @return instancia de tipo ${@link SecurityParams} con el resultado del proceso de cifrado
 *//* w ww .j  a  va2 s . c o m*/
public static SecurityParams encrypt(int KeySize, String value) {

    SecurityParams result = null;
    try {
        // Get the KeyGenerator
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(KeySize);

        // Generate the secret key specs.
        final SecretKey skey = kgen.generateKey();
        final byte[] raw = skey.getEncoded();
        final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        final Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        final String key = new Base64().encodeAsString(raw);
        final String encrypt = (new Base64()).encodeAsString(cipher.doFinal(value.getBytes()));

        result = new SecurityParams(encrypt, key, KeySize);

    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java

private static byte[] generateAESKey() throws NoSuchAlgorithmException {
    KeyGenerator generator;
    try {//www.  j  a  v a  2 s.c om
        generator = KeyGenerator.getInstance("AES");
        generator.init(128);
        return generator.generateKey().getEncoded();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}