JavaPad in a nutshell Chapter 2, Making keys


A key in encryption is what is needed to "lock" and "unlock" the file.
The key used by javaPad can be anything made up of bytes, but should be
a file with random bytes.

Making this file is very simple:

There are 2 programs wich follows this package that can be used to generate keys
the programs MakeKey.class and SMakeKey.class.
To create a key in textmode use this command "java MakeKey <keyFile> <length>"
This will generate <lengt> number of bytes in the file <keyFile>.
For example "java MakeKey /home/erlend/keys/friends/cindee.key 200000"
would create a key in the directory /home/erlend/friends/ named cindee.key with
a length of 200000 bytes.

Like in the example i suggest that you have a directory for keys, so that you
know wich key to use when encrypting files to different purposes.
I recoment that when sending encrypted files to other persons, have one key for
each person to increase security.

You can also encrypt the keyfile with a given passphrase.
"java PassPhrase keyfile" will ask you for a passphrase and encrypt
the file 'keyfile' with the string you inputed. Even if someone get's your
keyfile now they wont be able to use it without the passphrase.
NOTE:
I only recomend using PassPhrase on keys created with (S)MakeKey
or another completly random number source. This is because if there is many
of the same characters folowing eachother it's easy to crack the passphrase.
For a demonstration try creating a testfile with many spaces ' ' and passphrase
the file with a short passphrase. If you now view the file you can see the 
passphrase where the space's where before.
Try doing the same thing with a file created with MakeKey.

If you want to create keys from another java program this can be done by using one of the methods
in the class KeyGenerator.
When creating a new object of this class you can either use the standard random number algorithm
wich is SHA1PRNG from SUN. Or you can specify another algorithm by giving ("algorithm", "provider")
to the constructor. you create the object like this:

KeyGenerator keygen = new KeyGenerator();
KeyGenerator keygen2 = new KeyGenerator("someAlgorithm", "someProvider");

When this object is created you can call any of the three methods it offer.
To create a file consisting of random bytes you use the method toFile()
and give it either a File object or a String with the filename as first argument, and the length of
the key as seccond argument. you can call the method like this:

File keyFile = new File("/home/erlend/testfile");

keygen.toFile(keyFile, 40000); // generate 40000 bytes in /home/erlend/testfile
keygen.toFile("/home/erlend/testfile2", 5000); // give filename as String

You can also use the toArray function wich returns a int[] of specified length. Used like this:

int[] key = keygen.toArray(500); // toArray returns a int[] with random bytes


When using the abouve methods the program will jump to the key generation and return when the
key is completed, With larger keys this can take some time. Therefor there is a inner class
in KeyGenerator named ToFileThread. You can use it like this:

KeyGenerator outerClass = new KeyGenerator();
KeyGenerator.ToFileThread keyGenerator = null;

keyGenerator = outerClass. new ToFileThread();

keyGenerator.toFile(fileName, keyLength);
Thread generateKey = new Thread(keyGenerator);

generateKey.start();


The syntax for doing this in java is a bit messy (at least I think so, compared to other aspects of java)
Anyway this will create a outer class of type KeyGenerator wich is only used to create the inner class.
the KeyGenerator.ToFileThread object implements Runnable and can be used as a thread.
After this code there could be a loop wich checks the public field percentComplete of the
KeyGenerator.ToFileThread object. like this:

while(keyGenerator.percentComplete != 100) {
   System.out.print(percentComplete);

   try { Thread.currentThread().sleep(10); }
   catch(InterruptedException ie) { };
   System.out.print("\b\b\b");
}




There is also a graphical program SMakeKey, this program does the same as MakeKey
but has a Swing GUI, "java SMakeKey" to start it.

By now you may be wondering what length your keys should be.
The more characters you have in your key more secure the encrypted file will be.
If you are planning to encrypt small files smaller than 100KB, you can have a key that is larger
than the file being encrypted, this means 100% secure. But if you encrypt a file that
is larger than the key, each byte in the key will be used over again, this is not a big
security problem, the file will still be near impossible to crack.
But I cant claim it to be 100% secure encryption.

you can also use another program to fill a file with random bytes, be sure to use
"high quality random numbers".

The forth method is a little trick I will explain with a example:

Your have a friend Picard, wich needs you to send a very secret document to
him, but your mutual enemy Tomalak is listening to your network trafic.
You can not send picard the key, cause then Tomalak would also get it.
But you can call Picard (or even better have told him before, in person)
to use a file that is accessible to both of you to decrypt the message.
You then encrypt the secret document with a public accessible file like:
a act from Shakespear, a music file, a picture,
or the binary for sendmail8.9.3-20, the possibilites are endless.

-End of chapter 2
<- Last Next ->