Java Key Pair Create generateKey(String cipherAlgorithm)

Here you can find the source of generateKey(String cipherAlgorithm)

Description

Generates a random encryption key.

License

Open Source License

Parameter

Parameter Description
cipherAlgorithm the cipher algorithm the key will be used with. Will determine the key size

Return

the generated key

Declaration

public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException 

Method Source Code


//package com.java2s;
/*/* w w w .j a  v a 2  s.  c o  m*/
 * Copyright (C) 2007-2014 Crafter Software Corporation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Main {
    public static final SecureRandom secureRandom = new SecureRandom();

    /**
     * Generates a random encryption key.
     *
     * @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();
    }
}

Related

  1. generateKey(final String password, final byte salt[])
  2. generateKey(SecureRandom sr, int size)
  3. generateKey(String algom, int keylength)
  4. generateKey(String algorithm, int keySize)
  5. generateKey(String algorithm, int size)
  6. generateKey(String keyPhrase)
  7. generateKey(String password)
  8. generateKey(String PRIVATE_KEY_FILE, String PUBLIC_KEY_FILE)
  9. generateKey(String publicKeyFilename, String privateKeyFilename, String password)