DEV Community

Cover image for Tutorial One Time Pad (Vernam Cipher) Algorithm
Pedro Aravena for Vaultree

Posted on • Updated on

Tutorial One Time Pad (Vernam Cipher) Algorithm

Hello Vaultrarians!

Today we'll learn more about the One Time Pad algorithm is also known as Vernam Cipher. But, what is it?

The OTP is a method of encrypting alphabetic plain text. It is one of the Substitution techniques which converts plain text into cipher-text (decrypted word). In this idea, we assign a number to each character of the Plain-Text.

The assignment is as follows:

Image description

In this algorithm, the length of the keyword should be equal to that of plain text.

Examples:

Input: Message = VAULTREE, Keyword = AXDHCGQK
Output: Cipher – VXXSVXUO, Message – VAULTREE
Explanation:
Part 1: Plain text to Ciphertext
Plain text — V A U L T R E E → 21 0 20 11 19 17 4 4
Key — A X D H C G Q K → 0 23 3 7 2 6 16 10
Plain text + keyword = 21 23 23 18 21 23 20 14
Cipher Text → V X X S V X U O
Part 2: Ciphertext to Message
Cipher Text — V X X S V X U O → 21 23 23 18 21 23 20 14
Key — A X D H C G Q K → 0 23 3 7 2 6 16 10
Cipher text - key = 21 0 20 11 19 17 4 4 → Message → V A U L T R E E

Basically, the steps for encrypting are:

1- Assign a number to each character of the plain-text and the keyword according to alphabetical order.
2- Add ( + ) both the number (Corresponding plain-text character number and keyword character number).
3- Subtract ( - ) the number from 26 if the added number is greater than 26, if it isn’t then leave it.

Remember, the way to decrypt is applying the reverse process of encryption, which is CT - KW = PT. (cipher-text - keyword = plain-text).

Below is the implementation of the OTP algorithm:

`

public class OtpByVaultree {
// 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 = "VAULTREE";

    // Declaring key
    String key = "AXDHCGQK";

    // 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()));
}}
Enter fullscreen mode Exit fullscreen mode

`

With this algorithm you will be able to decrypt messages using the OTP method. Have fun!

-

At Vaultree we are building an encrypted future. We love sharing valuable information and trends to help you keep your data safe. Sign up to stay in the loop and discuss the hottest trends in cybersec with a team of experts.

Image description

Top comments (0)