【发布时间】:2018-06-20 16:37:23
【问题描述】:
我想用AES 加密技术加密消息。
当我使用此代码时,我遇到了一些错误
java.security.InvalidKeyException: Illegal key size or default parameters
我的加密代码:
public class Encryption {
public static class MessageEncrypt {
public static class AES {
private final static String ALGO = "AES";
private String secretKey;
private String data;
public String encrypt(String secretKey, String data) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGO);
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, key);
return toHex(cipher.doFinal(data.getBytes()));
}
public String decrypt(String secretKey, String data) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGO);
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(toByte(data)));
}
private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
return result;
}
public static String toHex(byte[] stringBytes) {
StringBuffer result = new StringBuffer(2 * stringBytes.length);
for (int i = 0; i < stringBytes.length; i++) {
result.append(HEX.charAt((stringBytes[i] >> 4) & 0x0f)).append(HEX.charAt(stringBytes[i] & 0x0f));
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
}
}
static class DataEncrypt {
}
}
我的测试计划:
public class Testing {
public static void main(String[] args) throws Exception {
AES cryptoAES = new AES();
System.out.println(cryptoAES.encrypt("43234sfeff", "re"));
}
}
当我运行这个时,我得到了这个错误:
Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1021)
at javax.crypto.Cipher.implInit(Cipher.java:796)
at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
at javax.crypto.Cipher.init(Cipher.java:1229)
at javax.crypto.Cipher.init(Cipher.java:1166)
at com.detroit.Encryption$MessageEncrypt$AES.encrypt(Encryption.java:35)
at testing.Testing.main(Testing.java:10)
在 Android Studio 中工作:
But the Same code working in Android (Android Studio), but when i run the same code in netbeans i got such kind of errors.
【问题讨论】:
-
欺骗 stackoverflow.com/questions/46362489/… 和 stackoverflow.com/questions/3862800/… 和 stackoverflow.com/questions/33055995/… 。还使用密码(虽然你称它为一个,但它并不是真正的密钥),因为 salt 是不安全的,128 次迭代是不安全的,并且 ECB 在大多数情况下是不安全的——但这些都是离题的。
-
请不要使用 StringBuffer,因为它在 2004 年被 StringBuilder 取代。
标签: java android encryption aes