Answer the question
In order to leave comments, you need to log in
Can you please explain the python code?
import itertools
def XOR_cipher(string, key):
answer = []
key = itertools.cycle(key) # Повторяем ключ, чтобы зашифровать всю строку
for s, k in zip(string, key):
answer.append(chr(ord(s) ^ ord(k)))
return ''.join(answer)
# Функция для расшифровки точно такая же
XOR_uncipher = XOR_cipher
Answer the question
In order to leave comments, you need to log in
itertools.cycle(key) returns an iterator in which characters from key are cycled in turn (that is, if the iteration reaches the last character from key, then it will return the first character from key next, and so on without end).
zip(string, key) returns an iterator of tuples, each iteration taking the elements from each argument (string and key in this case) and returning a tuple of those elements until there are elements left in all iterators.
Inside the loop, one character of the encrypted text is taken, one character of the key, their codes are taken, XOR of these codes is found, and a character with the resulting code is added to the list.
At the end, all the characters of the list are simply glued into one line.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question