Question

3. File Encryption and Decryption Write a program that uses a dictionary to assign codes to each letter of the alphabet. Fo
program must be written in python and have commentary. thank you
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for the two programs. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#encrypt.py (program to encrypt)

#creating a dictionary, mapping all letters to other codes
enc_dict={'A': '&', 'a': '"', 'B': '#', 'b': '|', 'C': '-', 'c': '<', 'D': "'", 'd': 'l', 'E': '@', 'e': 'z', 'F': 'k', 'f': ')', 'G': 'p', 'g': 'j', 'H': 'u', 'h': 'g', 'I': 'm', 'i': '$', 'J': '}', 'j': '{', 'K': 'd', 'k': 'w', 'L': 'o', 'l': '*', 'M': '/', 'm': ';', 'N': '!', 'n': '%', 'O': 'c', 'o': 'b', 'P': 'y', 'p': '[', 'Q': 't', 'q': 'e', 'R': 'f', 'r': 'x', 'S': '>', 's': 'h', 'T': '(', 't': ']', 'U': '+', 'u': 'v', 'V': '_', 'v': '~', 'W': 'n', 'w': '.', 'X': ':', 'x': '\\', 'Y': '`', 'y': '=', 'Z': 'q', 'z': '^'}
#getting input file name
filename=input('Enter input file name: ')
#opening input file and reading text. assuming input file contains only letters
input_file=open(filename)
text=input_file.read()
#getting output file name and opening it (or creating it)
filename=input('Enter output file name: ')
output_file=open(filename,'w')
#looping through each character of input file
for i in text:
    #if current character is present on enc_dict, taking the corresponding
    #code from dict and writing to output file
   
if i in enc_dict:
        output_file.write(enc_dict[i])
    else:
        #else, simply writing the same character to output file (spaces, new lines etc)
       
output_file.write(i)
print('Encrypted text has been saved in',filename)
#closing output file
output_file.close()

#decrypt.py (program to decrypt)

#creating a dictionary, mapping all symbols to the original letters

dec_dict={'&': 'A', '"': 'a', '#': 'B', '|': 'b', '-': 'C', '<': 'c', "'": 'D', 'l': 'd', '@': 'E', 'z': 'e', 'k': 'F', ')': 'f', 'p': 'G', 'j': 'g', 'u': 'H', 'g': 'h', 'm': 'I', '$': 'i', '}': 'J', '{': 'j', 'd': 'K', 'w': 'k', 'o': 'L', '*': 'l', '/': 'M', ';': 'm', '!': 'N', '%': 'n', 'c': 'O', 'b': 'o', 'y': 'P', '[': 'p', 't': 'Q', 'e': 'q', 'f': 'R', 'x': 'r', '>': 'S', 'h': 's', '(': 'T', ']': 't', '+': 'U', 'v': 'u', '_': 'V', '~': 'v', 'n': 'W', '.': 'w', ':': 'X', '\\': 'x', '`': 'Y', '=': 'y', 'q': 'Z', '^': 'z'}



filename=input('Enter input file name: ')
#opening input file and reading text
input_file=open(filename)
text=input_file.read()
filename=input('Enter output file name: ')

#creating output file

output_file=open(filename,'w')
#looping through each character in text
for i in text:
    #if current character is present in dec_dict, taking corresponding
    #character from the dict and writing to output file
    if i in dec_dict:
        output_file.write(dec_dict[i])
    else:
        #else. writing the same character (spaces, newlines etc)
        output_file.write(i)
print('Decrypted text has been saved in',filename)

#closing output file
output_file.close()

#output of first program

Enter input file name: input.txt
Enter output file name: output.txt
Encrypted text has been saved in output.txt
 

#output of second program



Enter input file name: output.txt
Enter output file name: output2.txt
Decrypted text has been saved in output2.txt

#input.txt

supergirl used her x ray vision to find where the treasure is hidden
but when she reached the location, batboy had already took it
and turns out, he had some help from batman and wonderwoman
hey

#output1.txt

hv[zxj$x* vhzl gzx \ x"= ~$h$b% ]b )$%l .gzxz ]gz ]xz"hvxz $h g$llz%
|v] .gz% hgz xz"<gzl ]gz *b<"]$b%, |"]|b= g"l "*xz"l= ]bbw $]
"%l ]vx%h bv], gz g"l hb;z gz*[ )xb; |"];"% "%l .b%lzx.b;"%
gz=

#output2.txt

supergirl used her x ray vision to find where the treasure is hidden
but when she reached the location, batboy had already took it
and turns out, he had some help from batman and wonderwoman
hey
Add a comment
Know the answer?
Add Answer to:
program must be written in python and have commentary. thank you 3. File Encryption and Decryption...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to...

    File Encryption and Decryption chapter 9 programming exercise #3 Design and write a python program to successfully complete chapter 9 programming exercise #3. File Encryption and Decryption 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...

  • Java Programming Part 1 File encryption is the science of writing the contents of a file...

    Java Programming Part 1 File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a...

  • In Python, write a program that assigns “codes” to each letter of the alphabet. For example,...

    In Python, write a program that assigns “codes” to each letter of the alphabet. For example, codes = {“a”: “%”, “b”:”9”, “c”:”?”,….} Write a function that takes in this dictionary and a sentence to encrypt the sentence. Write another function that takes the encrypted sentence and decrypt it.

  • Security is an important feature of information systems. Often, text is encrypted before being sent, and...

    Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located. For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly...

  • Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File...

    Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....

  • In Python, do a basic encryption of a text file in the following manner. The program...

    In Python, do a basic encryption of a text file in the following manner. The program encrypt.py will read in the following text file and rearrange the lines in the file randomly and save the rearranged lines of txt to another file called encrypted.txt. It will also save another file called key.txt that will contain the index of the lines that were rearranged in the encrypted file, so for example if the 4th line from the original file is now...

  • This is to be written in C++. Thank you so much for you help as I'm...

    This is to be written in C++. Thank you so much for you help as I'm really struggling with this. Must be written in this format: #include <stdio.h> int main(void) { printf("Hello World\n"); return 0; } The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise,...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • MASM Assembly language -- Message Encryption Pgm You are to write a program to input a...

    MASM Assembly language -- Message Encryption Pgm You are to write a program to input a text and a key and encrypt the text. I will supply you an encryption key consisting of multiple characters. Use this key to encrypt and decrypt the plain text by XOR-ing each character of the key against a corresponding byte in the message. Repeat the key as many times as necessary until all plain text bytes are translated. Suppose, for example, the key were...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT