Skip to content

Instantly share code, notes, and snippets.

@dhrumilp15
Last active April 13, 2021 00:06
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/299ba1d8d1522d05755f9dcebfd1d6bc to your computer and use it in GitHub Desktop.
Save dhrumilp15/299ba1d8d1522d05755f9dcebfd1d6bc to your computer and use it in GitHub Desktop.
def rev_multiply(ciphertexts):
candidates, noncandidates = condition1(ciphertexts)
gcdd, key_scaler, _ = egcd(diff, 1 << 64)
keycounter = Counter()
for cand in candidates:
ci1, ci2 = map(int, cand, repeat(16))
if (ci1 + ci2) % 2 != 0 or ci1 ^ ci2 == diff:
continue
kodd = (((ci1 + ci2) // gcdd) * key_scaler) % (1 << 64)
kcomp = int(''.join(comp(list(f'{kodd:064b}'), 0)), 2)
keycounter[kodd] += 1
keycounter[kcomp] += 1
return keycounter.most_common(1)
def crack_subkey(ciphertexts):
conf = 0
k_pair = rev_multiply(ciphertexts)
if len(k_pair) < 1:
return None
k_odd, conf = k_pair[0]
# Cracking the subkey is a deterministic process
# If the confidence in the calculated subkey is too low, we have to try
# different keys
if conf <= 1:
return None
kcomp = int(''.join(comp(list(f'{k_odd:064b}'), 0)), 2)
return [k_odd, kcomp, k_odd ^ 1, kcomp ^ 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment