In: Computer Science
Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example:
codes = {'A':'%', 'a':'9', 'B':'@', 'b':'#', etc....}
Using this example, the letter “A” would be assigned the symbol %, the letter “a” would be assigned the number 9, the letter “B” would be assigned the symbol “@” and so forth.
The program should open a specified text file, read its contents, and then use the dictionary to write an encrypted version of the file’s contents to a second file. Each character in the second file should contain the code for the corresponding character in the first file. Additionally, the order of the characters will be reversed. In other words, the encrypted version of the first character in the original file will be last in the new file, the encrypted version of the second character will be second to last in the new file, and so on. The encrypted version of the last character in the original file will be the first character in the new file.
Write a second program that opens an encrypted file and displays its decrypted contents on the screen.
Place your Python code and a screen shot of the output from both of your programs for #2 here (2 codes and 2 screen shots).
The first program is given below that open a file data_file.txt read file and write encrypted version of character into encrypted_file.txt . The last character in original file will be the first character in new file.
def read_file(codes):
#open data_file.txt
file=open("data_file.txt", "r")
#open encrypted_file.txt
file_w=open("encrypted_file.txt", "w")
count=0
str1=""
#read file character by character
while 1:
char = file.read(1)
#increment count by 1
count=count+1
if not char:
break
#append character into str1
str1=str1+char
#find length of str1
ch_count=len(str1)-1
#continue until ch_count not greater than equal to 0
while ch_count>=0:
#take character at position ch_count in str1
key=str1[ch_count]
#check character is in dictionary
if key in codes:
#store their code into ans
ans=codes[key]
#otherwise keep as it is
else:
ans=key
#write into encrypted_file
file_w.write(ans)
#decrement count by 1
ch_count=ch_count-1
print("Encrypted successfully")
#close file
file.close()
file_w.close()
codes={'A':'%','B':'@','C':'h','D':'!','E':'$','F':'+','G':'&','H':'*','I':'-',
'J':'/','K':'1','L':'2','M':'3','N':'4','O':'5','P':'6','Q':'7','R':'8',
'S':'g','T':'a','U':'b','V':';','W':'c','X':'d','Y':'e','Z':'f','a':'9',
'b':'#','c':'i','d':'j','e':'k','f':'l','g':'m','h':'n','i':'o','j':'p',
'k':'q','l':'r','m':'s','n':'t','o':'u','p':'v','q':'w','r':'x','s':'y',
't':'z','u':'A','v':'B','w':'C','x':'D','y':'E','z':'F'}
read_file(codes)
The screenshot of code is given below:
The data_file.txt
This is my first program
dfJh
dhjs
encrypted_file.txt (after execution of program):
ypnj
n/lj
s9xmuxv zyxol Es yo yona
Output:
The second program is given below:
that open a file encrypted_file.txt read file and write decrypted version of character into decrypted_file.txt and also print on output screen . The last character in original file will be the first character in new file.
def read_file(codes):
#open encrypted_file.txt
file=open("encrypted_file.txt", "r")
#open decrypted_file.txt
file_w=open("decrypted_file.txt", "w")
count=0
str1=""
str2=""
#read file character by character
while 1:
char = file.read(1)
#increment count by 1
count=count+1
if not char:
break
#append character into str1
str1=str1+char
#find length of str1
ch_count=len(str1)-1
#continue until ch_count not greater than equal to 0
while ch_count>=0:
#take character at position ch_count in str1
val=str1[ch_count]
ans=val
#check val exist in dictionary value
for key, value in codes.items():
if val == value:
#set ans equals to key
ans=key
str2=str2+ans
#write into decrypted_file.txt
file_w.write(ans)
#decrement count by 1
ch_count=ch_count-1
print("Decrypted successfully")
print("The decrypted content is:")
print(str2)
#close file
file.close()
file_w.close()
codes={'A':'%','B':'@','C':'h','D':'!','E':'$','F':'+','G':'&','H':'*','I':'-',
'J':'/','K':'1','L':'2','M':'3','N':'4','O':'5','P':'6','Q':'7','R':'8',
'S':'g','T':'a','U':'b','V':';','W':'c','X':'d','Y':'e','Z':'f','a':'9',
'b':'#','c':'i','d':'j','e':'k','f':'l','g':'m','h':'n','i':'o','j':'p',
'k':'q','l':'r','m':'s','n':'t','o':'u','p':'v','q':'w','r':'x','s':'y',
't':'z','u':'A','v':'B','w':'C','x':'D','y':'E','z':'F'}
read_file(codes)
The screenshot of code is given below:
encrypted_file.txt:
ypnj
n/lj
s9xmuxv zyxol Es yo yona
decrypted_file.txt(after execution of program):
This is my first program
dfJh
dhjs
Output: