我正在使用静态方法在类中使用javax.crypto加密和解密消息。我有2个使用cipher和dcipher的静态方法,以完成他们应该做的事情,我需要初始化一些变量(也是静态的)。但是,当我尝试使用它时,我得到的InvalidKeyException与我提供给ecipher.init(…)的参数。我找不到原因。这是代码:
private static byte[] raw = {-31, 17, 7, -34, 59, -61, -60, -16,
26, 87, -35, 114, 0, -53, 99, -116,
-82, -122, 68, 47, -3, -17, -21, -82,
-50, 126, 119, -106, -119, -5, 109, 98};
private static SecretKeySpec skeySpec;
private static Cipher ecipher;
private static Cipher dcipher;
static {
try {
skeySpec = new SecretKeySpec(raw, "AES");
// Instantiate the cipher
ecipher = Cipher.getInstance("AES");
dcipher = Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
dcipher.init(Cipher.DECRYPT_MODE, skeySpec);
} catch (NoSuchAlgorithmException e) {
throw new UnhandledException("No existe el algoritmo deseado", e);
} catch (NoSuchPaddingException e) {
throw new UnhandledException("No existe el padding deseado", e);
} catch (InvalidKeyException e) {
throw new UnhandledException("Clave invalida", e);
}
}