我正在试验 AES 加密。但是,我收到了我从以下代码中编写的错误:
private static byte[] readKey(String request) throws KeyFileNotFoundException,
UnsupportedEncodingException, UnknownKeyException {
File keyFile = new File(Logging.getCurrentDir() + "\\cikey.key");
Properties keys = new Properties();
byte[] storage;
if (!keyFile.exists())
throw new KeyFileNotFoundException("Key file not located.");
if (keys.containsKey(request) == false)
throw new UnknownKeyException("Key not found."); //I RECIEVE THIS ERROR
storage = keys.getProperty(request).getBytes(); //read the STRING value and turn into a byte array
return storage;
}
这是方法调用的代码readKey()
。我在将通过该方法读入的字节数组复制到该方法时也遇到了问题。请阅读方法中的注释以获得更详细的解释。readKey()
decrypt()
public static String decrypt(String in) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException, IOException,
KeyFileNotFoundException, UnknownKeyException {
String out = " "; //decrypted String to return
byte[] key = readKey("key").clone(); //my attempt to copy a byte array
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
byte iv[] = readKey("iv"); //works here? same as above so I don't know.
IvParameterSpec ivspec = new IvParameterSpec(iv);
//initialize the cipher for decryption
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
// decrypt the message
byte[] decrypted = cipher.doFinal(in.getBytes());
out = asHex(decrypted);
return out;
}
有什么想法可以解决这个问题吗?