In: Computer Science
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key.
TRY TO MAKE IT AS EASY AS YOU CAN.
def new_alph(ch):
ch = ch.lower()
alph = 'abcdefghijklmnopqrstuvwxyz'
new_alph = alph[alph.index(ch):] + alph[:alph.index(ch)]
return new_alph
def encrypt(text, big_key):
res = ''
alph = 'abcdefghijklmnopqrstuvwxyz'
i = 1
for char in big_key:
new = new_alph(char)
for t in text:
if alph.count(t) == 1 :
res += new[alph.index(t)]
text = text[i:]
break
elif alph.count(t.lower()) == 1:
res += new[alph.index(t.lower())].upper()
text = text[i:]
break
else:
res += t
text = text[i:]
break
i += 1
return res
def decrypt(text, big_key):
res = ''
alph = 'abcdefghijklmnopqrstuvwxyz'
i = 1
for char in big_key:
new = new_alph(char)
for t in text:
if alph.count(t) == 1 :
res += alph[new.index(t)]
text = text[i:]
break
elif alph.count(t.lower()) == 1:
res += alph[new.index(t.lower())].upper()
text = text[i:]
break
else:
res += t
text = text[i:]
break
i += 1
return res
text1 = input("Enter Plain text to encypt: ")
key = input("Enter key : ")
if len(key) <= len(text1):
big_key = key * (len(text1) // len(key)) + key[:len(text1) %
len(key)]
text_encrypt = encrypt(text1, big_key)
print('|Your text: "' + text1 + '"')
print('|Your key : "' + key + '"')
print('|Ans : ' + text_encrypt)
text_dec = input("Enter encrypted text to get plaintext : ")
text_decrypt = decrypt(text_dec, big_key)
key1 = input("Enter key : ")
print('|Your text: "' + text_dec + '"')
print('|Your key : "' + key1 + '"')
print('|Ans : ' + text_decrypt)
else:
print('Error: len(key)>len(text) ')
if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and i can edit and change the answers if you argue, thanks :)