Skip to content

Instantly share code, notes, and snippets.

@NickBeeuwsaert
Created August 1, 2013 01:02
Show Gist options
  • Save NickBeeuwsaert/6127644 to your computer and use it in GitHub Desktop.
Save NickBeeuwsaert/6127644 to your computer and use it in GitHub Desktop.
Fun with one time pads!
#!/usr/bin/env python
import Image
import os, sys
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: %s secret pad output"%(sys.argv[0],))
sys.exit(0);
secret = Image.open(sys.argv[1])
secret.convert("RGB");
secretPixels = secret.load();
pad = Image.open(sys.argv[2])
pad.convert("RGB");
padPixels = pad.load();
if secret.size != pad.size:
print("Both images need to be the same size!");
sys.exit(0)
w, h = secret.size
output = Image.new("RGB", (w,h))
pixels = output.load()
for x in range(w):
for y in range(h):
s = list(secretPixels[x,y])
p = list(padPixels[x,y])
#if len(p) != 4:
# p+=[0]
#if len(s) != 4:
# s+=[255]
try:
pixels[x,y] = tuple([s[i] ^ p[i] for i in range(min(len(s),len(p)))]) #(s[0]^p[0],s[1]^p[1],s[2]^p[2],s[3]^p[3])
except IndexError:
print("%d %d"%(len(s), len(p)));
raise
output.save(sys.argv[3])
#!/usr/bin/env python
import Image
import os, sys
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: %s width height output"%(sys.argv[0],))
sys.exit(0)
width = int(sys.argv[1])
height = int(sys.argv[2])
outfile = sys.argv[3]
img = Image.new("RGBA", (width,height))
pixels = img.load();
w, h = img.size
for x in range(w):
for y in range(h):
pad = os.urandom(4);
pixels[x,y] = (ord(pad[0]), ord(pad[1]), ord(pad[2]), 255)#ord(pad[3]));
img.save(outfile);
#print(os.urandom(4).encode("hex"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment