You are on page 1of 2

#Write a program that uses dictionary to assign 'codes' to each letter of the

alphabet
#Decryption program - opens encrypted file and displays the decrypted contents

#define main
def main():

#declare variables
encryption = dict()

print('This program decrypts the encrypted text file "encrypted.txt".')

decryptFile()

def decryptFile():

#variables
charTxt = ''
decryptList = []
finalStr = ''

#encryption key
encryption = {'A':'m', 'a':'M', 'B':'n', 'b':'N', 'C':'o', 'c':'O', 'D':'p',
'd':'P',
'E':'q', 'e':'Q', 'F':'r', 'f':'R', 'G':'s', 'g':'S', 'H':'t',
'h':'T',
'I':'u', 'i':'U', 'J':'v', 'j':'V', 'K':'w', 'k':'W', 'L':'x',
'l':'X',
'M':'y', 'm':'Y', 'N':'z', 'n':'Z', 'O':'a', 'o':'A', 'P':'b',
'p':'B',
'Q':'c', 'q':'C', 'R':'d', 'r':'D', 'S':'e', 's':'E', 'T':'f',
't':'F',
'U':'g', 'u':'G', 'V':'h', 'v':'H', 'W':'i', 'w':'I', 'X':'j',
'x':'J',
'Y':'k', 'y':'K', 'Z':'l', 'z':'L', '.':'0', '?':'1', '!':'2',
',':'3',
' ':'4'}

#open encrypted file, copy contents, and close


encryTxtFile = open('encrypted.txt', 'r')
txtFile = encryTxtFile.read()
encryTxtFile.close

#test - print(txtFile)

#cycle through characters


for chars in txtFile:

#cycle through keys in dictionary


for key in encryption:
charTxt = encryption.get(key, ' ')

#compare chars, write to list


if chars == charTxt:
decryptList.append(key)
#test - print(decryptList)

#convert list to string


print('The decrypted file says: ', finalStr.join(decryptList))
#call main
if __name__ == "__main__":
main()

You might also like