Skip to content

Instantly share code, notes, and snippets.

@physacco
Created April 4, 2013 16:41
Show Gist options
  • Save physacco/5311950 to your computer and use it in GitHub Desktop.
Save physacco/5311950 to your computer and use it in GitHub Desktop.
Decryption program for the game Twisty Destiny.
import os, sys
import StringIO
import time
import pdb
### init some paths
ScriptPath = os.path.abspath(__file__)
ScriptDir = os.path.dirname(ScriptPath)
InputDir = os.path.join(ScriptDir, "data")
OutputDir = os.path.join(ScriptDir, "decrypted")
def decodes(data):
c = 0
for i in xrange(len(data)):
byte = ord(data[i])
if i % 2:
yield byte ^ 0x39
else:
yield byte ^ 0x5B
def decode_stream(ins, outs):
ss = StringIO.StringIO()
[ss.write(chr(i)) for i in decodes(ins.read())]
outs.write(ss.getvalue())
def mkdir(path):
if not os.path.isdir(path):
mkdir(os.path.dirname(path))
os.mkdir(path)
def decrypt_file(infile):
print "Processing %s..." % infile
segs = infile.split('\\')[0:-1]
segs[2] = 'decrypted'
outpath = '\\'.join(segs)
if not os.path.isdir(outpath):
mkdir(outpath)
outfile = os.path.join(outpath, os.path.basename(infile))
t1 = time.time()
with open(infile, "rb") as inf:
with open(outfile, "wb") as outf:
decode_stream(inf, outf)
t2 = time.time()
elapsed = t2 - t1
print "\tOK. Time elapsed: %.3f secs" % elapsed
def decrypt_dir(dir):
dir = os.path.abspath(dir)
if not os.path.isdir(dir):
return
print "Enter directory %s..." % dir
for ent in os.listdir(dir):
ent_path = os.path.join(dir, ent)
if os.path.isfile(ent_path):
decrypt_file(ent_path)
elif os.path.isdir(ent_path):
decrypt_dir(ent_path)
else:
print "ERROR: " + ent_path
def main():
decrypt_dir(InputDir)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment