XOR encryption

J

joe jacob

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) ^ 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
 
M

Marc 'BlackJack' Rintsch

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.

You have to open the file in binary mode ('rb'/'wb') instead of text mode
('r'/'w') to stop Windows from interpreting the EOF character this way.

Ciao,
Marc 'BlackJack' Rintsch
 
J

joe jacob

You have to open the file in binary mode ('rb'/'wb') instead of text mode
('r'/'w') to stop Windows from interpreting the EOF character this way.

Ciao,
Marc 'BlackJack' Rintsch

Thanks a lot for the information now it is working fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top