NIS EXP6-Vernam Cipher
NIS EXP6-Vernam Cipher
6
Aim : Write a program to implement Vernam Cipher.
Introduction:
Encryption Algorithm
1. Assign a number to each character of the plain text and the key according to
alphabetical order.
2. Bitwise XOR both the number (Corresponding plain-text character number and Key
character number).
3. 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
Program:
import random
def generate_key(plaintext_length):
key = ''.join(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in
range(plaintext_length))
return key
# Example usage
if __name__ == "__main__":
plaintext = "HELLO"
key = generate_key(len(plaintext))
print("Plaintext:", plaintext)
print("Key:", key)
OUTPUT
Plaintext: HELLO
Key: OCVHW
Ciphertext: iRTZP
Decrypted Text: HELLO
Conclusion:
The Vernam cipher is, in theory, a perfect cipher. Instead of a single key, each
plaintext character is encrypted using its own key. This key -- or key stream — is
randomly generated or is taken from a one-time pad, e.g. a page of a book. The key
must be equal in length to the plaintext message. The fact that each character of the
message is encrypted using a different key prevents any useful information being
revealed through a frequency analysis of the ciphertext.