For uses in encryption, type:
python cripto.py -e image.png
Where image.png is the name of the image where the code is going to encrypt.
For use in decryption, type:
python cripto.py -d image.png
Where image.png is the name of the image where the code is going to be extracted.
The images with code embedded are: 2, 3, 5.
The images with code embedded are: 2, 3, 5.
The code used to encrypt-decipher the messages from the images is the following:
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 Image | |
import sys | |
def serie(x): | |
return (x + 21) | |
def convert_bin(msg): | |
bin_str = "" | |
for letter in msg: | |
bin_str += bin(abec.index(letter))[2:].zfill(len(bin(len(abec))[2:])) | |
return bin_str | |
def decodify(bin_str): | |
letter = "" | |
msg = "" | |
for i in range(len(bin_str)): | |
letter += bin_str[i] | |
if len(letter) == len(bin(len(abec))[2:]): | |
if abec[int(letter, 2)] == '\0': | |
return msg | |
msg += abec[int(letter, 2)] | |
letter = "" | |
return msg | |
mode = sys.argv[1] | |
name = sys.argv[2] | |
abec = ['c', 'P', 'U', 'x', ':', 'f', 'R', 'E', 'a', '"', 'q', '7', 'g', 'T', '+', 'Q', 'o', '#', ',', '2', 'b', 'h', 'k', '\n', 'Z', '\0', 'l', ']', '%', 'W', 'G', 'z', 'd', 's', '0', 'u', '-', 'O', ' ', '=', 'J', 'I', '\t', 'C', '.', 'N', '1', 'K', 'D', 'B', '5', 'F', '>', 'I', 'y', 'H', 'w', 'L', "'", 't', 'A', '\\', 'j', 'X', 'i', 'v', 'P', 'r', '_', '3', ')', 'n', 'm', 'M', '!', 'V', 'Y', '*', '[', '9', 'e', '(', 'p'] | |
im = Image.open(name) | |
pix = im.load() | |
width, height = im.size | |
f = open(sys.argv[0], 'r') | |
msg = f.read() + '\0' | |
cont = 0 | |
if mode == "-e": | |
print "Encrypting %s ..."%name | |
msg = convert_bin(msg) | |
bit = 0 | |
for i in range(width): | |
for j in range(height): | |
new = [0, 0, 0] | |
old = pix[i, j] | |
if cont == serie(bit): | |
for k in range(len(pix[i, j])): | |
curr = bin(pix[i, j][k])[2:].zfill(len(bin(len(abec))[2:])) | |
for m in range(1, 3): | |
curr = list(curr) | |
curr[len(curr)-m] = msg[bit] | |
curr = "".join(curr) | |
new[k] = int(curr, 2) | |
bit += 1 | |
if bit >= len(msg): | |
pix[i, j] = (new[0], new[1], new[2]) | |
im.save("output_%s"%name.replace(".jpg", "")) | |
print "Done!" | |
sys.exit() | |
pix[i, j] = (new[0], new[1], new[2]) | |
else: | |
cont += 1 | |
elif mode == "-d": | |
bit = 0 | |
binary_str = "" | |
cont = 0 | |
print "Deciphering %s ..."%name | |
try: | |
for i in range(width): | |
for j in range(height): | |
if cont == serie(bit): | |
for k in range(len(pix[i, j])): | |
curr = bin(pix[i, j][k])[2:].zfill(len(bin(len(abec))[2:])) | |
for m in range(1, 3): | |
curr = list(curr) | |
binary_str += curr[len(curr) - m] | |
bit += 1 | |
else: | |
cont += 1 | |
print decodify(binary_str) | |
sys.exit() | |
except IndexError: | |
print "Ups. Not here" |
Note: The Images have to be clicked and downloaded from the URL(from the Gallery), or else they will be downloaded in a different size and the data will be lost.
Muy bien; 10.
ResponderEliminar