【发布时间】:2017-11-15 07:09:09
【问题描述】:
我在进行加密时遇到以下代码异常。 创建的密钥是“[B@29ee9faa”。 “加密时出错:java.security.InvalidKeyException:无效的 AES 密钥长度:11 个字节”
另外,我已经更新了 jre/lib/security 中的 local_policy 和 Us_export_policy。
public static String generateKey(String eisId)
{
String uuidKey = null;
try {
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(128); /* 128-bit AES */
SecretKey secret = gen.generateKey();
uuidKey = secret.getEncoded().toString();
System.out.println("uuidKey : "+uuidKey);
// Store in DB
// **********************
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return uuidKey;
}
public static SealedObject encryptData(String eisId, SecurityDomainDTO sDObj)
{
try
{
String secret = generateKey(eisId);
SecretKeySpec aesKey = new SecretKeySpec(secret.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
SealedObject so = new SealedObject(sDObj, cipher);
return so;
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
【问题讨论】:
-
toString()不能那样工作,无论如何将你的 byte[] 转换为 String 在这里毫无意义。
标签: java encryption