Skip to content

Instantly share code, notes, and snippets.

@rikaardhosein
Created July 14, 2013 01:55
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 rikaardhosein/5992892 to your computer and use it in GitHub Desktop.
Save rikaardhosein/5992892 to your computer and use it in GitHub Desktop.
import hashlib, string, sys
def h(x):
x = hashlib.sha256(x).digest()
x = xor(x[:16], x[16:])
return x
def xor(a, b):
l = min(len(a), len(b))
return ''.join([chr(ord(x) ^ ord(y)) for x, y in zip(a[:l], b[:l])])
def crypt(msg, k):
out = ''
for i in xrange(0, len(msg), 16):
out += xor(msg[i:i+16], k)
k = h(k + str(len(msg)))
return out
def verify(x):
return all([ord(c) < 127 for c in x])
#plaintext = 'From: Baron van '
plaintext = 'From: Vlugge Jap'
print len(plaintext)
#get first 16 bytes from msg002.enc
file_contents = open('msg002.enc','r').read().decode('base64')
ciphertext = file_contents[0:16]
key = xor(plaintext,ciphertext)
out = crypt(file_contents,key)
if verify(out):
print out
else:
print 'Bad decrypt!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment