2

So this is a little bit of code that used to work on it's own, but when i tried to implement it into my code it gave me an error, i think it's because i mutated the bits inside of s2 is there any other way to XOR s3 without binascii.a2b_qp

def xor(s1,s2):
    s3 =''.join(chr(i^j) for i,j in zip(s1,s2))
    s4 = binascii.a2b_qp(s3)
    s5 = ''.join(chr(i^j) for i,j in zip(s2,s4))
    print(s5)

# for testing:
# additional info, s1 is a mutated form of bits from s2 using an S box
s1 = b'\xc3\xbf\x00\x00\xc3\xbf\x00\xc3\xbf\x00\x00'
s2 = b'aaaaaaaa'
xor(s1, s2)

Traceback:

Traceback (most recent call last): 
  File "C:\Users\Pavilion g7\workspace\Python Network\RW.py", line 138, in <module> 
    x= xor(m1,m2) 
  File "C:\Users\Pavilion g7\workspace\Python Network\RW.py", line 69, in xor 
    s4 = binascii.a2b_qp(s3) 
ValueError: string argument should contain only ASCII characters
glglgl
  • 89,107
  • 13
  • 149
  • 217
Newbie
  • 386
  • 4
  • 19
  • What input are you giving it that's triggering this error? – senshin Jan 02 '15 at 16:34
  • It's much more helpful if you can show the entire error trace, not just the error message. – Mark Ransom Jan 02 '15 at 16:35
  • 1
    @Newbie Was the code previously on Python 2 and now you're using it on Python 3? The behavior of `chr` changed between the two versions; in Python 3, it returns a `str` (i.e. unicode). – senshin Jan 02 '15 at 16:58
  • 1
    Wait, in `i^j`, are you xoring two strings? Does that even work? – NPE Jan 02 '15 at 16:58
  • nope i'm using python 3 since i started, below is the code i'm using implemented somewhere else – Newbie Jan 02 '15 at 17:03
  • s1 = b'abc' s2 = b'ebd' s3 =''.join(chr(i^j) for i,j in zip(s1,s2)) s4=binascii.a2b_qp(s3) s5 = ''.join(chr(i^j) for i,j in zip(s4,s2)) print("s5 = ",s5) >> s5 = abc – Newbie Jan 02 '15 at 17:04
  • What's the point of using `binascii.a2b_qp` at the first place? Is the string supposed to contain quoted-printable data? – glglgl Jan 02 '15 at 17:08
  • BTW, you should have only the one line in your function. The fiddling with the data should happen outside. – glglgl Jan 02 '15 at 17:08
  • i'm relatively new to python, this is a code i got from stackoverflow to xor a bstring, and if i don't use binascii i end up with this (PS i'm also asking for any substitution for binascii that would run the code): s5 = ''.join(chr(i^j) for i,j in zip(s2,s3)) TypeError: unsupported operand type(s) for ^: 'int' and 'str' – Newbie Jan 02 '15 at 17:15

1 Answers1

2

As was already commented, chr() returns a string.

In this question, you get advice about how to replace it.

Similiarly, you should not .join() with '', the empty string, but with b'', the empty bytes object.

Example:

def bchr(i):
    return bytes([i])

def xor(s1,s2):
    return b''.join(bchr(i ^ j) for i, j in zip(s1, s2))

s1 = b'\xc3\xbf\x00\x00\xc3\xbf\x00\xc3\xbf\x00\x00'
s2 = b'aaaaaaaa'
s3 = xor(s1, s2)
s4 = binascii.a2b_qp(s3)
s5 = xor(s2, s4)
print(s5)

s5 is still not s1, that's because s2 is too short. Replace it with

s2 = b'a' * len(s1)

and you are done. It now works as well with

s5 = xor(s2, s3)
print(s5 == s1) # -> True

thus removing the need for the binascii stuff altogether.

Community
  • 1
  • 1
glglgl
  • 89,107
  • 13
  • 149
  • 217
  • well i'm pretty sure that s1 and s2 have the same number of bits since when i implement the inverse Sbox s1 results in s2 – Newbie Jan 02 '15 at 17:28
  • block = b'aaaaaaaa' binary = getBin(block) s1 = initialPerm(binary) s2 = inversePerm(s1) m1= bitsToString(s1) m2= bitsToString(s2) print(m1) print(m2) b'\xc3\xbf\x00\x00\xc3\xbf\x00\xc3\xbf\x00\x00' b'aaaaaaaa' – Newbie Jan 02 '15 at 17:29
  • 11 bytes have 88 bits, 8 bytes have 64 bits. I don't know why `s1` is longer than `block`... – glglgl Jan 02 '15 at 17:33
  • Yeah your method works correctly, my method bitsToString added 3 bytes to my message i don't know how or why but when i switched the chr to bchr in it, it stopped giving that overhead so technically chr ruined 2 methods in my code thanks alot for the help i'm really new to python and i hassled you abit :) – Newbie Jan 03 '15 at 05:39