Generate AES random Key - Java Security

Java examples for Security:AES

Description

Generate AES random Key

Demo Code


//package com.java2s;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;

public class Main {
    public static void main(String[] argv) throws Exception {
        int size = 2;
        System.out.println(java.util.Arrays.toString(randomKey(size)));
    }/*from ww  w  .jav a 2 s  . c o  m*/

    public static byte[] randomKey(int size) {
        try {
            KeyGenerator gen = KeyGenerator.getInstance("AES");
            gen.init(size, new SecureRandom());
            return gen.generateKey().getEncoded();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials