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:
Let’s see the encryption method of the program here:
Fairly simple isn’t it? The whole program is this: Java XOR Encryptor/Decryptor (901) - 7.84 KB. 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){
//
}