我在加密时遇到以下代码的异常。 创建密钥是“[B @ 29ee9faa”。 “加密时出错:java.security.InvalidKeyException:无效的AES密钥长度:11字节”加密时出错: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 []转换为字符串在这里没有意义。 –