//算法
private static String algorithm="AES";
//自定义密钥,由uuid生成的32位字符串
/**
* uuid是128位整数 以16进制展现
*/
private static String key = UUID.randomUUID().toString().replaceAll("-","");
/**
* 生成密钥
* @return
* @throws Exception
*/
public static Key getKey(String strKey) throws Exception{
//创建密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
//初始化密钥
keyGenerator.init(new SecureRandom(strKey.getBytes()));
//生成密钥
SecretKey getKey = keyGenerator.generateKey();
System.out.println("生成密钥:"+bytesToHexString(getKey.getEncoded ())+"----"+bytesToHexString(getKey.getEncoded ()).length());
return getKey;
}AES加密 自定义密钥生成
AES密钥生成与UUID应用
最新推荐文章于 2025-10-15 22:38:16 发布
本文介绍了一种使用UUID生成AES加密算法所需密钥的方法。通过UUID的唯一性确保了密钥的安全性和不可预测性,并详细展示了如何利用Java语言实现这一过程。
1527





