use of javax.crypto.KeyGenerator in project protools by SeanDragon.
the class ToolMAC_BCP method initHmacSHA224Key.
/**
* 初始化HmacSHA224密钥
*
* @return byte[] 密钥
*
* @throws Exception
*/
public static byte[] initHmacSHA224Key() throws NoSuchAlgorithmException {
// 加入BouncyCastleProvider支持
Security.addProvider(new BouncyCastleProvider());
// 初始化KeyGenerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224");
// 产生秘密密钥
SecretKey secretKey = keyGenerator.generateKey();
// 获得密钥
return secretKey.getEncoded();
}
use of javax.crypto.KeyGenerator in project rxlib by RockyLOMO.
the class AESUtil method genarateRandomKey.
public static byte[] genarateRandomKey() {
KeyGenerator keygen;
try {
keygen = KeyGenerator.getInstance(AES_ALGORITHM);
} catch (NoSuchAlgorithmException ex) {
throw SystemException.wrap(ex);
}
SecureRandom random = new SecureRandom();
keygen.init(random);
Key key = keygen.generateKey();
return key.getEncoded();
}
use of javax.crypto.KeyGenerator in project jeesuite-libs by vakinge.
the class AES method initKey.
/**
* 生成密钥
* @throws Exception
*/
public static byte[] initKey() throws Exception {
// 密钥生成器
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
// 初始化密钥生成器
// 默认128,获得无政策权限后可用192或256
keyGen.init(128);
// 生成密钥
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
use of javax.crypto.KeyGenerator in project Bytecoder by mirkosertic.
the class Pair method doGenSecretKey.
/**
* Creates a new secret key.
*/
private void doGenSecretKey(String alias, String keyAlgName, int keysize) throws Exception {
if (alias == null) {
alias = keyAlias;
}
if (keyStore.containsAlias(alias)) {
MessageFormat form = new MessageFormat(rb.getString("Secret.key.not.generated.alias.alias.already.exists"));
Object[] source = { alias };
throw new Exception(form.format(source));
}
// Use the keystore's default PBE algorithm for entry protection
boolean useDefaultPBEAlgorithm = true;
SecretKey secKey = null;
if (keyAlgName.toUpperCase(Locale.ENGLISH).startsWith("PBE")) {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
// User is prompted for PBE credential
secKey = factory.generateSecret(new PBEKeySpec(promptForCredential()));
// Check whether a specific PBE algorithm was specified
if (!"PBE".equalsIgnoreCase(keyAlgName)) {
useDefaultPBEAlgorithm = false;
}
if (verbose) {
MessageFormat form = new MessageFormat(rb.getString("Generated.keyAlgName.secret.key"));
Object[] source = { useDefaultPBEAlgorithm ? "PBE" : secKey.getAlgorithm() };
System.err.println(form.format(source));
}
} else {
KeyGenerator keygen = KeyGenerator.getInstance(keyAlgName);
if (keysize == -1) {
if ("DES".equalsIgnoreCase(keyAlgName)) {
keysize = 56;
} else if ("DESede".equalsIgnoreCase(keyAlgName)) {
keysize = 168;
} else {
throw new Exception(rb.getString("Please.provide.keysize.for.secret.key.generation"));
}
}
keygen.init(keysize);
secKey = keygen.generateKey();
if (verbose) {
MessageFormat form = new MessageFormat(rb.getString("Generated.keysize.bit.keyAlgName.secret.key"));
Object[] source = { keysize, secKey.getAlgorithm() };
System.err.println(form.format(source));
}
}
if (keyPass == null) {
keyPass = promptForKeyPass(alias, null, storePass);
}
if (useDefaultPBEAlgorithm) {
keyStore.setKeyEntry(alias, secKey, keyPass, null);
} else {
keyStore.setEntry(alias, new KeyStore.SecretKeyEntry(secKey), new KeyStore.PasswordProtection(keyPass, keyAlgName, null));
}
}
use of javax.crypto.KeyGenerator in project openmrs-core by openmrs.
the class Security method generateNewSecretKey.
/**
* generate a new secret key; should only be called once in order to not invalidate all
* encrypted data
*
* @return generated secret key byte array
* @since 1.9
*/
public static byte[] generateNewSecretKey() {
// Get the KeyGenerator
KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance(OpenmrsConstants.ENCRYPTION_KEY_SPEC);
} catch (NoSuchAlgorithmException e) {
throw new APIException("could.not.generate.cipher.key", null, e);
}
// 192 and 256 bits may not be available
kgen.init(128);
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
return skey.getEncoded();
}
Aggregations