Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HOW DO YOU INITIALIZE THE CIPHERTEXT TO AN EMPTY STRING? ( PYTHON FUNCTION) I wa

ID: 3849071 • Letter: H

Question

HOW DO YOU INITIALIZE THE CIPHERTEXT TO AN EMPTY STRING? (PYTHON FUNCTION) I want to use substitution to create a ciphertext where EACH letter in the plaintext is represented by its value in the keysquare. Ignore all spaces and punctuation. For instance, the input: *?H E L L O WORLD 123!!!~ would return DGDFDXDXDDAXDDGVDXXVFVFGF

def Encrypt1(plaintext):

   # dictionary is defined here.
   keysquare = {'Q':'AA', 'C':'AD', '3':'AF', 'T':'AG', '6':'AV', 'W':'AX',
'M':'DA', 'O':'DD', 'E':'DF', 'H':'DG', 'N':'DV', 'L':'DX',
'8':'FA', 'A':'FD', '4':'FF', '2':'FG', '1':'FV', 'I':'FX',
'G':'GA', 'B':'GD', '5':'GF', 'Z':'GG', 'R':'GV', '7':'GX',
'S':'VA', 'X':'VD', '9':'VF', 'V':'VG', 'U':'VV', 'P':'VX',
'0':'XA', 'K':'XD', 'J':'XF', 'F':'XG', 'D':'XV', 'Y':'XX'}
  
   # get all the keys defined in dictionary
   keyList = keysquare.keys()
  
   # create a list to store the encrypted string.
   output = list()
  
   # iterate through the input string
   for i in plaintext:
       # if each character in string is part of the keys, then
       # encrypt the same.
       if i in keyList:
           temp = keysquare.get(i)
           output.append(temp)
  
   # print the outputs.
   print 'INPUT: ' + plaintext
   print 'OUTPUT: ' + ''.join(output)
  
Encrypt1("*?H E L L O WORLD 123!!!~#")

OUTPUT:

>python encrypt.py
INPUT: *?H E L L O WORLD 123!!!~
OUTPUT: DGDFDXDXDDAXDDGVDXXVFVFGF

Explanation / Answer

# function to encode plaintext using Caesar

# shift cipher

def encodePlaintext (plaintext, shiftKey):

ciphertext = ""

shiftNumber = ord(shiftKey) - ord('A')

for plainLetter in plaintext:

index = ((ord(plainLetter) - ord('A')

+ shiftNumber) % 26)

cipherLetter = chr(ord('A') + index)

ciphertext = ciphertext + cipherLetter

return ciphertext

def main():

key = raw_input ('Enter a shift key in uppercase: ')

message = raw_input('Enter a word in uppercase

to encrypt (# to quit): ')

while message != '#':

secret = encodePlaintext(message, key)

print 'The encrypted word is:', secret

message = raw_input('Enter a word in uppercase

to encrypt (# to quit): ')

print 'All done'