我需要通過加密將字符串數組列表存儲在文件中。然後我解密文件內容並將其恢復到數組列表。但是,當我解密內容時,內容中有'Null'塊。沒有'空'塊,其餘的文本與我編碼的相同。加密並解密ArrayList <String>
public static void encryptFile(List<String> moduleList, File fileOut) {
try {
OutputStream out = new FileOutputStream(fileOut);
out = new CipherOutputStream(out, encryptCipher);
StringBuilder moduleSet = new StringBuilder();
for (String module : moduleList) {
moduleSet.append(module + "#");
}
out.write(moduleSet.toString().getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
} catch (java.io.IOException ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
public static List<String> decryptFile(File fileIn) {
List<String> moduleList = new ArrayList<String>();
byte[] buf = new byte[16];
try {
InputStream in = new FileInputStream(fileIn);
in = new CipherInputStream(in, decryptCipher);
int numRead = 0;
int counter = 0;
StringBuilder moduleSet = new StringBuilder();
while ((numRead = in.read(buf)) >= 0) {
counter++;
moduleSet.append(new String(buf));
}
String[] blocks = moduleSet.split("#");
System.out.println("Items: " + blocks.length);
} catch (java.io.IOException ex) {
System.out.println("Exception: " + ex.getMessage());
}
return moduleList;
}
我用UTF-16嘗試,因爲串在Java編碼UTF-16,但它只會讓輸出最差。 您的建議將不勝感激... 感謝
絕不拍照源代碼併發布它們。將源代碼複製+粘貼到您的問題中,或花時間輸入。 – Jeffrey
問題可能位於位置(242,137) – Alex
@Jeffrey如果可能,我會去複製/粘貼。如果因爲我在Kindle上或其他原因而無法實現這一點,我可能會延遲詢問一個問題,直到我回到桌面開發機器。當(重新)輸入代碼時,太多的錯誤會蔓延。 –