Vernam Cipher in Cryptography

Last Updated : 25 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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. 

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.

Advantages 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.
  • Unbreakable with a Truly Random Key: In theory, if the key stream were truly random and used only once (hence the name “one-time pad”), the Vernam cipher would be unbreakable. The key stream is the same length as the message, and the likelihood of any decryption of the ciphertext is equal.
  • No Pattern Recognition: Unlike other encryption algorithms, the Vernam cipher does not produce a pattern of ciphertext that can be used to break the code. Each character of the ciphertext is encrypted independently.
  • Simple Algorithm: Both the encryption and decryption algorithms are simple and involve a bitwise XOR operation. This simplicity can be an advantage in some situations.

Disadvantages of the Vernam Cipher

  • Key Management: The main practical challenge of Vernam ciphers is the management of truly random private keys. The key must be the same length as the message, and a new key must be generated for each message. Distributing keys securely is not a trivial problem.
  • Key Reuse Compromises Security: If keys are reused, the security of the Vernum cipher is compromised. If an attacker intercepts two ciphertexts encrypted with the same key, he can use XOR to cancel the key stream and reveal the XOR of the two plaintexts. This could potentially leak information about the original message.
  • Key Storage and Transmission: Transmitting and storing keys securely is a major challenge. Once an adversary has access to the key, he or she can decrypt the ciphertext.
  • Practicality and Efficiency: Vernam ciphers are often impractical for large-scale use due to the challenges of generating and securely distributing one-time pads. It is typically used only in scenarios where complete confidentiality is most important.
  • Key Length Equal to Message Length: The key must be at least as long as the message, which can be inefficient for long messages. This is especially true when compared to modern symmetric key cryptography, which uses shorter keys.

For the implementation please refer to this article: Implementation of Vernam Cipher or One Time Pad Algorithm



Similar Reads

