Example usage for javax.crypto KeyGenerator init

List of usage examples for javax.crypto KeyGenerator init

Introduction

In this page you can find the example usage for javax.crypto KeyGenerator init.

Prototype

public final void init(int keysize) 

Source Link

Document

Initializes this key generator for a certain keysize.

Usage

From source file:org.apache.juddi.v3.client.cryptor.TripleDESCrytor.java

/**
 * generates a new key/* ww  w. j  a  v  a  2s.c o  m*/
 * @return a new key
 */
public static String GEN() {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
        kgen.init(168);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new String(Base64.encodeBase64(raw));
    } catch (Exception ex) {
        ex.printStackTrace();
        ;
    }
    return null;
}

From source file:com.miyue.util.Cryptos.java

/**
 * ?AES,?128,192,256?.//from ww w  . j  a  v a  2s  .com
 */
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 Exceptions.unchecked(e);
    }
}

From source file:com.miyue.util.Cryptos.java

/**
 * ?HMAC-SHA1,,160?(20)./*from w  ww . j  ava 2s . c o  m*/
 * HMAC-SHA1?, 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 Exceptions.unchecked(e);
    }
}

From source file:com.keybox.manage.util.KeyStoreUtil.java

/**
 * create new keystore/*from  www.j a  v a2 s .c  om*/
 */
private static void initializeKeyStore() {
    try {
        keyStore = KeyStore.getInstance("JCEKS");
        //create keystore
        keyStore.load(null, KEYSTORE_PASS);

        //set encryption key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        KeyStoreUtil.setSecret(KeyStoreUtil.ENCRYPTION_KEY_ALIAS, keyGenerator.generateKey().getEncoded());

        //write keystore
        FileOutputStream fos = new FileOutputStream(keyStoreFile);
        keyStore.store(fos, KEYSTORE_PASS);
        fos.close();
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
}

From source file:org.javaweb.utils.EncryptUtils.java

/**
 * ?AES /* w w w  .j  a v a2 s.c  o  m*/
 *
 * @param length
 * @return
 * @throws NoSuchAlgorithmException
 */
public static String getAESKey(int length) throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(length);
    SecretKey secretKey = keyGenerator.generateKey();

    return HexUtils.bytes2HexString(secretKey.getEncoded());
}

From source file:org.apache.juddi.webconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize/* w w w .jav  a 2  s. c  om*/
 * @return may return null if the key is not of a supported size by the
 * current jdk
 */
public static String GEN(int keysize) {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keysize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return Base64.encodeBase64String(raw);
    } catch (Exception ex) {
        log.fatal("error generating key", ex);
    }
    return null;
}

From source file:org.apache.juddi.adminconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize/*from  ww  w . ja v  a2 s. com*/
 * @return may return null if the key is not of a supported size by the
 * current jdk
 */
public static String GEN(int keysize) {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keysize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new String(Base64.encodeBase64(raw), Charset.defaultCharset());
    } catch (Exception ex) {
        log.fatal("error generating key", ex);
    }
    return null;
}

From source file:com.microsoft.azure.storage.util.KeyVaultUtility.java

/**
 * Creates a secret in Azure Key Vault and returns its ID.
 * /* ww w .jav a2 s  .c  om*/
 * @param secretName
 *            The name of the secret to create
 * @return The ID of the created secret
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws NoSuchAlgorithmException
 * @throws URISyntaxException
 * @throws MalformedURLException
 */
public static String SetUpKeyVaultSecret(String secretName) throws InterruptedException, ExecutionException,
        NoSuchAlgorithmException, URISyntaxException, MalformedURLException {
    KeyVaultClient cloudVault = GetKeyVaultClient();

    if (Utility.vaultURL == null || Utility.vaultURL.isEmpty()) {
        throw new IllegalArgumentException("No Keyvault URL specified.");
    }

    try {
        // Delete the secret if it exists.
        cloudVault.deleteSecretAsync(Utility.vaultURL, secretName).get();
    } catch (ExecutionException ex) {
        boolean keyNotFound = false;
        if (ex.getCause().getClass() == ServiceException.class) {
            ServiceException serviceException = (ServiceException) ex.getCause();
            if (serviceException.getHttpStatusCode() == 404) {
                keyNotFound = true;
            }
        }

        if (!keyNotFound) {
            System.out.println(
                    "Unable to access the specified vault. Please confirm the KVClientId, KVClientKey, and VaultUri are valid in the app.config file.");
            System.out.println(
                    "Also ensure that the client ID has previously been granted full permissions for Key Vault secrets using the Set-AzureKeyVaultAccessPolicy command with the -PermissionsToSecrets parameter.");
            System.out.println("Press any key to exit");
            Scanner input = new Scanner(System.in);
            input.nextLine();
            input.close();
            throw ex;
        }
    }

    // Create a 256bit symmetric key and convert it to Base64.
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256); // Note that we cannot use SymmetricKey.KeySize256,
                      // because this resolves to '0x20'.
    SecretKey wrapKey = keyGen.generateKey();

    // Store the Base64 of the key in the key vault. Note that the
    // content-type of the secret must
    // be application/octet-stream or the KeyVaultKeyResolver will not load
    // it as a key.
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/octet-stream");
    Secret cloudSecret = cloudVault.setSecretAsync(Utility.vaultURL, secretName,
            Base64.encodeBase64String(wrapKey.getEncoded()), "application/octet-stream", null, null).get();

    // Return the base identifier of the secret. This will be resolved to
    // the current version of the secret.
    return cloudSecret.getSecretIdentifier().getBaseIdentifier();
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java

private static byte[] generateAESKey() throws NoSuchAlgorithmException {
    KeyGenerator generator;
    try {/*from w  w w.  j  a  v  a  2  s  .c om*/
        generator = KeyGenerator.getInstance("AES");
        generator.init(128);
        return generator.generateKey().getEncoded();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.silverpeas.core.security.encryption.ContentEncryptionServiceTest.java

/**
 * Generates a key for an AES enciphering.
 * @return the key in hexadecimal.//  w ww . j av a2s.  c om
 * @throws Exception if the key cannot be generated.
 */
public static String generateAESKey() throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(256);
    SecretKey skey = keyGenerator.generateKey();
    return EncodingUtil.asHex(skey.getEncoded());
}