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: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);
    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);
    return keyGenerator.generateKey();
}

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Encryption /*from  www  .  j av a2s  .co  m*/
 * @param content content to be encrypted
 * @param encryptKey encryption key
 * @return 
 * @throws Exception 
 */
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

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

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Decryption/*  ww  w  .ja v a 2s . c  o m*/
 * 
 * @param encryptBytes byte[] to be decrypted
 * @param decryptKey decryption key 
 * @return 
 * @throws Exception 
 */
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);

    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);
    return new String(decryptBytes);
}

From source file:org.craftercms.commons.crypto.CryptoUtils.java

/**
 * Generates a random encryption key.//from   ww w .j  a  v a  2 s  .c  o m
 *
 * @param cipherAlgorithm the cipher algorithm the key will be used with. Will determine the key size
 * @return the generated key
 */
public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(cipherAlgorithm);
    keyGenerator.init(secureRandom);

    return keyGenerator.generateKey();
}

From source file:pl.kotcrab.crypto.CryptoUtils.java

/** Generates random AES key
 * @return random AES key *///from w w  w.j  ava2 s .  c  om
public static SecretKey getAESRandomKey() {
    try {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES", "BC");
        keyGen.init(256);
        return keyGen.generateKey();
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.atricore.idbus.kernel.main.authn.util.CipherUtil.java

/**
 * This generates a 128 AES key./* www .j  a v a 2s  .  c o  m*/
 *
 * @throws NoSuchAlgorithmException
 */
public static SecretKeySpec generateAESKey() throws NoSuchAlgorithmException {

    SecretKeySpec skeySpec;

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    SecretKey skey = kgen.generateKey();
    byte[] key = skey.getEncoded();

    skeySpec = new SecretKeySpec(key, "AES");

    return skeySpec;
}

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
 *///from  w w  w.  j a va2 s . co 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.shenit.commons.codec.CodecUtils.java

/**
 * ??/*from   www.j a v a 2  s.  co  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: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  w  w. ja v  a2  s .c  o m
    return (keyGenerator.generateKey());
}