Here you can find the source of generateKey()
public static String generateKey() throws Exception
//package com.java2s; //License from project: Open Source License import java.security.SecureRandom; import java.util.Base64; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class Main { private static final int KEY_SIZE_IN_BITS = 128; public static String generateKey() throws Exception { SecureRandom random = SecureRandom.getInstanceStrong(); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(KEY_SIZE_IN_BITS, random); SecretKey key = keyGen.generateKey(); return new String(Base64.getEncoder().encode(key.getEncoded()), "UTF-8"); }/* w w w.jav a2 s . c o m*/ }