-1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
LarryZ
  • 107
  • 2
  • 11

1 Answers1

1

Your error is unrelated to open. The error message says explicitly that the problem is with int function being called with 'P' as an argument. Since 'P' does not represent a binary number, int fails.

Błotosmętek
  • 12,717
  • 19
  • 29
  • Thanks, Blotosmetek!My native thinking has been that since I am opening in "rb", I should get nothing but binary value/numbers. This is not the case then what is inside the results from open file in "rb" ? – LarryZ Jun 07 '17 at 18:03
  • No, the difference is that if you open file in text mode (without `b`) then it is treated as a sequence of strings; if you open file in binary mode, it is treated as a sequence of `bytes`. This means that no charset decoding is done. – Błotosmętek Jun 07 '17 at 18:21
  • It looks like it returns a string of bytes. Each byte can be converted to int, but the string itself is not a number. Try looking at the `struct` module - https://docs.python.org/3/library/struct.html – OldGeeksGuide Jun 07 '17 at 18:22
  • 1
    @OldGeeksGuide: The elements of a byte string _are_ integers (although, as you say, the string itself is not). – martineau Jun 07 '17 at 18:29
  • Thanks for everyone's help. Now I can better understand a post I read before. [Link](https://stackoverflow.com/questions/20004859/python-how-to-read-raw-binary-from-a-file-audio-video-text) I looked into "struck", yet had a hard time to find the right "fmt" for my data. This trick (bin(int(binascii.hexlify(b), 16))[2:].zfill(8)) seemed get me what I need 8-digit 0 and 1. Now, the last questions, after I did the XoR to encrypt then another XoR to decrypt, how can I write them back using "wb" mode? – LarryZ Jun 08 '17 at 17:27