Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

A Java-implementation of the famous One-time pad algorithm to encrypt data.

License

Notifications You must be signed in to change notification settings

yannickkirschen/one-time-pad

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

One-time Pad

Lint commit message Release Drafter Unit tests

This is a Java-implementation of the famous One-time pad algorithm to encrypt data.

DISCLAIMER: This library can be used for serious encryption as it uses java.util.SecureRandom to generate a secret. However, I do not take any responsibility for possible issues due to bad encryption!

As Wikipedia says:

In cryptography, the one-time pad (OTP) is an encryption technique that cannot be cracked, but requires the use of a one-time pre-shared key the same size as, or longer than, the message being sent. In this technique, a plaintext is paired with a random secret key (also referred to as a one-time pad). Then, each bit or character of the plaintext is encrypted by combining it with the corresponding bit or character from the pad using modular addition.

The resulting ciphertext will be impossible to decrypt or break if the following four conditions are met:

  1. The key must be truly random.
  2. The key must be at least as long as the plaintext.
  3. The key must never be reused in whole or in part.
  4. The key must be kept completely secret.

Point 3 and 4 cannot be covered by this library, as they are your responsibility. This library will take care of point 1 and 2.

Usage

Deployment to Maven will follow ...

OneTimePad pad = new OneTimePad();

byte[] text = "hello, world".getBytes(StandardCharsets.UTF_8);

// Destroy this secret once the message has been delivered
// and tell the recipient to destroy it after they decoded
// the message.
byte[] secret = pad.generateSecret(text.length);

byte[] ciphertext = pad.encode(text, secret);
byte[] decrypted = pad.decode(ciphertext, secret);

Assertions.assertArrayEquals(text, decrypted);