Here you can find the source of generateKey(String cipherAlgorithm)
Parameter | Description |
---|---|
cipherAlgorithm | the cipher algorithm the key will be used with. Will determine the key size |
public static SecretKey generateKey(String cipherAlgorithm) throws NoSuchAlgorithmException
//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(); } }