A few months ago (see related posts) i released a small package for a XOR encryption in C. Now, here i am with an even smaller program for XOR encryption in Java. As expected, in Java it was way easier to implement it than C. No pointer loses, no “segmentation faults” no nothing. Nice and sweet. But let’s take things one at a time.
First of all what is XOR? It is a boolean operation. The truth table of XOR is as follows:
A | B | Res |
0 | 0 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
1 | 1 | 0 |
As you can see, the result is “1” only if A and B are different. One of the most important property of XOR operation is that (AxB)xB = A. This means that if we XOR something twice with something else we get this “something” back.
But how is this used on encryption? Well the idea is simple. We have a file we want to encrypt, let’s call it the input file. We also have a much smaller file that we use as a key file. We can use this key file to both encrypt and decrypt our input file since, if we xor it once (input XOR key = output) we get an encrypted file and if we do this twice (output XOR key = input) we get the input, plain text, file back.
One more plus is that XOR encryption is very hard to brake if we don’t have the key file. Now, it is easy and very simple. Let’s see how we can make a small program in java to do just that. Here is what the steps should be:
- The input file, key file and output file should be provided.
- The program should read the whole key file on the memory (so keep it small but not small enough!)
- Then it should start reading the input file int-int (4 bytes at a time) and encrypt on the fly. The encryption should be done with the “readByte mod keyLength” byte of the key.
- The product should be written out to the output file.
Let’s see the encryption method of the program here:
private void encode(String inputFile, String keyFile, String outputFile) throws Exception{
//there is a method called readKey that reads the whole key into this array
int[] key = readKey(keyFile);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputFile), 2048);
FileOutputStream out = new FileOutputStream(outputFile);
int read = -1;
int totalRead = 0;
long totalSize = (new File(inputFile)).length();
long curPercentage = -1;
long tmpPercentage = -1;
do{
//read int (4 bytes)
read = in.read();
//xor them with the key and write them out
out.write(read ^ key[totalRead % (key.length - 1)]);
totalRead++;
//calculate percentage that is done
tmpPercentage = ((100 * totalRead) / totalSize);
if(tmpPercentage % 5 == 0 && tmpPercentage != curPercentage){
curPercentage = tmpPercentage;
System.out.print("" + ((100 * totalRead) / totalSize) + "...");
}
}while(read != -1);
in.close();
out.flush();
out.close();
}
Fairly simple isn’t it? The whole program is this: [download#1#size]. You can also find it in the downloads page created to hold my downloads. I hope you find it usefull and please leaeve a comment if you use it someplace or if you have any problem/question.
I haven’t use Java for ages!
well it is an excellent programing language and i use it alot 😉
u just got a gr8 domain name
Where does the readKey()method come from? Is it a java method or one you created?
Cheers
//read int (4 bytes)
read = in.read(); //what happendes if read=-1?
//xor them with the key and write them out
out.write(read ^ key[totalRead % (key.length – 1)]);
use
read = in.read();
while(read != -1){
//
}