I am trying to encrypt a file input by user into a randomized key of ascii. I got the random key and can translate the string in the file into ascii but cannot figure out how to translate it into the key
I tried many things but am not good enough in python to understand what else to try/ what I could be doing wrong
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 the key
print("Encytption key=", listsdict)
print()
filename=input("Enter File: ")
file=open(filename, "r")
filetxt=file.readlines()
file.close() #for user input file
with open('file name here','r') as f: #also cant figure out how to make this work with user inputted file
for x in f:
arr=list(x)
ctd=list(map(str,map(ord,arr)))
filename=filename.replace('.txt','')
encrypt=open(filename,"w")
#below is for the encryption process
CodePudding user response:
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)
random.shuffle(lst)
listsdict = dict(zip(lst, range(1,128))) #makes the key
print("Encytption key=", listsdict)
print()
filename=input("Enter File: ")
# file=open(filename, "r")
# filetxt=file.readlines()
# file.close()
with open(filename, "r") as file:
filetxt = file.read()
ascii_codes = [ord(c) for c in filetxt]
# with open('file name here','r') as f: #also cant figure out how to make this work with user inputted file
# for x in f:
# arr=list(x)
# ctd=list(map(str,map(ord,arr)))
# listdict is the key!
encrypted_codes = [listsdict[code] for code in ascii_codes]
print(encrypted_codes)
cypher = "".join([chr(c) for c in encrypted_codes])
print(cypher)
# filename=filename.replace('.txt','')
# encrypt=open(filename,"w")
#below is for the encryption process