Difference between Block Cipher and Transposition Cipher
1. Block Cipher :  Block Cipher is the symmetric key cipher used for converting the plain text into cipher text. It uses a simple substitution process or sometimes the permutation process where the block of plain text is substituted with arbitrary bit of cipher text.    2. Transposition Cipher :  Transposition Cipher rearranges the position of the
2 min read
Difference between Monoalphabetic Cipher and Polyalphabetic Cipher
A Monoalphabetic Cipher is a cipher where each letter in the plaintext is always mapped to the same letter in the ciphertext While a Polyalphabetic Cipher is a cipher where each letter in the plaintext can be encrypted to multiple possible letters in the ciphertext, depending on its position and a more complex algorithm. In this article, we will se
3 min read
Difference between Substitution Cipher Technique and Transposition Cipher Technique
An encryption algorithm, or cipher, is a means of transforming plaintext into ciphertext under the control of a secret key. Cryptographic algorithms are classified as Symmetric key cryptography and Asymmetric key cryptography. All encryption algorithms are based on two general principles. substitution, in which each element in the plaintext(bit, le
4 min read
Difference between Block Cipher and Stream Cipher
Block Cipher and Stream Cipher belong to the symmetric key cipher. These two block ciphers and stream cipher are the methods used for converting the plain text into ciphertext. The main difference between a Block cipher and a Stream cipher is that a block cipher converts the plain text into cipher text by taking the plain text's block at a time. Wh
4 min read
Bifid Cipher in Cryptography
This cipher technique considered more secure compared to other substitution algorithms reason being it breaks the message apart into two separate streams and then recombines them. It is a combination of the Polybius square with the transposition and uses fractionation to achieve diffusion. This encrypting technique invented by Felin Delastelle. It
4 min read
Transposition Cipher Techniques in Cryptography
Transposition Ciphers are an essential part of cryptography that uses systematic shuffling of plain text characters or bits to secure data by altering their positions based on some defined way or algorithm. Moreover, unlike substitutive codes where different letters substitute others, in these, you just shift about original letters hence it does no
5 min read
Caesar Cipher in Cryptography
The Caesar Cipher is one of the simplest and oldest methods of encrypting messages, named after Julius Caesar, who reportedly used it to protect his military communications. This technique involves shifting the letters of the alphabet by a fixed number of places. For example, with a shift of three, the letter 'A' becomes 'D', 'B' becomes 'E', and s
12 min read
What is Multiplicative Cipher in Cryptography?
A multiplicative cipher is a type of cipher that comes under a monoalphabetic cipher, in which each letter that is present in the plaintext is replaced by a corresponding letter of the ciphertext, according to a fixed multiplication key. In this article, we will learn about the multiplicative cipher and its working for the encryption and decryption
9 min read
Classical Cryptography and Quantum Cryptography
Cryptography is the technique that is used for secure communication between two parties in a public environment where unauthorized users and malicious attackers are present. In cryptography, there are two processes i.e. encryption and decryption performed at the sender and receiver end respectively. Encryption is the process where simple multimedia
7 min read
Latin alphabet cipher
The Latin Alphabet Cipher Encryption Technique is one of the earliest and simplest techniques of encrypting data. It’s simply a type of substitution cipher technique, i.e., each letter of a given text is substituted by its corresponding number as represented in its alphabetical order. For Example, we have given a string as "hello everyone", then it
5 min read
Transforming a Plain Text message to Cipher Text
There are two primary ways in which a plain text can be modified to obtain cipher text: Substitution Technique and Transposition Technique. 1. Substitution Technique: Substitution technique involves the replacement of the letters by other letters and symbols. In a more straightforward way, the characters of plaintext are replaced, and other substit
6 min read
Feistel Cipher
Feistel Cipher model is a structure or a design used to develop many block ciphers such as DES. Feistel cipher may have invertible, non-invertible and self invertible components in its design. Same encryption as well as decryption algorithm is used. A separate key is used for each round. However same round keys are used for encryption as well as de
3 min read
Encrypt using XOR Cipher with Repeating Key
Computer Architectures have predefined ASCII values & Binary forms for all printable characters, which allows us to operate bit-wise logic like XOR and most encryption/decryption algorithms depend on. The Key is XOR-operated on the plain text to produce the encrypted text. Only Parameters required to encrypt a plain text using this technique: P
2 min read
Block Cipher Design Principles
Block ciphers are built in the Feistel cipher structure. Block cipher has a specific number of rounds and keys for generating ciphertext.Block cipher is a type of encryption algorithm that processes fixed-size blocks of data, usually 64 or 128 bits, to produce ciphertext. The design of a block cipher involves several important principles to ensure
3 min read
Symmetric Cipher Model
Symmetric Encryption is the most basic and old method of encryption. It uses only one key for the process of both the encryption and decryption of data. Thus, it is also known as Single-Key Encryption. A few basic terms in Cryptography are as follows: Plain Text: original message to be communicated between sender and receiver Cipher Text: encoded f
3 min read
XOR Cipher
XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating random encryption keys to match with the correct one. Below is a simple implementation in C++. The concept of implementation is to first define XOR - encryption key and then to perform XOR operation of the characters in the String
5 min read
Rail Fence Cipher - Encryption and Decryption
Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail Fence algorithm. The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded. Examples:  EncryptionInput : "GeeksforGeeks "Key = 3Output : GsGsekfrek eoeDecryptionInput : GsGsekf
15 min read
Implementation of Affine Cipher
The Affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard
10 min read
Implementing Atbash Cipher
Definition: Atbash cipher is a substitution cipher with just one specific key where all the letters are reversed that is A to Z and Z to A. It was originally used to encode the Hebrew alphabets but it can be modified to encode any alphabet. Relationship to Affine: Atbash cipher can be thought of as a special case of Affine cipher with both the keys
7 min read
Keyword Cipher
Keyword cipher is a form of monoalphabetic substitution. A keyword is used as the key, and it determines the letter matchings of the cipher alphabet to the plain alphabet. Repeats of letters in the word are removed, then the cipher alphabet is generated with the keyword matching to A, B, C, etc. until the keyword is used up, whereupon the rest of t
15+ min read
Null Cipher
A null cipher, also known as concealment cipher, is an ancient form of encryption where the plaintext is mixed with a large amount of non-cipher material. Today it is regarded as a simple form of steganography, which can be used to hide ciphertext. There are various options of using the Null Cipher. Here we are taking the first letter from each wor
6 min read
Columnar Transposition Cipher
Given a plain-text message and a numeric key, cipher/de-cipher the given text using Columnar Transposition Cipher The Columnar Transposition Cipher is a form of transposition cipher just like Rail Fence Cipher. Columnar Transposition involves writing the plaintext out in rows, and then reading the ciphertext off in columns one by one. Examples: Enc
12 min read
What is Round Cipher?
Round ciphers are also known as block ciphers, and they are a classification of encryption algorithms that work systematically, converting the plaintext into ciphertext. These algorithms work on a limited number of bits at a time and subject them to a set of mathematical processes called rounds which are used to bring about the act of encryption. I
10 min read
What is Cipher?
Ciphers are the most vital components of cryptography, as it is through them that the protection of information is made possible in the ever-growing digital environment. In the present generation, given the technological advancement especially in the field of communication, the aspect of security is critical. Cipher is used in securing financial tr
7 min read
What is Monoalphabetic Cipher?
Monoalphabetic Cipher is a part of the substitution technique in which a single cipher alphabet is used per message (mapping is done from plain alphabet to cipher alphabet). Monoalphabetic cipher converts plain text into cipher text and re-convert a cipher text to plain text. Monoalphabetic Cipher eliminates the brute-force techniques for cryptanal
4 min read
Playfair Cipher with Examples
The Playfair cipher was the first practical digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone but was named after Lord Playfair who promoted the use of the cipher. In playfair cipher unlike traditional cipher we encrypt a pair of alphabets(digraphs) instead of a single alphabet.It was used for tactical purposes by B
15+ min read
Substitution Cipher
Hiding some data is known as encryption. When plain text is encrypted it becomes unreadable and is known as ciphertext. In a Substitution cipher, any character of plain text from the given fixed set of characters is substituted by some other character from the same set depending on a key. For example with a shift of 1, A would be replaced by B, B w
6 min read
Autokey Cipher | Symmetric Ciphers
The Autokey Cipher is a method of encrypting messages to keep them secret. It uses a keyword to start the process, but instead of repeating that keyword, it merges letters from the actual message as part of the key. This makes it harder for anyone trying to decode the message without knowing the key. Each letter in the message is shifted based on t
6 min read
History of Cryptography
Humans have two basic needs when we take about communication. One is the need to communicate selectively, to communicate and share information. These two basic needs while communicating gave rise to coding and encrypting the messages in such a way that only intended people could have access to the information. The word 'cryptography' originated fro
4 min read
Differences between Classical and Quantum Cryptography
Classical Cryptography: Classical cryptography is based on mathematics and it relies on the computational difficulty of factorizing large number. The security of classical cryptography is based on the high complexity of the mathematical problem for the instance factorization of large number. Quantum Cryptography: Quantum Cryptography is based on ph
1 min read