Skip to content

Instantly share code, notes, and snippets.

@dhrumilp15
Created April 13, 2021 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrumilp15/1a38e48e4db169f79c21603708c6b799 to your computer and use it in GitHub Desktop.
Save dhrumilp15/1a38e48e4db169f79c21603708c6b799 to your computer and use it in GitHub Desktop.
def recurse(plaintexts, ciphertexts, kcands, keyers=[], depth=1):
if depth > 5:
return None, None
for k in kcands:
dciphertexts = ciphertexts[:]
# DECRPYT CIPHERTEXTS WITH THE CURRENT KEY
for pair in range(len(dciphertexts)):
ci1, ci2 = map(int, dciphertexts[pair], repeat(16))
# undo modular multiplication by odd-ed key, reversal,
# xor by guessed key
ci1 = g((ci1 * pow(k | 1, -1, 1 << 64)) % (1 << 64)) ^ k
ci2 = g((ci2 * pow(k | 1, -1, 1 << 64)) % (1 << 64)) ^ k
dciphertexts[pair] = [hex(ci1)[2:], hex(ci2)[2:]]
# If the decrypted ciphertexts are the same as the plaintext, we're done!
if all([int(dciphertexts[i][j], 16) == int(plaintexts[i][j], 2)
for j in range(0, 2) for i in range(len(dciphertexts))]) == True:
return dciphertexts, [k] + keyers
# SOLVE FOR THE NEXT ODD-ED KEY
dkcands = crack_subkey(dciphertexts)
if dkcands is None:
continue
v = recurse(plaintexts, dciphertexts,
dkcands, [k] + keyers, depth + 1)
if v is not None:
return v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment