I am currently writing an encryption program that encrypts text documents with 64Bit encryption. The way it works is that it takes a string, and encrypts the string. I am currently searching for a way to have the program store all contents of a file in a string, encrypt the string, and then overwrite the file with the encrypted string. However, using
while((bufferedReader.readLine()) != null) {
...
}
it only reads and encrypts the first line, and the rest of it remains untouched.
however, using:
List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
Charset.defaultCharset());
for (String line : lines) {
...
}
only the last line is encrypted. I honestly don't know what to do anymore, as I am kind of running out of ideas.
Here is my current code (which also only appends to the file, as I was trying something new.) :
public static void Encrypt() throws Exception {
try {
FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
System.out.println(AESencrp.encrypt(line));
bw.write(AESencrp.encrypt(line));
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}