XOR encryption

joe jacob joejacob21 at gmail.com
Fri Jan 18 12:06:51 CET 2008


I wrote a python script to perform XOR encryption on a text and write
the encrypted text to a file. But when I try to read the file as the
encrypted text contains an EOF in between the file is read only to the
first EOF and remaining part of the text is not read.

 I used the text "hello world" and code 109. The ASCII value of w is
119 which is XOR ed with 109 gives EOF (ascii 26). So when I read the
file next time and decrypts it with 109; I'm getting result as "hello
" as the encrypted w is considered as EOF and remaining pare is not
read. I'm posting the code below. Please give me some inputs ot sort
this out.

name='a.txt'
f1=open(name,"w")
text='hello world'
data=[]
code=109
coded=""
for i in range(0,len(text)):
        coded = coded+chr(ord(text[i]) ^ code)
f1.write(coded)
print coded
f1.close()
f1=open(name,"r")
while 1:
        char = f1.read(1)
        if not char:
                break
        else:
                char = chr(ord(char) ^ int(code))
                data.append(char)
f1.close()
print data



More information about the Python-list mailing list