Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 6, 2018 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/49066558d03445d8870bd21e157a16a2 to your computer and use it in GitHub Desktop.
Save parzibyte/49066558d03445d8870bd21e157a16a2 to your computer and use it in GitHub Desktop.
"""
Leer mensaje en una imagen utilizando esteganografía, leyendo un bit en cada nivel de color
Nota: recuerda instalar Pillow:
pip install Pillow
@author parzibyte
@date 06-04-2018
@web parzibyte.me/blog
"""
from PIL import Image
caracter_terminacion = "11111111"
def obtener_lsb(byte):
return byte[-1]
def obtener_representacion_binaria(numero):
return bin(numero)[2:].zfill(8)
def binario_a_decimal(binario):
return int(binario, 2)
def caracter_desde_codigo_ascii(numero):
return chr(numero)
def leer(ruta_imagen):
imagen = Image.open(ruta_imagen)
pixeles = imagen.load()
tamaño = imagen.size
anchura = tamaño[0]
altura = tamaño[1]
byte = ""
mensaje = ""
for x in range(anchura):
for y in range(altura):
pixel = pixeles[x, y]
rojo = pixel[0]
verde = pixel[1]
azul = pixel[2]
byte += obtener_lsb(obtener_representacion_binaria(rojo))
if len(byte) >= 8:
if byte == caracter_terminacion:
break
mensaje += caracter_desde_codigo_ascii(binario_a_decimal(byte))
byte = ""
byte += obtener_lsb(obtener_representacion_binaria(verde))
if len(byte) >= 8:
if byte == caracter_terminacion:
break
mensaje += caracter_desde_codigo_ascii(binario_a_decimal(byte))
byte = ""
byte += obtener_lsb(obtener_representacion_binaria(azul))
if len(byte) >= 8:
if byte == caracter_terminacion:
break
mensaje += caracter_desde_codigo_ascii(binario_a_decimal(byte))
byte = ""
else:
continue
break
return mensaje
mensaje = leer("salida.png")
print("El mensaje oculto es:")
print(mensaje)
@c04tl
Copy link

c04tl commented Jul 3, 2018

Hola, gran trabajao, me pregunto si los bits de finlaizacion tienen que llevar siempre esa forma, es decir 1 byte de 1's

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment