Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Created July 25, 2015 21:41
Show Gist options
  • Save jdiez17/1b7a1a5c07b51d3ecd00 to your computer and use it in GitHub Desktop.
Save jdiez17/1b7a1a5c07b51d3ecd00 to your computer and use it in GitHub Desktop.
def cifra(a, b, key, num_bytes=2):
assert len(a) == len(b)
l = len(a)
#padding
if len(a)%num_bytes:
a += chr(0)*((-len(a))%num_bytes)
b += chr(0)*((-len(b))%num_bytes)
assert len(a)%num_bytes == 0
assert len(b)%num_bytes == 0
a_chunks = [a[x:x+num_bytes] for x in range(0,len(a),num_bytes)]
b_chunks = [b[x:x+num_bytes] for x in range(0,len(b),num_bytes)]
ret = ""
for pos in range(len(a_chunks)):
a_chunk_n = str2n(a_chunks[pos])
b_chunk_n = str2n(b_chunks[pos])
n = (256**num_bytes - 1) & ((a_chunk_n^b_chunk_n) + key*(pos+1))
ns = n2str(n, num_bytes)
ret += ns
#quita padding
ret = ret[:l]
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment