0% found this document useful (0 votes)
21 views3 pages

NIS EXP6-Vernam Cipher

The document describes the Vernam Cipher, a cryptographic method for encrypting text using a key of equal length to the plaintext. It outlines the encryption algorithm involving the assignment of numerical values to characters, bitwise XOR operations, and adjustments for values exceeding 26. The provided Python program demonstrates key generation, encryption, and decryption processes, illustrating the cipher's theoretical perfection and resistance to frequency analysis.

Uploaded by

ksai58701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

NIS EXP6-Vernam Cipher

The document describes the Vernam Cipher, a cryptographic method for encrypting text using a key of equal length to the plaintext. It outlines the encryption algorithm involving the assignment of numerical values to characters, bitwise XOR operations, and adjustments for values exceeding 26. The provided Python program demonstrates key generation, encryption, and decryption processes, illustrating the cipher's theoretical perfection and resistance to frequency analysis.

Uploaded by

ksai58701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.

6
Aim : Write a program to implement Vernam Cipher.
Introduction:

Vernam Cipher in Cryptography Vernam Cipher is a method of encrypting alphabetic


text. It is one of the Transposition techniques for converting a plain text into a 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 Vernam cipher algorithm, we take a key to encrypt the plain text which length
should be equal to the length of the plain text.

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

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)

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.

You might also like