Example usage for javax.crypto KeyGenerator getInstance

List of usage examples for javax.crypto KeyGenerator getInstance

Introduction

In this page you can find the example usage for javax.crypto KeyGenerator getInstance.

Prototype

public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyGenerator object that generates secret keys for the specified algorithm.

Usage

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 va 2s  . com
 */
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:models.logic.CipherDecipher.java

public static SecretKey generateKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenS = KeyGenerator.getInstance("AES");
    keyGenS.init(128);/*  w ww.  ja v a2 s . co  m*/
    SecretKey sKey = keyGenS.generateKey();

    return sKey;
}

From source file:edu.emory.cci.aiw.cvrg.eureka.common.entity.CipherEncryptionAlgorithm.java

public KeyGenerator getKeyGeneratorInstance() throws NoSuchAlgorithmException {
    if (this.keyAlgorithm != null) {
        return KeyGenerator.getInstance(this.keyAlgorithm);
    } else {/*from  w  w w  .  ja va2 s . c o m*/
        return null;
    }
}

From source file:com.thoughtworks.go.security.AESCipherProvider.java

private byte[] generateKey() throws NoSuchAlgorithmException {
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);/*from w  ww  .ja v  a  2  s  .  c o  m*/
    byte[] key = keygen.generateKey().getEncoded();
    return key;
}

From source file:com.fengduo.bee.commons.security.Digests.java

/**
 * ??/*from w  w w  .j  ava2s .  c  o  m*/
 * 
 * @param algorithm 
 * @return 
 * @throws RuntimeException  {@link java.security.NoSuchAlgorithmException} ?
 */
private static byte[] getHmacKey(String algorithm) {
    // ?KeyGenerator
    KeyGenerator keyGenerator = null;
    try {
        keyGenerator = KeyGenerator.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    // 
    SecretKey secretKey = keyGenerator.generateKey();
    // 
    return secretKey.getEncoded();
}

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

private String generateDESedeKey() throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    SecretKey desedeKey = keygen.generateKey();

    SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec desedeSpec = (DESedeKeySpec) desedeFactory.getKeySpec(desedeKey,
            javax.crypto.spec.DESedeKeySpec.class);
    byte[] rawDesedeKey = desedeSpec.getKey();
    return new String(Base64.encodeBase64(rawDesedeKey));
}

From source file:org.sakaiproject.linktool.LinkToolUtil.java

/**
 * Generate a secret key, and write it to a file
 * //from  w  w  w .  ja v  a2 s  .  c  o m
 * @param dirname
 *        writes to file privkeyname in this 
 *        directory. dirname assumed to end in /
 */

private static void genkey(String dirname) {
    try {
        /* Generate key. */
        M_log.info("Generating new key in " + dirname + privkeyname);
        SecretKey key = KeyGenerator.getInstance("Blowfish").generateKey();

        /* Write private key to file. */
        writeKey(key, dirname + privkeyname);
    } catch (Exception e) {
        M_log.debug("Error generating key", e);
    }

}

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

/** 
 * Aes Decryption/*  ww w.ja v a2  s.co 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:cl.niclabs.tscrypto.common.messages.EncryptedData.java

private static SecretKeySpec generateAESKey() {
    SecretKeySpec skeySpec = null;
    try {/*from ww w.ja v a 2  s .com*/
        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:net.thewaffleshop.nimbus.api.EncryptionAPI.java

/**
 * Generate a random AES {@link SecretKey}
 *
 * @return//from  w  w w .  ja va  2 s  .c  om
 */
public SecretKey createSecretKey() {
    try {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(256);
        SecretKey key = keyGen.generateKey();
        return key;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}