moth/puzzles/steg/40/index.exe

61 lines
1.3 KiB
Plaintext
Raw Normal View History

2010-10-06 17:20:00 -06:00
#! /usr/bin/python
import os
import sys
import md5
import cStringIO as StringIO
key = 'breadfruit chawbacon'
src = os.path.dirname(sys.argv[0])
class md5file:
def __init__(self, fn='outf.bin'):
self.fn = fn
self.f = open(fn, 'wb')
self.m = md5.new()
def write(self, s):
self.f.write(s)
self.m.update(s)
def close(self):
self.f.close()
os.rename(self.fn, self.m.hexdigest())
os.system('wget -O lena.tif http://www.cs.cmu.edu/~chuck/lennapg/lena_std.tif')
inf = os.popen('tifftopnm lena.tif', 'rb')
version = inf.readline()
dimensions = inf.readline()
depth = inf.readline()
# Read in plaintext
noise = open(os.path.join(src, 'noise.txt')).read()
noise += '\n\nThe key is "%s".\n' % key
noise = StringIO.StringIO(noise)
# Set the low-order bits in output file
outf = md5file()
outf.write(version)
outf.write(dimensions)
outf.write(depth)
i = 0
while True:
c = noise.read(1)
if c:
c = ord(c)
else:
c = 0
img_bytes = inf.read(8)
if not img_bytes:
break
for j in range(8):
bit = 7 - j
img_byte = ord(img_bytes[j]) & 0xFE
noise_bit = (c & (1<<bit)) >> bit
out_byte = img_byte | noise_bit
outf.write(chr(out_byte))
outf.close()
open('key', 'w').write(key)
os.unlink('lena.tif')