LSB隐写术-解码功能故障

时间:2018-10-24 16:31:22

标签: python-3.x

我正在进行LSB隐写术,对于字符串的每个字符,我将其转换为二进制,然后将每一位分别转换为红色,绿色,蓝色像素。因此,每个角色占用几乎2个像素半(第3个像素的红色和绿色)。

在编码时,它可以完美工作,但在解码时,它不仅仅适用于前两个角色。

我该如何解决?

def encode_image(img, msg):
"""
mettre les quantites du (r,v,b) dans un tuple
cacher le message sous carateres binaires
"""
#Longueur du message: L
L = len(msg) + 1
# longueur max du message (255 car)
if L > 255:
    print("texte trop long! (ne pas depasser 255 caracteres)")
    return False
if img.mode != 'RGB':
    print("l image doit etre de type RVB")
    return False
# on utilise une copie de l image pour cacher le texte 
encode = img.copy()
row=column=0
colonne, ligne = img.size

r, v, b = img.getpixel((column, row))
encode.putpixel((column, row), (L, v , b))

for c in (msg):
    print ("\n\t\t\t\tCaractèr:\t ", c)
    binary_char = bin(ord(c))[2:]
    if (len(binary_char) != 8):
        difference = 8 - len(binary_char)
        x = 0
        for x in range(0, difference):
            binary_char = "0" + binary_char
    print ("\n\t\t\t\tCaractère binaire:\t ", binary_char)
    i = 0
    pic = 0;
    column+=1
    if(column == colonne):
        row+=1
        column=0
    for char in (binary_char):
        r, v, b = img.getpixel((column, row))
        print("\nRouge=\t",r,"\nVert=\t",v,"\nBleu=\t",b)

        binary_red = bin(r)[2:]
        if (len(binary_red) != 8):
            difference = 8 - len(binary_red)
            x = 0
            for x in range(0, difference):
                binary_red = "0" + binary_red
        binary_blue = bin(b)[2:]
        if (len(binary_blue) != 8):
            difference = 8 - len(binary_blue)
            x = 0
            for x in range(0, difference):
                binary_blue = "0" + binary_blue
        binary_vert = bin(v)[2:]
        if (len(binary_vert) != 8):
            difference = 8 - len(binary_vert)
            x = 0
            for x in range(0, difference):
                binary_vert = "0" + binary_vert
        print("\nBinary Rouge=\t",binary_red,"\nBinary 
Vert=\t",binary_vert,"\nBinary Bleu=\t",binary_blue)

        if( i % 3 == 0):
            new_binary_red = binary_red[:7] + char
            new_red = int(new_binary_red, 2)
            pic+=1
            i+=1
            print("\nNew binary Rouge=\t",new_binary_red)
            print("\nNew Rouge=\t",new_red)
        elif( i % 3 == 1):
            new_binary_vert = binary_vert[:7] + char
            new_vert = int(new_binary_vert, 2)
            pic+=1
            i+=1
            print("\nNew binary Vert=\t",new_binary_vert)
            print("\nNew Vert=\t",new_vert)
        elif( i % 3 == 2):
            new_binary_blue = binary_blue[:7] + char
            new_blue = int(new_binary_blue, 2)
            pic+=1
            i+=1
            print("\nNew binary Bleu=\t",new_binary_blue)
            print("\nNew Bleu=\t",new_blue)

        print("\nPixel=\t",pic,"\nLigne=\t",row,"\nColonne=\t",column)
        if( pic == 3):
            encode.putpixel((column, row), (new_red, new_vert, new_blue))
            pic = 0
            column+=1
            if(column == colonne):
                row+=1
                column = 0
return encode



def decode_image(img):

colonne, ligne = img.size
#print("\nrow:\t", ligne,"\ncolonne:\t",colonne)
i=j=compteur=0
r,v,b = img.getpixel((j,i))
L=r - 1 
msg = ""
c=''

for compteur in range(L):
    pic = 0;
    j+=1
    if(j == colonne):
        i+=1
        j=0
    char_binaire = ""
    taille = len(char_binaire)
    for taille in range(0, 8):
        print("\nrow:\t", i,"\ncolonne:\t",j)
        r,v,b = img.getpixel((j,i))
        print("\nRouge=\t",r,"\nVert=\t",v,"\nBleu=\t",b)
        binary_red = bin(r)[2:]
        if (len(binary_red) != 8):
            difference = 8 - len(binary_red)
            x = 0
            for x in range(0, difference):
                binary_red = "0" + binary_red
        binary_blue = bin(b)[2:]
        if (len(binary_blue) != 8):
            difference = 8 - len(binary_blue)
            x = 0
            for x in range(0, difference):
                binary_blue = "0" + binary_blue
        binary_vert = bin(v)[2:]
        if (len(binary_vert) != 8):
            difference = 8 - len(binary_vert)
            x = 0
            for x in range(0, difference):
                binary_vert = "0" + binary_vert
        print("\nBinary Rouge=\t",binary_red,"\nBinary 
Vert=\t",binary_vert,"\nBinary Bleu=\t",binary_blue)

        if (taille % 3 == 0):
            char_binaire = char_binaire + binary_red[7:]
            pic += 1
        elif (taille % 3 == 1):
            char_binaire = char_binaire + binary_vert[7:]
            pic += 1
        elif (taille % 3 == 2):
            char_binaire = char_binaire + binary_blue[7:]
            pic += 1
        print("\nBinary Caracter=\t",char_binaire)
        print("\nPixel=\t",pic)
        if ( pic == 3):
            pic = 0
            j+=1
            if(j == colonne):
                i+=1
                j = 0
    c = chr(int(char_binaire, 2))
    msg = msg + c
    print("\nCaracter=\t",c,"\nMessage=\t",msg)
return msg

choix=int(input("Choisir : \n 1 => Encoder l'image \n 2 => Decoder l'image 
\n 99 => Quitter \n Votre choix: \t"))

while( choix != 99):
    if choix == 1:
    # vous pouvez donner le chemin complet de l image
    fichier_original_image=input("Donnez le chemin complet de l'image à 
crypter:\t")
    img = Image.open(fichier_original_image)
    # le mode de l image doit etre 'RVB'


    # creer un nouveau document pour l image modifiee/codee
    pos1 = fichier_original_image.rfind('\\')+1
    pos2 = fichier_original_image.rfind('.')
    pos2 = pos2 + len('.bmp')
    image_name = fichier_original_image[pos1:pos2]
    image_encode = "enc_" + image_name
    chemin_image = fichier_original_image[:pos1]+image_encode


    # on ne depasse pas 255 caracteres
    secret_msg = input("Donnez le message à cacher: \t")

    img_encode = encode_image(img, secret_msg)
    if img_encode:
        # save the image with the hidden text
        img_encode.save(chemin_image)
        print("{} saved!".format(image_encode))

        # l image sauvegardee dans le chemin du programme

        import os
        os.system('cygwin' + image_encode)

elif choix == 2:

    # retourner le message secret
    chemin = input("Donnez le chemin complet de l'image à decrypter: ")
    encode_img = Image.open(chemin)
    hidden_text = decode_image(encode_img)

    print("Texte caché:\n{}".format(hidden_text))

    choix=int(input("Choisir : \n 1 => Encoder l'image \n 2 => Decoder l'image \n 99 => Quitter \n Votre choix: \t"))

0 个答案:

没有答案