Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Implement the One Time Pad Algorithm

  • Difficulty Level : Medium
  • Last Updated : 19 Jan, 2021

One Time Pad algorithm is also known as Vernam Cipher. It is a method of encrypting alphabetic plain text. It is one of the Transposition techniques which converts plain text into ciphertext. In this mechanism, we assign a number to each character of the Plain-Text.

Assignment is as follows: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:  plain text - HELLO
        Key - MONEY
Output: Cipher - TSYPM
        Message - HELLO
        
Input:  plain text - SAVE
        Key - LIFE  
Output: Cipher - DIAI        
        Message - SAVE

Explanation of the above:

Part 1 (plain text to cipher text)

   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 (cipher text 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 

 

Java




// Java program which implements
// one time pad algorithm
  
import java.io.*;
public class GFG {
  
    // function which returns encryptedText
    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;
            }
        }
  
        // convert 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;
    }
  
    // function which returns plainText
    public static String stringDecryption(String s,String key)
    {
        // initializing plainText
        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;
            }
        }
  
        // convert 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;
    }
  
    // main function
    public static void main(String[] args)
    {
  
        // declaration of plain text
        String plainText = "Hello";
  
        // declaration of 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);
  
        // function call 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 :