我希望能够转换一个ArrayList<String>存储从 BufferedReader 读取的文件内容的文件,然后将内容转换为 byte[] 以允许使用 Java 的 Cipher 类对其进行加密。
我尝试过使用.getBytes(),但它不起作用,因为我认为我需要先转换 ArrayList,而且我在弄清楚如何做到这一点时遇到了麻烦。
代码:
// File variable
private static String file;
// From main()
file = args[2];
private static void sendData(SecretKey desedeKey, DataOutputStream dos) throws Exception {
ArrayList<String> fileString = new ArrayList<String>();
String line;
String userFile = file + ".txt";
BufferedReader in = new BufferedReader(new FileReader(userFile));
while ((line = in.readLine()) != null) {
fileString.add(line.getBytes()); //error here
}
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
byte[] output = cipher.doFinal(fileString.getBytes("UTF-8")); //error here
dos.writeInt(output.length);
dos.write(output);
System.out.println("Encrypted Data: " + Arrays.toString(output));
}
提前谢谢了!
MYYA
慕哥9229398
一只名叫tom的猫
相关分类