//算法
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加密 自定义密钥生成
最新推荐文章于 2025-01-22 15:37:48 发布