-3

I have got an app to encrypting text files using XOR. Now I need to modify it to code binary files (such as jpegs) using another binary files (as a key). How can I do that? Does it has something in common with binary offset? I need it for learning purposes. Fragment of my code, responsible for text encryption:

ACTIONS CLASS:

import javax.swing.JFileChooser;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Actions extends GuiElements {

    final JFileChooser fc = new JFileChooser("D:");
    final JFileChooser fc1 = new JFileChooser("D:");

    public Actions() {
        btnLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Handle open button action.
                if (e.getSource() == btnLoad) {     
                    int returnVal = fc.showOpenDialog(Actions.this);
                    if (returnVal == JFileChooser.APPROVE_OPTION){
                        File file = fc.getSelectedFile();
                        String content = file.toString();
                        FileReader reader = null;
                        try{
                            reader = new FileReader(content);
                        } 
                        catch (FileNotFoundException e1){
                            e1.printStackTrace();
                        }
                        BufferedReader br = new BufferedReader(reader);
                        try{
                            textArea.read(br, null);
                        } 
                        catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        try{
                            br.close();
                        } 
                        catch (IOException e1) {
                            e1.printStackTrace();
                        }
                        textArea.requestFocus();
                    } 
                    else{  
                    }
               } 
            }
        });

        btnCipher.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent click) {
                String textToCipher = textArea.getText();
                String cipherKey = textField.getText();
                String cipheredText = "";
                int xor;
                char temp;
                for (int i=0; i<textToCipher.length(); i++){
                    xor = textToCipher.charAt(i) ^ cipherKey.charAt(i % cipherKey.length());
                    temp = (char)xor;
                    cipheredText += temp;
                }
                textArea.setText(cipheredText);
            }
        });

        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String textTosave = textArea.getText();
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new File("D:"));
                int retrival = chooser.showSaveDialog(null);
                if (retrival == JFileChooser.APPROVE_OPTION) {
                    try {
                        FileWriter fw = new FileWriter(chooser.getSelectedFile());
                        String cont = textArea.getText();
                        String content = cont.toString();
                        fw.write(content);
                        fw.flush();
                        fw.close();
                    } 
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }  
                }   
        };
    }); 
}
}
user3235376
  • 87
  • 1
  • 2
  • 10
  • When you say "encrypt", do you mean you're writing this for an assignment, or you're actually going to write your own encryption code and deploy it somewhere? – user2357112 Feb 14 '14 at 06:41
  • What has all that GUI code to do with the question? Can you strip it down to a minimal example? – Henry Feb 14 '14 at 08:06
  • What is "textArea"? It's never declared. – Hot Licks Feb 14 '14 at 17:18
  • Encrypting a binary file is basically no different from encrypting a text file. So long as your algorithm doesn't depend on the file being broken into lines or containing only legal UTF characters or whatever, it shouldn't care. – Hot Licks Feb 14 '14 at 17:19
  • (Encrypting a file by XORing with another file is a legitimate form of encryption -- it is, in essence, a "one time pad", the most secure of all encryption strategies. However, it depends on the second file being kept secret (just like any key) and not being "predictable" -- that one character not be statistically connected to the next. In general, no common file (text, mpeg, etc) meets that second requirement, but it can be achieved by applying a suitable cryptographic hash.) – Hot Licks Feb 14 '14 at 17:24

1 Answers1

0

You have two input binary files: the key file and the plaintext file. You have one output file, the cyphertext file that you write and which is also binary. The key file must be at least as long as the plaintext file.

repeat
  p <- read byte from plaintext binary file
  k <- read byte from key binary file
  c <- p XOR k
  write byte c to cyphertext binary file
until all bytes read from plaintext file

You say that your previous effort used text files. Be aware that Java uses different methods to read/write binary files and text files. Using a text method on a binary file, or vice versa, will give incorrect results.

rossum
  • 15,344
  • 1
  • 24
  • 38