Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One time pad, encryption and decryption

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.

like image 606
user1792962 Avatar asked Nov 02 '12 00:11

user1792962


People also ask

What is an example of the one-time pad cipher?

A one-time pad is a random collection of letters, e.g. FXIPUF, which can be used to encrypt messages with complete security (i.e. it is impossible to recover the message without knowing the key).

How secure is a one-time pad?

A One Time Pad (OTP) is the only potentially unbreakable encryption method. Plain text encrypted using an OTP cannot be retrieved without the encrypting key. However, there are several key conditions that must be met by the user of a one time pad cipher, or the cipher can be compromised.

Is one-time pad better than AES?

So, AES is more secure than one-time-pad in this case. No, the one-time pad is perfectly secure, so it cannot be less secure than AES. What's tripping you up is you're not understanding a subtle detail of what perfect secrecy means.

What are two problems with the one-time pad?

Now what we've said is that the one time pad has these two key drawbacks first of all that the key is long as the message, and secondly that the one time pad encryption scheme is only secure, if a particular key is used to encrypt only a single plain text.


1 Answers

Here you have a full working example:

    // convert secret text to byte array
    final byte[] secret = "secret".getBytes()

    final byte[] encoded = new byte[secret.length];
    final byte[] decoded = new byte[secret.length];

    // Generate random key (has to be exchanged)
    final byte[] key = new byte[secret.length];
    new SecureRandom().nextBytes(key);

    // Encrypt
    for (int i = 0; i < secret.length; i++) {
        encoded[i] = (byte) (secret[i] ^ key[i]);
    }

    // Decrypt
    for (int i = 0; i < encoded.length; i++) {
        decoded[i] = (byte) (encoded[i] ^ key[i]);
    }

    assertTrue(Arrays.equals(secret, decoded));
like image 196
Literadix Avatar answered Oct 22 '22 12:10

Literadix