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
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.