Skip to content

Instantly share code, notes, and snippets.

@tuankiet65
Created May 6, 2014 05:19
Show Gist options
  • Save tuankiet65/dac87f60d43aad77841a to your computer and use it in GitHub Desktop.
Save tuankiet65/dac87f60d43aad77841a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# A04 Decoder Logic by monocle with modifications from tuankiet65
import hashlib
import sys
from subprocess import check_output
f=open(sys.argv[1] + ".txt", 'r')
string = f.read()
f.close()
qr = check_output(['zbarimg', '--raw', '-q', sys.argv[1]+'.png'])
key = qr.replace('\n', '')
string = string.replace('\n', ' ')
string = string.replace('\r', ' ')
string = string.replace(' ', ' ')
asci = " aeiouqwrtypsdfghjxklzcvbnm1234567890',.?!@/"
#print string
string = string.split(" ")
decoded = ""
strlen = len(string)
swap = True
for x in string:
notFound = True;
print "Decoding", x
for i in asci:
#print "Matching", string[(strlen - 1) - i], "to", string[(strlen - 2) - i]
for a in asci:
if swap:
m = hashlib.sha1();
m.update(i+a+key);
if m.hexdigest()[:20] == x:
print "Unswapped\t", m.hexdigest(), i, a
#print "D3"
#print "Found match:", string[strlen - i], "=", a;
decoded = decoded + i + a;
swap = False
notFound = False
break # r.i.p semicolon
else:
m = hashlib.sha1();
m.update(i+a+key);
if m.hexdigest()[20:] == x:
print "Swapped\t", m.hexdigest(), i, a
#print "D3"
#print "Found match:", string[strlen - i], "=", a;
decoded =decoded + i + a;
swap = True
notFound = False
break;
if notFound:
decoded = i + a + decoded
for i in asci:
decoded = decoded.replace(i+i, i)
decoded = decoded.replace("/", "");
print sys.argv[1]+" message: ",decoded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment