So I'm trying to learn python and I came up with an idea for a little program that reads a text file, converts all of the characters into pseudo-random ones, and then creates a key to return it to human readable form. The issue is that, when I decode the message sometimes one character will get decoded incorrectly. So all 'a' characters might show up as '0', or 'M', etc.
any help or general python advice is greatly appreciated :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
import random import pickle import copy """ Encodes and decodes files """ def load(string): """ function that converts characters to pseudo-random characters and creates a key to decipher later. """ ## APPEND PATH HERE* ## opens file to store converted data in datafile = open ( '/your/path/here/data.dep' , 'wb' ) ## oens file to store key in keyfile = open ( '/your/path/here/key.dep' , 'wb' ) ## defines some default values key = [] user_string = [] working_pool = {} out_key = {} new_string = [] ## character map, gets copied to create character pools all_char = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '!' , '@' , '#' , '$' , '%' , '^' , '&' , '*' , '+' , '-' , '_' , '=' , '~' , ' ' , '.' , ',' , ';' , '(' , ')' , '<' , '>' , '?' , ':' , '|' , '/' , '~' , '[' , ']' , '{' , '}' , '\'' , '"' , '\t' , '\n' , '\\' ] ## above list copied char_copy = copy.deepcopy(all_char) pool = copy.deepcopy(all_char) ## for each character in the character map, for char in char_copy[:]: ## pick a random character from pool temp_char = random.choice(pool) ## appends working_pool dictionary ## {'random': 'actual'} EXAMPLE working_pool[temp_char] = char ## appends my_key dictionary, creates key ## {'actual': 'random'} EXAMPLE out_key[char] = temp_char ## remove the random character assigned ## from the pool so it cant be picked twice pool.remove(temp_char) ## opens user defined file supplied by function parameter with open (string) as infile: ## while true, examine each character while True : ## append 'user_string' with each character in file ch = infile.read( 1 ) user_string.append(ch) ## if the character is in the conversion pool if ch in working_pool: ## convert character new_string.append(working_pool[ch]) else : ## keep character as is new_string.append(ch) ## repeat until EOF if not ch: eof = 'true' break ## start DEBUG #test = ''.join(str(z) for z in new_string) #ustring = ''.join(str(y) for y in user_string) #print(test) #print('\n') #print(ustring) #print(working_pool) ## end DEBUG ## serializes converted string and stores it pickle.dump(new_string, datafile) pickle.dump(out_key, keyfile) datafile.close() keyfile.close() def unload(data, key): """ function that, given the correct keyfile, will translate converted string to human readable clear text """ ## opens files supplies by function parameter input_file = open (data, 'rb' ) decoder = open (key, 'rb' ) active = True ## while active is true while active: try : ## dump data into 'contents' until EOF contents = pickle.load(input_file) except EOFError: ## if EOF, active is false active = False ## same as above but for decoder active = True while active: try : load_key = pickle.load(decoder) except EOFError: active = False ## sets empty list for converting converted_string = [] ## for each value in the encrypted data, for ch in contents[:]: ## if the value is in the key, if ch in load_key: ## add the decoded character to converted_string converted_string.append(load_key[ch]) #else: #converted_string.append(ch) clear_text = ''.join( str (e) for e in converted_string) ## start DEBUG #print('\n') #print(load_key) ## end DEBUG ## print unencrypted results print (clear_text) input_file.close() decoder.close() ## CALL FUNCTIONS HERE load( '/fake/data/someFile.txt' ) unload( '/your/path/here/data.dep' , '/foo/bar/key.dep' ) |