0

I want to excrypt a file in java very basically. Simply read line by line the file, and change the value of the chars to "char += key", where key is an integer. The problem is that if I use a key larger or equal with 2, it doesn't work anymore.

public void encryptData(int key) { System.out.println("Encrypt"); try { BufferedReader br = new BufferedReader(new FileReader("encrypted.data")); BufferedWriter out = new BufferedWriter(new FileWriter("temp_encrypted.data")); String str; while ((str = br.readLine()) != null) { char[] str_array = str.toCharArray(); // Encrypt one line for (int i = 0; i < str.length(); i++) { str_array[i] += key; } // Put the line in temp file str = String.valueOf(str_array); out.write(str_array); } br.close(); out.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } 

The decrypt function is the same but with the input/output files interchanged and instead of adding the key value, I subtract it.

I check char by char and indeed, the header gets messed up when i use a key value > 1. Any ideas? Is it because of maximum value of the char being exceeded?

2
  • Assume encrypted.data file in above example is the raw text to encrypt not encrypted data. Commented Dec 10, 2016 at 17:21
  • Yes forgot to mention this. This is because I first input data into encrypted.data, after that I encrypt it in a new file, and overwrite the new file over the old file, this is why it is called temp. Sorry for the confusion. Commented Dec 10, 2016 at 17:28

1 Answer 1

1

You're basically implementing a general-purpose Caesar cipher. Adding a number to a character could change a character to newline, etc which will not work if using a BufferedReader to read it back in.

Best to manipulate the text as a byte stream which would correctly encode and decode newline and any non-ASCII characters.

public void encryptData(int key) { System.out.println("Encrypt"); try { BufferedInputStream in = new BufferedInputStream(new FileInputStream("raw-text.data")); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("temp_encrypted.data")); int ch; while((ch = in.read()) != -1) { // NOTE: write(int) method casts int to byte out.write(ch + key); } out.close(); in.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } public void decryptData(int key) { System.out.println("Decrypt"); try { BufferedInputStream in = new BufferedInputStream(new FileInputStream("temp_encrypted.data")); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("decrypted.data")); int ch; while((ch = in.read()) != -1) { out.write(ch - key); } out.close(); in.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This was very helpful. Thank you! it seems to obvious now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.