I am trying to pick up cryptography and had been trying this exercise
Write a program (preferably Java) to generate a one-time pad, which is a relatively large file of all random data (say 1 MB). The program should also be able to encrypt/decrypt files based on the generated one time pad.
Tip: use the following test vector to check if your program does encryption correctly.
Plaintext (ASCII): Every cloud has a silver lining
OTP (HEX): 6dc72fc595e35dcd38c05dca2a0d2dbd8e2df20b129b2cfa29ad17972922a2
ciphertext (HEX): 28b14ab7ecc33ea157b539ea426c5e9def0d81627eed498809c17ef9404cc5
I have tried to generate a one time pad using random number generator as I need to convert them to HEX form. and I am pretty sure I am confused or not tackling it the right way
public static void oneTimePad()
{
Random ran = new Random();
String s = "0123456789ABCDEF";
for(int i = 0; i < 100; i++)
{
System.out.print(s.charAt(ran.nextInt(s.length())));
}
}
Above would be my one time pad, and I was wondering how any idea how I could implement the encryption using the one time pad and decrypting it.
For the one time pad you need a byte array, not hexadecimals. The hexadecimals are only required for displaying data (we tend to have trouble reading bits). You can use the Apache Commons libraries (codec package) to create hexadecimals from byte arrays, or back if you want to decode the test vectors from hexadecimals to bytes.
You should use a secure random number generator, not
Random. So usenew SecureRandom()instead. To generate random data, first create a byte array, then callnextBytes()on the random number generator. There is not need to generate integers.