Encryption Algorithm

  • Assign a number to each character of the plain text and the key according to alphabetical order. 
  • Bitwise XOR both the number (Corresponding plain-text character number and Key character number). 
  • Subtract the number from 26 if the resulting number is greater than or equal to 26, if it isn’t then leave it.

Example 1:

Plain-Text: O A K
Key: S O N
O ==> 14 = 0 1 1 1 0
S ==> 18 = 1 0 0 1 0
Bitwise XOR Result: 1 1 1 0 0 = 28

Since the resulting number is greater than 26, subtract 26 from it.  Then convert the Cipher-Text character number to the Cipher-Text character.

28 - 26 = 2 ==> C
CIPHER-TEXT: C

Similarly, do the same for the other corresponding characters,

PT: O  A  K
NO: 14 00 10
KEY: S  O  N
NO:  18 14 13

New Cipher-Text is after getting the corresponding character from the resulting number. 

CT-NO: 02 14 07
CT:    C  O  H

Example 2: 

Plain-Text: RAMSWARUPK
Key: RANCHOBABA 

Now according to our encryption algorithm, we assign a number to each character of our plain text and key.

PT:   R  A  M   S   W   A  R   U   P   K
NO:   17 0  12  18  22  0  17  20  15  10
KEY:  R   A  N   C  H  O   B  A  B  A  
NO:   17  0  13  2  7  14  1  0  1  0 

Now Bitwise XOR the number of Plain-Text and Key and after doing the XOR operation and subtraction operation (if required), we will get the corresponding Cipher-Text character number. 

CT-NO: 0  0  1  16  17  14  16  20  14  10 

Since there are no numbers that are greater than or equal to 26 we do not have to subtract 26 from any of them.

New Cipher-Text is after getting the corresponding character from the number. 

CIPHER-TEXT: A  A  B  Q  R  O  Q  U  O  K 

Note: For the Decryption apply the just reverse process of encryption.

Example

let’s implement the Vernam Cipher in Python:

Python




import random
 
def generate_key(plaintext_length):
    key = ''.join(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(plaintext_length))
    return key
 
def encrypt(plaintext, key):
    ciphertext = ''.join(chr(ord(p) ^ ord(k)) for p, k in zip(plaintext, key))
    return ciphertext
 
def decrypt(ciphertext, key):
    decrypted_text = ''.join(chr(ord(c) ^ ord(k)) for c, k in zip(ciphertext, key))
    return decrypted_text
 
# Example usage
if __name__ == "__main__":
    plaintext = "HELLO"
    key = generate_key(len(plaintext))
 
    print("Plaintext:", plaintext)
    print("Key:", key)
 
    ciphertext = encrypt(plaintext, key)
    print("Ciphertext:", ciphertext)
 
    decrypted_text = decrypt(ciphertext, key)
    print("Decrypted Text:", decrypted_text)


Explanation

  • The ‘generate_key()' function creates a random key of the same length as the plaintext. For this example, we are using uppercase letters from ‘A’ to ‘Z’ for simplicity, but in practice, any binary data or random characters should be used.
  • The ‘encrypt()' function takes the plaintext and the key as input and performs the encryption using bitwise XOR (^) operation. Each character in the plaintext is combined with the corresponding character in the key using XOR, producing the ciphertext.
  • The ‘decrypt()' function performs the decryption in the same way as the encryption. By XORing the ciphertext with the key, it retrieves the original plaintext.
  • In the example usage section, we define a sample plaintext (“HELLO”) and generate a random key using ‘generate_key()'.
  • We then encrypt the plaintext using the Vernam Cipher and the generated key and print the resulting ciphertext.
  • Finally, we decrypt the ciphertext back to the original plaintext using the same key and print the decrypted text.

Output:

Plaintext: HELLO
Key: OCVHW
Ciphertext: iRTZP
Decrypted Text: HELLO

In this example, the Vernam Cipher successfully encrypts and decrypts the plaintext, demonstrating the correctness of the algorithm. However, it is essential to note that the one-time pad requires a truly random and secret key, which must be exchanged securely between the sender and receiver. Additionally, the key should never be reused, or else the security of the cipher is compromised.

Vernam Cipher in Cryptography

Vernam Cipher is a method of encrypting alphabetic text. It is one of the Substitution techniques for converting plain text into cipher text. In this mechanism, we assign a number to each character of the Plain-Text, like (a = 0, b = 1, c = 2, … z = 25). 
Method to take key: In the Vernam cipher algorithm, we take a key to encrypt the plain text whose length should be equal to the length of the plain text. 

Similar Reads

Encryption Algorithm

Assign a number to each character of the plain text and the key according to alphabetical order.  Bitwise XOR both the number (Corresponding plain-text character number and Key character number).  Subtract the number from 26 if the resulting number is greater than or equal to 26, if it isn’t then leave it....

Advantages of the Vernam Cipher

...

Disadvantages of the Vernam Cipher

Perfect Secrecy: With a truly random and unpredictable key of the same length as the message, the charnum cipher provides perfect confidentiality. This means that, in theory, an encrypted message provides no information about the original message....