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:org.mozilla.android.sync.Cryptographer.java

public static KeyBundle generateKeys() {
    KeyGenerator keygen;
    try {//from  ww  w .  jav a  2 s  . co  m
        keygen = KeyGenerator.getInstance(KEY_ALGORITHM_SPEC);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    keygen.init(KEY_SIZE);
    byte[] encryptionKey = keygen.generateKey().getEncoded();
    byte[] hmacKey = keygen.generateKey().getEncoded();
    return new KeyBundle(encryptionKey, hmacKey);
}

From source file:org.mozilla.android.sync.crypto.Cryptographer.java

/**
 * Make 2 random 256 bit keys (encryption and HMAC).
 * @return a KeyBundle//  w w w.j  av a2s .c  o m
 * @throws CryptoException on error
 */
public static KeyBundle generateKeys() throws CryptoException {
    KeyGenerator keygen;
    try {
        keygen = KeyGenerator.getInstance(KEY_ALGORITHM_SPEC);
    } catch (NoSuchAlgorithmException e) {
        throw new CryptoException(e);
    }

    keygen.init(KEY_SIZE);
    byte[] encryptionKey = keygen.generateKey().getEncoded();
    byte[] hmacKey = keygen.generateKey().getEncoded();
    return new KeyBundle(encryptionKey, hmacKey);
}

From source file:com.ironchain.common.kits.DigestKit.java

/**
 * ?AES,?128,192,256?.//from   w w w.  j a v a  2 s . co m
 */
public static byte[] generateAesKey(int keysize) {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
        keyGenerator.init(keysize);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jsecurity.crypto.BlowfishCipher.java

/**
 * Generates a new {@link Key Key} of the specified size suitable for this Cipher
 * (based on the {@link #ALGORITHM ALGORITHM} using the JDK {@link KeyGenerator KeyGenerator}.
 * @param keyBitSize the bit size of the key to create
 * @return the created key suitable for use with this Cipher.
 */// w w  w .j  av  a  2s . c om
public static Key generateNewKey(int keyBitSize) {
    KeyGenerator kg;
    try {
        kg = KeyGenerator.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        String msg = "Unable to acquire " + ALGORITHM + " algorithm.  This is required to function.";
        throw new IllegalStateException(msg, e);
    }
    kg.init(keyBitSize);
    return kg.generateKey();
}

From source file:com.zacwolf.commons.crypto.Crypter_AES.java

/**
 * @param keyfile/*from w w w .ja  v  a 2 s  . co m*/
 * @param keysize
 * @param salter
 * @throws InvalidKeySpecException
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter)
        throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
    if (keysize > Cipher.getMaxAllowedKeyLength(mytype))
        throw new InvalidKeySpecException(
                "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of "
                        + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype);

    final FileOutputStream fos = new FileOutputStream(keyfile);
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(mytype);
        kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available
        final SecretKey sk = kgen.generateKey();
        fos.write(sk.getEncoded());
    } finally {
        fos.flush();
        fos.close();
        ;
    }
}

From source file:com.ironchain.common.kits.DigestKit.java

/**
 * ?HMAC-SHA1,,160?(20). HMAC-SHA1?,//  w w  w  .  j a v  a 2s .co  m
 * 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 new RuntimeException(e);
    }
}

From source file:com.zacwolf.commons.crypto.Crypter_Blowfish.java

/**
 * /* w  w  w  .ja v  a 2 s.  co m*/
 * Create a new key with a custom keysize less-than equal to mykeysizemax,
 * specifying a custom salter
 * 
 * @param keyfile
 * @param keysize
 * @param salter
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws InvalidKeySpecException
 */
public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter)
        throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    if (keysize > Cipher.getMaxAllowedKeyLength(mytype))
        throw new InvalidKeySpecException(
                "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of "
                        + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype);

    final FileOutputStream fos = new FileOutputStream(keyfile);
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(mytype);
        kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available
        final SecretKey sk = kgen.generateKey();
        fos.write(sk.getEncoded());
    } finally {
        if (fos != null) {
            fos.flush();
            fos.close();
        }
    }
}

From source file:org.panbox.core.crypto.CryptCore.java

public static SecretKey generateSymmetricKey() {
    KeyGenerator generator;
    try {//from w  ww  .  j  a v a  2 s .c o  m
        generator = KeyGenerator.getInstance(KeyConstants.SYMMETRIC_ALGORITHM, KeyConstants.PROV_BC);
        generator.init(KeyConstants.SYMMETRIC_KEY_SIZE);
        return generator.generateKey();
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error during symmetric key generation: " + e);
    } catch (NoSuchProviderException e) {
        logger.error("Error during symmetric key generation: " + e);
    }
    return null;
}

From source file:org.apache.ws.security.processor.EncryptedKeyProcessor.java

/**
 * Generates a random secret key using the algorithm specified in the
 * first DataReference URI/* ww w.  ja va  2 s.  c o  m*/
 *
 * @param dataRefURIs
 * @param doc
 * @param wsDocInfo
 * @return
 * @throws WSSecurityException
 */
private static byte[] getRandomKey(List<String> dataRefURIs, Document doc, WSDocInfo wsDocInfo)
        throws WSSecurityException {
    try {
        String alg = "AES";
        int size = 16;
        if (!dataRefURIs.isEmpty()) {
            String uri = dataRefURIs.iterator().next();
            Element ee = ReferenceListProcessor.findEncryptedDataElement(doc, uri);
            String algorithmURI = X509Util.getEncAlgo(ee);
            alg = JCEMapper.getJCEKeyAlgorithmFromURI(algorithmURI);
            size = WSSecurityUtil.getKeyLength(algorithmURI);
        }
        KeyGenerator kgen = KeyGenerator.getInstance(alg);
        kgen.init(size * 8);
        SecretKey k = kgen.generateKey();
        return k.getEncoded();
    } catch (Throwable ex) {
        // Fallback to just using AES to avoid attacks on EncryptedData algorithms
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128);
            SecretKey k = kgen.generateKey();
            return k.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            throw new WSSecurityException(WSSecurityException.FAILED_CHECK, null, null, e);
        }
    }
}

From source file:com.glaf.core.security.SecurityUtils.java

/**
 * ?//from   w  w w . j av a 2s  .  c o  m
 * 
 * @param ctx
 *            
 * @return key
 */
public static Key generateSecretKey(SecurityContext ctx) {
    try {
        KeyGenerator skg = KeyGenerator.getInstance(ctx.getSymmetryKeyAlgorithm(), ctx.getJceProvider());
        SecureRandom secureRandom = SecureRandom.getInstance(ctx.getSecureRandomAlgorithm());
        skg.init(ctx.getSymmetryKeySize(), secureRandom);
        SecretKey key = skg.generateKey();
        return key;
    } catch (Exception ex) {
        throw new SecurityException(ex);
    }
}