generate AES Key SecretKey - Java Security

Java examples for Security:AES

Description

generate AES Key SecretKey

Demo Code


//package com.java2s;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

public class Main {
    public static void main(String[] argv) throws Exception {
        String pwd = "java2s.com";
        System.out.println(generateKey(pwd));
    }/*from w  w w  . j av  a  2s .  com*/

    private static final String KEY_ALGORITHM = "AES";

    private static SecretKey generateKey(String pwd)
            throws NoSuchAlgorithmException {
        KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
        kgen.init(128, new SecureRandom(pwd.getBytes()));
        SecretKey secretKey = kgen.generateKey();
        return secretKey;
    }
}

Related Tutorials