Skip to content

Instantly share code, notes, and snippets.

@rspencer01
Created January 20, 2018 15:24
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 rspencer01/7f6b9a451f13b7f799b0e626695d6c25 to your computer and use it in GitHub Desktop.
Save rspencer01/7f6b9a451f13b7f799b0e626695d6c25 to your computer and use it in GitHub Desktop.
Breaks a message encrypted with Cape
enc = open('enc').read().split(',')
for i in xrange(len(enc)):
enc[i] = int(enc[i][2:],16)
length = len(enc)-1
ALPHABET = '0.123456789[],\'positions:{}\n '
def all_choices_for(x):
if len(x)>1:
for i in x[0]:
for j in list(all_choices_for(x[1:])):
yield [i] + j
elif len(x) ==1:
for i in x[0]:
yield [i]
for key_length in xrange(1,20):
print "Trying key length",key_length
for srk in xrange(256):
key_options = []
for i in xrange(key_length):
key_options.append([])
for option in xrange(256):
if i == (length ^ srk) % key_length:
if option != length ^ enc[-1]:
continue
good = True
for j in xrange(length):
if i == (srk^j) % key_length:
dec = enc[j] ^ j ^ option
if chr(dec) not in ALPHABET:
good = False
if not good:
continue
key_options[-1].append(chr(option))
if key_options[-1] == []: break
if 0 in map(len,key_options):
continue
if all(map(lambda x: len(x)<=2, key_options)):
for key in all_choices_for(key_options):
message= ''
for i in xrange(length):
message += chr(enc[i] ^ i ^ ord(key[(srk^i)%key_length]))
print "Possible message:"
print message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment