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:org.apache.juddi.adminconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize//from w ww  .j  a  v  a2s.  co  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:yoyo.framework.standard.shared.SecurityUtils.java

/**
 * ???// w w  w  .  j  ava 2  s  .c o  m
 * <ul>
 * <li>AES256bit?????</li>
 * <li>SHA1PRNG???????URL?BASE64?????</li>
 * </ul>
 * @return ?(URL?BASE64)
 */
public static String createKey() {
    try {
        final KeyGenerator generator = KeyGenerator.getInstance(ALGO_NAME);
        generator.init(KEY_LENGTH, SecureRandom.getInstance(RAND_NAME));
        return Base64.encodeBase64URLSafeString(generator.generateKey().getEncoded());
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.alta189.deskbin.util.DesEncrypter.java

public static SecretKey getNewKey() {
    try {/*from   w  ww  .ja  v a 2s.  com*/
        return KeyGenerator.getInstance("DES").generateKey();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

@Test
public void testDES() throws Exception {
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*"
            .getBytes();/*www . j  a va  2s  .c o  m*/

    System.out.println(clearText.length);

    byte[] encryptedText = m_encrypter.doFinal(clearText);
    System.out.println(encryptedText.length);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText);
    System.out.println(decryptedText.length);

    System.out.println(new String(clearText));
    System.out.println(new String(encryptedText));
    System.out.println(new String(decryptedText));

}

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

/**
 * Generates a random encryption key./*from  ww w. j av  a 2s  . 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(CryptoUtils.secureRandom);

    return keyGenerator.generateKey();
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

private static SecretKey generateKey() throws NoSuchAlgorithmException {
    // Generate a 256-bit key
    final int outputKeyLength = 256;

    SecureRandom secureRandom = new SecureRandom();
    // Do *not* seed secureRandom! Automatically seeded from system entropy.
    KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
    keyGenerator.init(outputKeyLength, secureRandom);
    return keyGenerator.generateKey();
}

From source file:com.shenit.commons.codec.CodecUtils.java

/**
 * ??//from  w  w  w  .jav a2 s.  c  om
 * 
 * @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:enc_mods.aes.java

public void setKey() {
    try {/*from  ww w .  j a va  2  s  .  c  om*/
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(AES_Key_Size);
        SecretKey aeskey = kgen.generateKey();
        key = aeskey.getEncoded();
        secretkey = new SecretKeySpec(key, "AES");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

public SimpleAesCipher(String base64Key) {
    KeyGenerator kgen = null;/*from  w w  w . ja v  a 2s. com*/
    try {
        kgen = KeyGenerator.getInstance("AES");
    } catch (NoSuchAlgorithmException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    kgen.init(128); // 192 and 256 bits may not be available

    byte[] raw = Base64.decodeBase64(base64Key);

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

    try {
        cipher = Cipher.getInstance("AES");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java

public CryptographyServiceImplBlowfish()
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);//  ww  w. j  av a2 s.c  om
    key = keyGenerator.generateKey();
    cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
    cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}