Created
July 9, 2013 07:20
-
-
Save soul0592/5955341 to your computer and use it in GitHub Desktop.
Implementacion de esteganografia
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
from PIL import Image | |
from sys import argv | |
def convertBin(caracter): | |
bin = '' | |
dec = ord(caracter) | |
while dec > 0: | |
bin = str(dec % 2) + bin | |
dec >>= 1 | |
while len(bin) < 8: | |
bin = '0' + bin | |
return bin | |
def convertChar(bits): | |
return chr(int(bits,2)) | |
def ocultar(data): | |
mensaje = raw_input('Mensaje a ocultar: ') | |
bloques = convertBin(str(len(mensaje))) | |
bits = bloques | |
for caracter in mensaje: | |
bits += convertBin(caracter) | |
(w,h) = data.size | |
pixel = data.load() | |
res = Image.new('RGB',(w,h)) | |
nuevo = res.load() | |
i = 0 | |
for x in xrange(w): | |
for y in xrange(h): | |
r,g,b = pixel[x,y] | |
if i < len(bits): | |
if int(bits[i]) == 1: | |
if r % 2 == 1 and r < 255: | |
r += 1 | |
elif r % 2 == 1 and r == 255: | |
r -= 1 | |
else: | |
if r % 2 == 0: | |
r += 1 | |
i += 1 | |
nuevo[x,y] = r,g,b | |
res.save('nuevo.png') | |
print 'Mensaje ocultado.' | |
#data.show() | |
#res.show() | |
def obtenerBloques(data): | |
(w,h) = data.size | |
pixel = data.load() | |
bloqbin = '' | |
for x in xrange(w): | |
for y in xrange(h): | |
r,g,b = pixel[x,y] | |
if y < 8: | |
if r % 2 == 0: | |
bloqbin += '0' | |
else: | |
bloqbin += '1' | |
else: | |
return int(bloqbin,2) | |
def recuperar(data): | |
print 'Recuperando mensaje...' | |
(w,h) = data.size | |
pixel = data.load() | |
num = '' | |
mensaje = '' | |
bloques = obtenerBloques(data)+1 | |
c = 0 | |
for x in xrange(w): | |
for y in xrange(h): | |
r,g,b = pixel[x,y] | |
if c < bloques: | |
if r % 2 == 0: | |
num += '1' | |
else: | |
num += '0' | |
if len(num) % 8 == 0: | |
if c != 0: | |
mensaje += convertChar(num[c*8:(c+1)*8]) | |
c += 1 | |
else: | |
return mensaje | |
def main(): | |
try: | |
imagen = argv[2] | |
if argv[1] == 'o': | |
data = Image.open(imagen).convert('RGB') | |
ocultar(data) | |
elif argv[1] == 'r': | |
new = Image.open(imagen).convert('RGB') | |
mensaje = recuperar(new) | |
print 'Mensaje recuperado: ',mensaje | |
except: | |
data = Image.open('mustang.png').convert('RGB') | |
ocultar(data) | |
new = Image.open('nuevo.png').convert('RGB') | |
mensaje = recuperar(new) | |
print 'Mensaje recuperado: ',mensaje | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment