Wednesday, June 8, 2011

Text File Encryptor

Here is the idea. Create a simple program that changes the bytes of a file using a passphrase in a such way that they can be revered to their initial state.
Of course this program is not suitable for commercial use because the encryption is very simple. But you get the idea.
The program is written in java and you can download the source code and the compiled program from there:

ByteReader source code
ByteReader byte code
ByteEncryptor source code
ByteEncryptor byte code
DataEncryptor source code
DataEncryptor byte code


The process is:
We have an object ByteReader that reads the contents of a text file using a BufferedReader which wraps a FileReader. It reads it line by line to avoid reading dumb null bytes (it happened when we used InputStreamReader, no idea why!
After that we convert each character of the line to an int! And then we store it in a variable such that b = object ByteReader and b.bytes[][] = thearray of ByteReader that keeps the bytes read.

Then we create another object, a ByteEncryptor which takes as one of its arguments the byte reader and according to another argument password and whether to subtract from the value of the bytes or add(i.e. 0 or 1) as another argument. It also takes the number of lines and the destination to store the encrypted data.

It encrypts each byte with the simplest calculation that exists!
for each byte
byte = byte + (passphrase.hashCode() % 91)
and decrypts
byte = byte - (passphrase.hashCode() % 91)

Some notes to have in mind!
1) Never use this program on important data! They might be corrupt! Use it only on test data that you don't mind to lose!
2) It fails to decrypt images, videos, pdf and similar formats. Do you suspect why?
3) Never use this program to encrypt data and feel safe! Can you think of a simple program that it can decrypt these data without knowing the password? If yes, let others know! (We know :) )

To run the program open a terminal the directory you saved the files and then run
java DataEncryptor yourFileYouWantToEncrypt destinationFile password 1/0

1 is for adding, ie encryption, 0 is for subtracting, ie decryption.
Do not run it the first time with the value 0, you may not be able to recover it, and also remember the notes above.
If you put less arguments you will get an ArrayIndexOutOfBoundsException, i.e. the program will crash.
You can make whatever changes you like on this program since it's just a simple silly one that aims to excite your imagination for building similar things, more sophisticated. So, it is not licensed and there is no copyright.

Enjoy!!!

No comments:

Post a Comment