Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import java.security.*;

public class Main {
    /**
     * Generates a new symmetric key of a given type.
     * @param type Type of key to generate (algorithm).
     * @return Generated new SecretKey.
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey generateSymmetricKey(String type) throws NoSuchAlgorithmException {
        int keySize = 192;
        /* SecureRandom seeded automatically */
        SecureRandom secureRandom = new SecureRandom();
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        //128, 192, 256
        /* This is the correct way to initialize the keygenerator */
        keyGenerator.init(keySize, secureRandom);
        SecretKey key = keyGenerator.generateKey();
        return key;
    }
}