Skip to content
Related Articles

Related Articles

Implementation of Vernam Cipher or One Time Pad Algorithm

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 01 Jun, 2022
View Discussion
Improve Article
Save Article

One Time Pad algorithm is the improvement of the Vernam Cipher, proposed by An Army Signal Corp officer, Joseph Mauborgne. It is the only available algorithm that is unbreakable(completely secure). It is a method of encrypting alphabetic plain text. It is one of the Substitution techniques which converts plain text into ciphertext. In this mechanism, we assign a number to each character of the Plain-Text.

The two requirements for the One-Time pad are

  • The key should be randomly generated as long as the size of the message.
  • The key is to be used to encrypt and decrypt a single message, and then it is discarded.

So to encrypt every new message requires a new key of the same length as the new message in one-time pad.

The ciphertext generated by the One-Time pad is random, so it does not have any statistical relation with the plain text.

The assignment is as follows: 

ABCDEFGHIJ
0123456789
KLMNOPQRST
10111213141516171819
UVWXYZ
202122232425

The relation between the key and plain text: In this algorithm, the length of the key should be equal to that of plain text.

Examples:

Input: Message = HELLO, Key = MONEY Output: Cipher – TSYPM, Message – HELLO Explanation:     Part 1: Plain text to Ciphertext         Plain text — H E L L O → 7 4 11 11 14         Key — M O N E Y → 12 14 13 4 24         Plain text + key → 19 18 24 15 38 → 19 18 24 15 12 (= 38 – 26)         Cipher Text → T S Y P M     Part 2: Ciphertext to Message         Cipher Text — T S Y P M → 19 18 24 15 12         Key — M O N E Y→ 12 14 13 4 24         Cipher text – key → 7 4 11 11 -12 → 7 4 11 11 14         Message → H E L L O Input: Message = SAVE, Key = LIFE Output: Cipher – DIAI Message – SAVE

Security of One-Time Pad

  • If any way cryptanalyst finds these two keys using which two plaintext are produced but if the key was produced randomly, then the cryptanalyst cannot find which key is more likely than the other. In fact, for any plaintext as the size of ciphertext, a key exists that produces that plaintext.
  • So if a cryptanalyst tries the brute force attack(try using all possible keys), he would end up with many legitimate plaintexts, with no way of knowing which plaintext is legitimate. Therefore, the code is unbreakable.
  • The security of the one-time pad entirely depends on the randomness of the key. If the characters of the key are truly random, then the characters of ciphertext will be truly random. Thus, there are no patterns or regularities that a cryptanalyst can use to attack the ciphertext.

Advantages

  • One-Time Pad is the only algorithm that is truly unbreakable and can be used for low-bandwidth channels requiring very high security(ex. for military uses).

Disadvantages

  • There is the practical problem of making large quantities of random keys. Any heavily used system might require millions of random characters on a regular basis.
  • For every message to be sent, a key of equal length is needed by both sender and receiver. Thus, a mammoth key distribution problem exists.

Below is the implementation of the Vernam Cipher:

Java




// Java program Implementing One Time Pad Algorithm
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Method 1
    // Returning encrypted text
    public static String stringEncryption(String text,
                                          String key)
    {
 
        // Initializing cipherText
        String cipherText = "";
 
        // Initialize cipher array of key length
        // which stores the sum of corresponding no.'s
        // of plainText and key.
        int cipher[] = new int[key.length()];
 
        for (int i = 0; i < key.length(); i++) {
            cipher[i] = text.charAt(i) - 'A'
                        + key.charAt(i)
                        - 'A';
        }
 
        // If the sum is greater than 25
        // subtract 26 from it
        // and store that resulting value
        for (int i = 0; i < key.length(); i++) {
            if (cipher[i] > 25) {
                cipher[i] = cipher[i] - 26;
            }
        }
 
        // Converting the no.'s into integers
 
        // Convert these integers to corresponding
        // characters and add them up to cipherText
        for (int i = 0; i < key.length(); i++) {
            int x = cipher[i] + 'A';
            cipherText += (char)x;
        }
 
        // Returning the cipherText
        return cipherText;
    }
 
    // Method 2
    // Returning plain text
    public static String stringDecryption(String s,
                                          String key)
    {
        // Initializing plain text
        String plainText = "";
 
        // Initializing integer array of key length
        // which stores difference
        // of corresponding no.'s of
        // each character of cipherText and key
        int plain[] = new int[key.length()];
 
        // Running for loop for each character
        // subtracting and storing in the array
        for (int i = 0; i < key.length(); i++) {
            plain[i]
                = s.charAt(i) - 'A'
                  - (key.charAt(i) - 'A');
        }
 
        // If the difference is less than 0
        // add 26 and store it in the array.
        for (int i = 0; i < key.length(); i++) {
            if (plain[i] < 0) {
                plain[i] = plain[i] + 26;
            }
        }
 
        // Converting int to corresponding char
        // add them up to plainText
        for (int i = 0; i < key.length(); i++) {
            int x = plain[i] + 'A';
            plainText += (char)x;
        }
 
        // Returning plainText
        return plainText;
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring plain text
        String plainText = "Hello";
 
        // Declaring key
        String key = "MONEY";
 
        // Converting plain text to toUpperCase
        // function call to stringEncryption
        // with plainText and key as parameters
        String encryptedText = stringEncryption(
            plainText.toUpperCase(), key.toUpperCase());
 
        // Printing cipher Text
        System.out.println("Cipher Text - "
                           + encryptedText);
 
        // Calling above method to stringDecryption
        // with encryptedText and key as parameters
        System.out.println(
            "Message - "
            + stringDecryption(encryptedText,
                               key.toUpperCase()));
    }
}

Output:

Cipher Text - TSYPM
Message - HELLO

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!