Created
July 14, 2013 01:55
-
-
Save rikaardhosein/5992892 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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