Example of AES secret key generation algorithm implemented by java
- 2020-05-27 05:36:04
- OfStack
The example of this paper describes the AES secret key generation algorithm implemented by java. I will share it with you for your reference as follows:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Test {
public static void main(String[] args) {
getKey();
getKeyByPass();
}
/**
* Generate the secret key at random
*/
public static void getKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
// How many bits do I need to generate? I just need to change this 128, 192 or 256
SecretKey sk = kg.generateKey();
byte[] b = sk.getEncoded();
String s = byteToHexString(b);
System.out.println(s);
System.out.println("106 The base key length is "+s.length());
System.out.println("2 The length of the base key is "+s.length()*4);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println(" There is no such algorithm. ");
}
}
/**
* Generates the secret key using the specified string
*/
public static void getKeyByPass() {
// To generate the secret key
String password="testkey";
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
// kg.init(128);// How many bits do I need to generate? I just need to change this 128, 192 or 256
//SecureRandom Is to generate a sequence of safe random Numbers, password.getBytes() Is the seed, as long as the seed is the same, the sequence 1 So the generated secret key is 1 The sample.
kg.init(128, new SecureRandom(password.getBytes()));
SecretKey sk = kg.generateKey();
byte[] b = sk.getEncoded();
String s = byteToHexString(b);
System.out.println(s);
System.out.println("106 The base key length is "+s.length());
System.out.println("2 The length of the base key is "+s.length()*4);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println(" There is no such algorithm. ");
}
}
/**
* byte Array to 16 Base string
* @param bytes
* @return
*/
public static String byteToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String strHex=Integer.toHexString(bytes[i]);
if(strHex.length() > 3) {
sb.append(strHex.substring(6));
} else {
if(strHex.length() < 2) {
sb.append("0" + strHex);
} else {
sb.append(strHex);
}
}
}
return sb.toString();
}
}
PS: about encryption and decryption interested friends can also refer to the website online tools:
Password security online detection:
http://tools.ofstack.com/password/my_password_safe
High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword
Thunderbolt, express, and whirlwind encryption/decryption tools:
http://tools.ofstack.com/password/urlrethunder
Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt
MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:
http://tools.ofstack.com/password/hash_md5_sha
Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
http://tools.ofstack.com/password/sha_encode
I hope this article is helpful to you java programming.