Java Key Pair Create generateKey(String algorithm, int size)

Here you can find the source of generateKey(String algorithm, int size)

Description

Generates a secret symmetric key

License

Open Source License

Parameter

Parameter Description
algorithm algorithm used for key generation, ie AES
size size of the generated key, must be one of 128, 192, 256. Use 128 when unsure, default configurations and providers should refuse to use longer keys.

Exception

Parameter Description
KeyException key generation or saving failed

Return

the generated key

Declaration

public static SecretKey generateKey(String algorithm, int size) throws KeyException 

Method Source Code


//package com.java2s;
/*// www  .ja  v a 2  s . c o m
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */

import java.security.KeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Main {
    /**
     * Generates a secret symmetric key
     * 
     * @param algorithm algorithm used for key generation, ie AES
     * @param size size of the generated key, must be one of 128, 192, 256. Use 128 when unsure,
     *             default configurations and providers should refuse to use longer keys.
     * @return the generated key
     * @throws KeyException key generation or saving failed
     */
    public static SecretKey generateKey(String algorithm, int size) throws KeyException {
        KeyGenerator keyGen = null;
        try {
            keyGen = KeyGenerator.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new KeyException("Cannot initialize key generator", e);
        }

        SecureRandom random = new SecureRandom();
        keyGen.init(size, random);

        return keyGen.generateKey();
    }
}

Related

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