0

I'm trying to create a Stream cipher using Java in which I take plain text from the file encrypt it (Simple XOR with a random key with seed value) and the store it in a different file and then to decrypt, again take cipher text from the file and decrypt it (same XOR operation with key as encryption) and storing in a file.

But I'm facing problem when I tried to encrypt large string. Half of the string is being decrypted correctly but the other half remains in unreadable format.

                FileReader fileReader = new FileReader(file);

                // Always wrap FileReader in BufferedReader.
                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);

                FileWriter fileWriter =
                        new FileWriter(file2);

                    // Always wrap FileWriter in BufferedWriter.
                    BufferedWriter bufferedWriter =
                        new BufferedWriter(fileWriter);


                while((line = bufferedReader.readLine()) != null) {
                    sb = new StringBuffer (line);

                    int lenStr = line.length();
                    int lenKey = String.valueOf(random).length();

                    // For each character in our string, encrypt it...
                    for ( int i = 0, j = 0; i < lenStr; i++, j++ ) 
                    {
                       if ( j >= lenKey ) j = 0;  // Wrap 'round to beginning of key string.

                       //
                       // XOR the chars together. Must cast back to char to avoid compile error. 
                       //
                       String key = random + "";
                       bufferedWriter.write((char)(line.charAt(i) ^ key.charAt(j)));

                    }
                }   

                // Always close files.
                bufferedReader.close();  
                bufferedWriter.close();

1 Answers1

0

You are mixing binary and characters and strings without enough care. The XOR'ing of characters may well result in a character not representing a string. If two identical characters are XOR'ed together then the result will be a null character, usually interpreted as END of FILE.

Instead it is best to keep everything binary when XOR'ing. XOR is a binary operation, not an operation on characters. So you use byte arrays instead.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • So in my program how can I achieve that? – Jaymin Shethwala Feb 27 '16 at 14:13
  • PlainText: This is just to check whether the program is working properly or not. Cipher text: yYXF^JZBCYEZTQVS\ZYTA_RKD_UACZPEX^^CF^G\^WTGBBATG[N\B^BE Plain text after decryption: This is just to check whether thelrkerog#ntkovii`m#wurlevny.eq'irh. Anyone please help me to fix this. I also tried to convert it to binary then XOR and then again converting to char and store it in the file but the output remains same as of the above program. – Jaymin Shethwala Feb 27 '16 at 15:19
  • I'm sure you've solved it by now (adding an answer to this question would be nice). Have you looked into java's CipherOutputStream? – Rolf Oct 20 '16 at 11:46