Example usage for javax.crypto KeyGenerator generateKey

List of usage examples for javax.crypto KeyGenerator generateKey

Introduction

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

Prototype

public final SecretKey generateKey() 

Source Link

Document

Generates a secret key.

Usage

From source file:Main.java

public static byte[] initKey(int size) throws NoSuchAlgorithmException {
    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    kg.init(size);//from   w w w  .j a  v a2  s  .  co m
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}

From source file:Main.java

private static byte[] iv() {
    byte[] iv = null;
    try {/*from  ww  w. j  a  v  a 2 s .co m*/
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        SecretKey skey = kgen.generateKey();
        iv = skey.getEncoded();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return iv;
}

From source file:Main.java

public static byte[] initKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256); //192 256
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}

From source file:MainClass.java

public static SecretKey createKeyForAES(int bitLength, SecureRandom random)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");

    generator.init(128, random);/*w w  w .j  a v a2 s  . c  om*/

    return generator.generateKey();
}

From source file:Main.java

public static SecretKey generateAesKey() {
    try {/*  w  ww  . j ava  2 s .  c  o  m*/
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(KEY_LENGTH_AES);
        SecretKey key = kg.generateKey();

        return key;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:MainClass.java

static SecretKey createKeyForAES(int bitLength, SecureRandom random)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
    generator.init(256, random);/*from   w w  w  . j a  v  a 2  s  .c om*/
    return generator.generateKey();
}

From source file:com.java.demo.DesDemo.java

public static void Init() {
    try {// ww w .  ja va2 s . c o m
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        DESKeySpec dESKeySpec = new DESKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        convertSecretKey = factory.generateSecret(dESKeySpec);

    } catch (Exception e) {
    }
}

From source file:net.foreworld.util.RestUtil.java

public static String genApiKey() {
    try {//from w ww  .  ja v  a  2s. c  o  m
        KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
        SecretKey key = generator.generateKey();
        String encodedKey = Base64.encodeBase64URLSafeString(key.getEncoded());
        return encodedKey;
    } catch (NoSuchAlgorithmException ignore) {
    }
    return null;
}

From source file:Main.java

private static byte[] generateEncryptionSecret() {
    try {/*from   w w w . j  a v  a 2s.c  om*/
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);

        SecretKey key = generator.generateKey();
        return key.getEncoded();
    } catch (NoSuchAlgorithmException ex) {
        Log.w("keyutil", ex);
        return null;
    }
}

From source file:Main.java

@SuppressLint("TrulyRandom")
public static SecretKey generateHmacKey(String algorithm) {
    try {/*w  w  w .j a v  a 2 s. c  o  m*/
        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
        SecretKey key = keyGenerator.generateKey();
        return key;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}