I just started to learn Python 2.X. I am trying to encrypt a file using Python. Professional users all are using pycrpto, while I am just doing a simple project to explore some of the basics in python's open file in "rb". Here is the code:
def BinXor(tBIN, kBIN):
tLength = len(tBIN)
kLength = len(kBIN)
if tLength > kLength:
while True:
kBIN = kBIN + kBIN
if not len(kBIN) < tLength:
break
kBINXOR = kBIN[:tLength]
return "".join(["%x" % (int(x, 2) ^ int(y, 2)) for (x, y) in
zip(tBIN,kBINXOR)])
def question():
t = open("b.xlsx",'rb').read()
k = '00101101'
print BinXor(t,k)
The error message is:
File "....py", line 27, in BinXor
return "".join(["%x" % (int(x, 2) ^ int(y, 2)) for (x, y) in zip(tBIN,kBINXOR)])
ValueError: invalid literal for int() with base 2: 'P'
The error is the same if I remove "b" from the open file statement. I have been reading couple of related questions here but still could not get a clue. Can anyone help me here? Why my code is not working? What is exactly inside the result of a file opened in 'rb' mode?