Vernam Cipher in Cryptography
Last Updated :
25 Dec, 2023
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
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
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 al
6 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 encr
4 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,
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
9 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
5 min read
DNA Cryptography
Cryptography is the branch of science that deals with the encoding of information to hide messages. It plays a vital role in the infrastructure of communication security. The Pioneering work had been done by Ashish Gehani et al and Amin et al after Leonard Max Adleman had shown the capability of mol
7 min read
What is p-box in Cryptography?
P-boxes are permutation boxes which are usually one of the main components of a modern block cipher. They are also known as D-boxes or diffusion boxes. P-boxes are used in the block cipher called the data encryption standard commonly known as DES in cryptography. In this article, we will look into w
5 min read
Avalanche Effect in Cryptography
In cryptography, the avalanche effect is a term associated with a specific behavior of mathematical functions used for encryption. The avalanche effect is considered one of the desirable properties of any encryption algorithm. A slight change in either the key or the plain text should result in a si
5 min read
Cryptography and its Types
Cryptography is a technique of securing communication by converting plain text into ciphertext. It involves various algorithms and protocols to ensure data confidentiality, integrity, authentication, and non-repudiation. In this article, we will discuss cryptography and its types. What is Cryptograp
7 min read
ECB Mode vs CBC Mode in Cryptography
The Electronic Code Book (ECB) is the easiest block cipher way of functioning. It is easier since each block of input plaintext is directly encrypted, and the output is in the form of encrypted ciphertext blocks. In general, if a message is bigger than b bits in size, it can be divided into many blo
5 min read
CAST Algorithm in Cryptography
CAST is a general procedure for creating a family of block ciphers. Individual ciphers have names like CAST-128 and CAST-256. The article focuses on discussing the CAST algorithm in cryptography. What is CAST Algorithm?The CAST algorithm is generally used in IP security and follows the Fiestal struc
4 min read
Cryptography Tutorial
Cryptography is a technique of securing communication by converting plain text into unintelligible ciphertext. It involves various algorithms and protocols to ensure data confidentiality, integrity, authentication, and non-repudiation. The two primary types of cryptography are symmetric key cryptogr
7 min read
PRG, PRF and PRP in Cryptography
Cryptography is the study and practice of techniques for secure communication in the presence of third parties called adversaries. It deals with developing and analyzing protocols that prevent malicious third parties from retrieving information being shared between two entities thereby following the
2 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 i
4 min read
Symmetric Key Cryptography
Symmetrical Key Cryptography also known as conventional or single-key encryption was the primary method of encryption before the introduction of public key cryptography in the 1970s. In symmetric-key algorithms, the same keys are used for data encryption and decryption. This type of cryptography pla
11 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 =
15 min read
Electronic Code Book (ECB) in Cryptography
Electronic Code Book (ECB) is the simplest AES block cipher mode. A block cipher takes a fixed-size block of text (b bits) and a key and outputs a block of b-bit encrypted text. If the number of plaintexts to be encrypted is greater than b bits, then the block cipher can still be used by breaking th
7 min read
Knapsack Encryption Algorithm in Cryptography
Knapsack Encryption Algorithm is the first general public key cryptography algorithm. It was developed by Ralph Merkle and Mertin Hellman in 1978. As it is a Public key cryptography, it needs two different keys. One is the Public key which is used for the Encryption process and the other one is the
6 min read
Stream Ciphers
In stream cipher, one byte is encrypted at a time while in block cipher ~128 bits are encrypted at a time. Initially, a key(k) will be supplied as input to pseudorandom bit generator and then it produces a random 8-bit output which is treated as keystream. The resulted keystream will be of size 1 by
4 min read