Python: Decrypting a file (have key)

Question:

I encrypted a file from ascii randomized key. I need to decrypt back into normal letters from and then put that into a new file

import random

lst = list(range(1,128)) #for key
numbersran = list()

while len(numbersran) < 128:      
    candidate = random.randint(1,128)
    if candidate not in numbersran:
        numbersran.append(candidate) 

listsdict = dict(zip(lst,numbersran)) #makes key, changes every time
print("Encytption key=", listsdict)
print()

filename=input("Enter File: ")
with open(filename, "r") as file:
    filetxt = file.read()
    ascii_codes = [ord(c) for c in filetxt]

encrypted_codes = [listsdict[code] for code in ascii_codes]
print('nEncrypted file: ',encrypted_codes)
cypher = "".join([chr(c) for c in encrypted_codes])    #encrypts from key

#decrypt below here into a file using the key to translate (should be original text from file)

I am not too sure how to reverse the process back to the original text in file. Must be decrypted back, can’t just be copied from og file

Asked By: Addapp

||

Answers:

To decrypt the file back to its original form, you will need to use the key that you created to map the encrypted code back to the original ascii code. To do this, you can use the dict() method to create a new dictionary that has the encrypted codes as the keys and the original ascii codes as the values. You can then use this dictionary to lookup the original ascii codes for each encrypted code in the encrypted file, and use the chr() method to convert those codes back into their corresponding characters.

Here is an example of how you could implement this:

# Create a new dictionary that maps the encrypted codes to the original ascii codes
decryption_key = dict((v,k) for k,v in listsdict.items())

# Use the decryption key to lookup the original ascii codes for each encrypted code
decrypted_codes = [decryption_key[code] for code in encrypted_codes]

# Use the chr() method to convert the ascii codes back into characters
decrypted_text = "".join([chr(c) for c in decrypted_codes])

# Save the decrypted text to a new file
with open("decrypted_file.txt", "w") as f:
    f.write(decrypted_text)
Answered By: AIGUY

The decryption process should be reverse of what has been done for encryption. Find below the code snippet.

decrypted_file = [ord(c) for c in cypher]
decrypted_key = list()
for value in decrypted_file:
    for key in listsdict:
        if listsdict[key] == value:
            decrypted_key.append(key)
            break
print(''.join([chr(c) for c in decrypted_key]))

There can be better ways to achieve the decryption but the solution in its naivest form should be like above.

easy way to replace this code snippet:

for value in decrypted_file:
    for key in listsdict:
        if listsdict[key] == value:
            decrypted_key.append(key)
            break

is to reverse listsdict keys with value as suggested by @scott hunter

decrypted_file = [ord(c) for c in cypher]
decrypted_key = list()
listsdict = dict(zip(numbersran,lst)) 
decrypted_key = [listsdict[code] for code in decrypted_file]
print(''.join([chr(c) for c in decrypted_key]))
Answered By: skedia
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.