0

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();
    }
}
Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
None None
  • 137
  • 1
  • 3
  • 11

3 Answers3

2

BufferedReader#readLine will return the line of text read from the reader. In you're example, you are ignoring the return value.

Instead, you should be doing something like...

String text = null;
while((text = bufferedReader.readLine()) != null) {
    // Process the text variable
}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • That was one of the first things I tried, but I can't figure out a way to make a file writer only replace that entire line that it is currently reading. For example, the FileWriter would either append and just add the encryption, of only encrypt the first line before the FileWriter completely deletes the rest of the file. That's why I was hoping there was a way to read the entire document at the same time and store the entire thing in a String. – None None Jun 22 '13 at 00:31
  • You need o write to a temporary file, once competed and the streams are closed, you would delete the old file and rename the new one in its place. It's very difficult to read and write fro a file simultaneously – MadProgrammer Jun 22 '13 at 00:34
1

I dont think that encrypting line by line is a good idea. I would do it this way

Cipher cipher = ...
Path path = Paths.get(file);
File tmp = File.createTempFile("tmp", "");
try (CipherOutputStream cout = new CipherOutputStream(new FileOutputStream(tmp), cipher)) {
    Files.copy(path, cout);
}
Files.move(tmp.toPath(), path, StandardCopyOption.REPLACE_EXISTING);

and read encrypted text like this

Scanner sc = new Scanner(new CipherInputStream(new FileInputStream(file), cipher));
while(sc.hasNextLine()) {
    ...
Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
0

Try the next:

public static void Encrypt() throws Exception {
    try {
        Path path = Paths.get(selectedFile.toURI());
        Charset charset = Charset.defaultCharset();

        // Read file
        List<String> lines = Files.readAllLines(path, charset);

        // Encrypt line
        lines.set(0, AESencrp.encrypt(lines.get(0)));

        // Write file
        Files.write(path, lines, charset);

    } catch (IOException e) {
        e.printStackTrace();
    }
}
Paul Vargas
  • 38,878
  • 15
  • 91
  • 139