[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

XOR encryption

joe jacob

1/18/2008 11:07:00 AM

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
2 Answers

Marc 'BlackJack' Rintsch

1/18/2008 11:11:00 AM

0

On Fri, 18 Jan 2008 03:06:51 -0800, joe jacob wrote:

> 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

joe jacob

1/18/2008 11:16:00 AM

0

On Jan 18, 4:11 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> On Fri, 18 Jan 2008 03:06:51 -0800, joe jacob wrote:
> > 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

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