I appologize for the length of this post, but I have no clue what is wrong with my program. As an experiment, I wrote a XOR encryption program. It isnt very fast, but it works. Apparently, only for strings. I implemented a file encryption function, and it seems that some characters come out slightly different (As in the ord() function returns numbers that are only slightly different). I have tested the same string as contained in the file using the string encoder, and it works fine. Any help is greatly apppriciated. My code is attached below, Along with a couple text files. The unmodified text is in unmodified.txt, the encrypted text is in encrypted.txt, and the decrypted text is in decrypted.txt. There is also errors.txt, which shows which characters are off and by how much. Again, All help is greately appriciated.
from __future__ import division
from math import ceil
def xor_tuple(message, password, type): #XOR a few characters
len_mess, len_pass = len(message), len(password)
if len_mess != len_pass: raise message + ' and ' + password + ' are not same lengths!' #Not same length strings
result = [] #Empty list for result
for x in range(0, len_mess, 1): #Cycle through characters and XOR them, then append them to the list
if type == 'chr': #Outputs characters
result.append(chr(ord(message[x]) ^ ord(password[x])))
if type == 'int': #Outputs a list of numbers, the result of ord(char)
result.append(ord(message[x]) ^ ord(password[x]))
return result
def crypt(message='message', password='password', type='chr', input=''): #Encrypt/Decrypt function
#Type - chr returns characters, int returns a list of numbers
#Input - Leave blank, unless inputting a list of numbers, then change to int
if type != 'chr' and type != 'int': raise type + " is not a type. Accepted types are 'int' or 'chr'." #Error checking
if input != 'int' and input != '': raise input + " is not an input. Accepted inputs are 'int' or ''."
if input == 'int': #Make sure a list of numbers is passed, and if not, leave it alone
try:
message[0] + 1
temp = ''
for char in message: temp = temp + chr(char)
message = temp
except: pass
len_mess, len_pass = len(message), len(password)
passes = int(ceil(len_mess/len_pass)) #How many times the password must be repeated throughout the message
crypted = [] #Empty list for result
for x in range(0, passes, 1): #Cylces through the message
temp = xor_tuple(message[x*len_pass:x*len_pass+len_pass], password[0:len(message[x*len_pass:x*len_pass+len_pass])], type)
for item in temp: #Appends every number individually
crypted.append(item)
if type == 'int': return crypted
crypted2 = ''
for char in crypted: crypted2 = crypted2 + char
return crypted2
def crypt_file(filename, password): #Password is a string, filename is a file
if filename[len(filename)-9:len(filename)] == 'encrypted': #We are decrypting
infile = open(filename, 'U') #Open the encrypted file
outfile = open(filename[:len(filename)-10], 'wb') #Create the decrypted file without the .encrypted suffix
else: #We are encrypting
infile = open(filename, 'U') #Open the unencrypted file
outfile = open(filename+'.encrypted', 'wb') #Create the encrypted file
infile_data = '' #Everything else remains the same
for char in infile: infile_data = infile_data + char #Dump the data of the file to a string
outfile.write(crypt(infile_data, password, 'chr')) #Actual encrypting/decrypting
infile.close() #Close the files
outfile.close()