Css Exp 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Vidya Vikas Education Trust’s

Universal College of Engineering, Kaman Road, Vasai-401208


Accredited by B+ Grade by NAAC

Experiment No 2

Aim: To implement Vignere cipher algorithm.

Theory:

Introduction

Vigenere Cipher is a method of encrypting alphabetic text. It uses a simple form of


polyalphabetic substitution. A polyalphabetic cipher is any cipher based on substitution, using
multiple substitution alphabets. The encryption of the original text is done using the Vigenère
square or Vigenère table.
● The table consists of the alphabets written out 26 times in different rows, each alphabet
shifted cyclically to the left compared to the previous alphabet, corresponding to the 26
possible Caesar Ciphers.
● At different points in the encryption process, the cipher uses a different alphabet from one of
the rows.
● The alphabet used at each point depends on a repeating keyword.

The vigenere cipher is an algorithm that is used to encrypt and decrypt the text. The vigenere
cipher is an algorithm of encrypting an alphabetic text that uses a series of interwoven caesar
ciphers. It is based on a keyword's letters. It is an example of a polyalphabetic substitution
cipher. This algorithm is easy to understand and implement. This algorithm was first described in
1553 by Giovan Battista Bellaso. It uses a Vigenere table or Vigenere square for encryption and
decryption of the text. The vigenere table is also called the tabula recta.
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

Program

import re

import string

alphabets = "abcdefghijklmnopqrstuvwxyz" # this is the english letters

def encrypt(p, k):

c = ""

kpos = [] # return the index of characters ex: if k='d' then kpos= 3

for x in k:

# kpos += alphabets.find(x) #change the int value to string

kpos.append(alphabets.find(x))

i=0

for x in p:
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

if i == len(kpos):

i=0

pos = alphabets.find(x) + kpos[i] #find the number or index of the character and perform the
shift with the key

print(pos)

if pos > 25:

pos = pos-26 # check you exceed the limit

c += alphabets[pos].capitalize() #because the cipher text always capital letters

i +=1

return c

def decrypt(c, k):

p = ""

kpos = []
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

for x in k:

kpos.append(alphabets.find(x))

i=0

for x in c:

if i == len(kpos):

i=0

pos = alphabets.find(x.lower()) - kpos[i]

if pos < 0:

pos = pos + 26

p += alphabets[pos].lower()

i +=1

return p
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

try:

print("Welcome to vigenere cipher.\n\n"

"The text message should contain only characters and the key should be one character
word \n"

"Press 1 to Enrypt a message \npress 2 to Decrypt a message")

choose = input("Choice: ")

if choose == '1':

p = input("enter the plain text: ")

p = p.replace(" ", "") # this will make sure that there is no space in the message

if p.isalpha():

k = input("Enter the key: ")

k = k.strip() # remove the white spaces from both sides

if k.isalpha():
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

# print(k)

c = encrypt(p, k)

print("The cipher text is: ", c)

else:

print(k)

print("Enter valid key, key is only one character word!")

else:

print("only letters are allowed !!")

elif choose == '2':

c = input("enter the cipher text: ")

c = c.replace(" ", "")

if c.isalpha():
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

k = input("Enter the key: ")

if not k.isalpha():

print("Enter valid key, key is only one character word!")

else:

p = decrypt(c, k)

print("The plain text is: ", p)

else:

print("only letters are allowed!")

else:

print("Please enter a valid choice!")

except Exception as e:

print(e)
Vidya Vikas Education Trust’s
Universal College of Engineering, Kaman Road, Vasai-401208
Accredited by B+ Grade by NAAC

exit()

Output:

Conclusion:

Hence we have successfully implemented the vigenere cipher algorithm.

You might also like