Wpis z mikrobloga

Mozna ten kod jakos prosciej napisac, niz sam to zrobilem?
Dekodowanie i kodowanie XOR.

from textwrap import wrap
def xor_stringsDec(s, t):
ul=''
s=wrap(s.decode('utf8'), 2)

for sx in s:
cv=int(sx,16)
for x in t:
cv = cv^x
ul+=chr(cv)
return ul

def xor_stringsEnc(s, t):
ul=''
for sx in s:
cv=sx
for x in t:
cv = cv^x
z=hex(cv)
ul+=str(hex(cv)[2:])
return ul
messencod = '66687a7e613f617d'
key = b'biauek'
print (xor_stringsDec(messencod.encode('utf8'), key))
mess_orig = 'wykop.pl'
print (xor_stringsEnc(mess_orig.encode('utf8'), key))

#python #programowanie
kanasta - Mozna ten kod jakos prosciej napisac, niz sam to zrobilem?
Dekodowanie i ko...

źródło: xor

Pobierz
  • 5
mały refactor, żeby było czytelniej:

def xor_stringsDec(s, key):
encodedLetters = map(lambda hex: int(hex, 16), wrap(s.decode('utf8'), 2))
decodeLetter = lambda letter: chr(functools.reduce(operator.xor, key, letter))
return ''.join(map(decodeLetter, encodedLetters))
@kanasta: @ponton:

from functools import reduce
import operator

def xor_strings_dec(message, key):
return bytes(
reduce(operator.xor, key, char)
for char in bytes.fromhex(message)
).decode()

def xor_strings_enc(message, key):
return bytes(
reduce(operator.xor, key, char)
for char in message.encode()
).hex()

key = b'biauek'
print(xor_strings_dec('66687a7e613f617d', key))
print(xor_strings_enc('wykop.pl', key))