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

private static Key newDesInstance(String strKey) {
    Key key = null;/*from w  w w  .  j a v  a 2s. com*/
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(new SecureRandom(strKey.getBytes()));
        key = keyGenerator.generateKey();
        keyGenerator = null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return key;
}

From source file:Main.java

/**
 * Generate a symmetric DES3 key./*from  w  w  w.  j  a va2s  . c  o  m*/
 *
 * @return A DES3 key
 */
public static SecretKey generateSymmetricKey() throws NoSuchAlgorithmException {
    KeyGenerator symmetricKeyGenerator = KeyGenerator.getInstance(symmetricAlgorithm);
    return symmetricKeyGenerator.generateKey();
}

From source file:com.janrain.backplane.common.HmacHashUtils.java

private static SecretKey generateMacKey(String algorithm, int keySize) throws NoSuchAlgorithmException {
    KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
    keyGen.init(keySize);/*from  w  w  w.  j av  a2  s.com*/
    return keyGen.generateKey();
}

From source file:es.logongas.fpempresa.security.SecureKeyGenerator.java

public static String getSecureKey() {
    try {/*from ww  w .  j ava2  s. co m*/
        Base32 base32 = new Base32();
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(keyLength);
        SecretKey secretKey = keyGen.generateKey();
        byte[] encoded = secretKey.getEncoded();
        return base32.encodeAsString(encoded);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:birch.util.EncryptionKeyFileUtil.java

public static String createKey(String cipher, int keysize) throws InvalidKeyException,
        IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
    KeyGenerator keyGenerator;
    Key key;/* w w  w  .  j av a  2s .c o  m*/

    keyGenerator = KeyGenerator.getInstance(cipher);
    keyGenerator.init(keysize);
    key = keyGenerator.generateKey();

    return new String(Base64.encodeBase64(key.getEncoded()));
}

From source file:Main.java

/**
 * do not work/*from   ww  w.j  a  v  a 2s  .c o  m*/
 */
private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128, new SecureRandom(seed)); // 192 and 256 bits may not be available
    return keyGenerator.generateKey().getEncoded();
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);//from   www  .ja v  a2  s . c  om
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String decrypt1(String encryptedData) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);//from w  w w. j a va2  s.  c  o  m
    SecretKey key = keyGen.generateKey();
    Cipher c = Cipher.getInstance(ALGO);

    c.init(Cipher.DECRYPT_MODE, key);
    //        byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    //        byte[] decValue = c.doFinal(decordedValue);
    byte[] decValue = c.doFinal(encryptedData.getBytes("UTF8"));
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:de.openflorian.crypt.CipherKeyGenerator.java

/**
 * Generate Cipher Secret<br/>//ww w. j  a  v a 2  s .  c  om
 * <br/>
 * Secret is generated by Blowfish {@link KeyGenerator} and a system default
 * {@link SecureRandom} provider and Base64 encoded afterwards.
 * 
 * @return Base64 encoded {@link SecureRandom} generated encryption key
 * @throws GeneralSecurityException
 */
public static String generateKey() throws GeneralSecurityException {
    try {
        KeyGenerator gen = KeyGenerator.getInstance("Blowfish");
        gen.init(192, new SecureRandom());
        SecretKey key = gen.generateKey();

        return new String(new Base64().encode(key.getEncoded())).trim();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:com.github.woki.payments.adyen.action.CSEUtil.java

private static SecretKey aesKey(int keySize) throws NoSuchAlgorithmException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(keySize);//from   w  ww  .ja va  2s.  c  o  m
    return kgen.generateKey();
}