In: Computer Science
1. Monoalphabetic substitution (using the Caesar Cipher tool right) Encipher (convert plaintext into ciphertext): meal times Decipher (convert ciphertext in to plaintext): JR PHDQ JUHHQ 2. Polyalphabetic substitution (using the Vigenere Square in the lecture slide) Encipher: fall Decipher: VPX TWOKM
def encrypt(text,s):
result = ""
# transverse the plain text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters in plain text
if (char.isupper()):
result += chr((ord(char) + s-65) % 26 + 65)
# Encrypt lowercase characters in plain text
else:
result += chr((ord(char) + s - 97) % 26 + 97)
return result
#check the above function
text = "meal times"
s = 4
print "Plain Text : " + text
print "Shift pattern : " + str(s)
print "Cipher: " + encrypt(text,s)
This is the encipher of Monoalphabetic substitution with key 4.
message = "JR PHDQ JUHHQ"
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for key in range(len(LETTERS)):
translated = ''
for symbol in message:
if symbol in LETTERS:
num = LETTERS.find(symbol)
num = num - key
if num < 0:
num = num + len(LETTERS)
translated = translated + LETTERS[num]
else:
translated = translated + symbol
print('Hacking key #%s: %s' % (key, translated))
This is the solution for deciphering monoalphabetic substitution using each and every key. By observing all the outputs the solution is "GO MEAN GREEN".
2.
For viginere cipher, we should have a key with which we have to encipher the given text. So, Giving the function for viginere cipher in python,
import random
import string
from itertools import cycle
def encrypt(flag , key):
ciphertext=[]
for i,j in zip(flag,cycle(key)):
x=(ord(i)+ord(j))%95
x+=ord(' ')
ciphertext.append(chr(x))
return("" . join(ciphertext))
For deciphering, the function is as follows,
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return(key)
else:
for i in range(len(string) -
len(key)):
key.append(key[i % len(key)])
return("" . join(key))
def originalText(cipher_text, key):
orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) -
ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
if __name__ == "__main__":
string = input()
keyword = input()
key = generateKey(string, keyword)
cipher_text = cipherText(string,key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",
originalText(cipher_text, key))