How to encrypt a file

2024/11/7 18:31:57

Just trimmed this down big time

I have an overall assignment that must read a file, encrypt it and then write the encrypted data to a new file.

what i've tried is this:

filename=input("Enter file name:")
fr=open(filename)
keep_going=0
data = fr.readline()
fw=open('encrypted_file.txt', 'w')
for x in range(len(data)):fw.write(data[x])       
fw.close()
fr.close()
Answer

If your goal is just to exchange the letters in a string with others that you specify, then the solution is the following:

decrypted = 'abcdefghijklmnopqrstuvwxyz' #normal alphabet
encrypted = 'MNBVCXZLKJHGFDSAPOIUYTREWQ' #your "crypted" alphabet#Encription
text = 'cryptme' #the string to be crypted
encrypted_text = ''
for letter in text:encrypted_text += encrypted[decrypted.find(letter)]
print encrypted_text
#will print BOWAUFC#Decription
text = encrypted_text #"BOWAUFC" in this example
decrypted_text = ''
for letter in text:decrypted_text += decrypted[encrypted.find(letter)]
print decrypted_text
#will print cryptme

Note that your "crypted alphabet" do not convert any white space or any symbols but the lowercase letters, if you have other symbols in your text you have to include them as well.

However, this is not the proper way to encrypt anything! As suggested by others already, look up for a proper encryption algorithm.

https://en.xdnf.cn/q/120706.html

Related Q&A

Any Idea on how Should I analyze this Algorithm? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Syntax error ; multiple statements found while

Why is there this syntax error Multiple statements found while compiling a single statement given when I run this code? Answer and help will be super appreciated for this python newbie here

No module named PyPDF2._codecs, even after already installed

I have installed PyPDF2==2.3.0, but I still get the error below when I import PyPDF2. The error message is:ModuleNotFoundError: No module named PyPDF2._codecs

rearding regex (python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

While Loop Guessing Number Game - Python

Im trying to make a guess the number between 1-10 game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start aga…

Calculating distance between word/document vectors from a nested dictionary

I have a nested dictionary as such:myDict = {a: {1:2, 2:163, 3:12, 4:67, 5:84}, about: {1:27, 2:45, 3:21, 4:10, 5:15}, apple: {1:0, 2: 5, 3:0, 4:10, 5:0}, anticipate: {1:1, 2:5, 3:0, 4:8, 5:7}, an: {1:…

Is there anything wrong with the Python code itself? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python Iterate Over String

So Ive got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters a…

what does with open do in this situation [duplicate]

This question already has answers here:What is the Python "with" statement used for?(3 answers)Closed 7 years ago.sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN D…

How to perform HTTP GET operation in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…