Question

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 the 1st line in the encrypted file then the first index in the key will say 4 and so on. Next write a program called decrypt.py that reads the encrypted.txt and key.txt files and decrypts it to print the contents of the original file. So in short all you will be submitting is the encrypt.py and decrypt.py files as part of this assignment.

lest-Nctepo Who said, It is Just as I icared! Two Owls and Hen a Wren Have ฮา! built their nests in lay board! O lype hene to search

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random

fname = input("Enter name of the file:")

f = open(fname, "r").read().split("\n")

indices = []

for i in range(0, len(f)):

indices.append(i)

random.shuffle(indices) # shuffling indices

writef = open("encrypted.txt", "w")

writekey = open("key.txt", "w")

for each in indices:

writef.write(f[each]+"\n") # writing line to file

writekey.write(str(each)+" ") # writing index to file

writef.close()

writekey.close()

'''SAMPLE INPUT

Enter name of the file: text.txt

SAMPLE OUTPUT

key.txt contains

0 1 4 3 2

encrypted.txt contains

There was an Old Man with a beard

Who said, It is just as I learned!

Have all built their nests in my beard!

Four Larks and a Wren,

Two Owls and a Hen,

'''

# reading from files

en = open("encrypt.txt", "r").read().split("\n")

k = open("key.txt", "r").read().split()

f = open("decrypt.txt","w")

for i in range(len(k)):

for j in range(len(k)):

if i == int(k[j]):

f.write(en[j]+"\n") # writing to correct index

f.close()

''' SAMPLE OUTPUT

decrypt.txt contains

There was an Old Man with a beard

Who said, It is just as I learned!

Two Owls and a Hen,

Four Larks and a Wren,

Have all built their nests in my beard!

'''

Add a comment
Know the answer?
Add Answer to:
In Python, do a basic encryption of a text file in the following manner. The program...
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...

  • program must be written in python and have commentary. thank you 3. File Encryption and Decryption...

    program must be written in python and have commentary. thank you 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':'8','a' :'9', 'B' : e','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 e, and so forth. The program should open a...

  • 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...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Task 3: Encryption Mode – ECB vs. CBC The file pic original.bmp can be downloaded from...

    Task 3: Encryption Mode – ECB vs. CBC The file pic original.bmp can be downloaded from this lab’s website, and it contains a simple picture. We would like to encrypt this picture, so people without the encryption keys cannot know what is in the picture. Please encrypt the file using the ECB (Electronic Code Book) and CBC (Cipher Block Chaining) modes, and then do the following: 1. Let us treat the encrypted picture as a picture, and use a picture...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • All the white space among words in a text file was lost. Write a C++ program...

    All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime. You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc) - Input is a text file...

  • 1. Create an RSA private key 2. Output the key in a text format so that it shows the following:     modulus     public e...

    1. Create an RSA private key 2. Output the key in a text format so that it shows the following:     modulus     public exponent (e)     private exponent (d)     the primes (p and q) Send me a file called key.txt with this information. 3. Using openssl's rsautl option encrypt this message to me: "NAME" using the public key that's embedded with the private key above. Attach a file named encrypted.txt that contains the encrypted message. Hint: Copy the text above and put...

  • 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...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

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
Active Questions
ADVERTISEMENT