diff --git a/TP2/Gladius.jpg b/TP2/Gladius.jpg deleted file mode 100644 index 0ee08a7..0000000 Binary files a/TP2/Gladius.jpg and /dev/null differ diff --git a/TP2/Gladius.png b/TP2/Gladius.png new file mode 100644 index 0000000..c3603d2 Binary files /dev/null and b/TP2/Gladius.png differ diff --git a/TP2/Zeus MKII CL.jpeg b/TP2/Zeus MKII CL.jpeg deleted file mode 100644 index 5e3c41d..0000000 Binary files a/TP2/Zeus MKII CL.jpeg and /dev/null differ diff --git a/TP2/Zeus-MKII-CL.png b/TP2/Zeus-MKII-CL.png new file mode 100644 index 0000000..db360d4 Binary files /dev/null and b/TP2/Zeus-MKII-CL.png differ diff --git a/TP2/main.py b/TP2/main.py index 7d517d2..5ef7344 100644 --- a/TP2/main.py +++ b/TP2/main.py @@ -57,6 +57,10 @@ def __tp2_optionnel__(): def __tp_steganographie_text__(): + hiddenText1 = Image.open("hiddenText1.png") + hiddenText2 = Image.open("hiddenText2.png") + hiddenText3 = Image.open("hiddenText3.png") + hiddenText4 = Image.open("hiddenText4.png") print(get_hidden_text_with_delimiter(hiddenText1, "\0")) print(get_hidden_text_with_delimiter(hiddenText2, "\0")) print(get_hidden_text_of_length(hiddenText3, 4)) @@ -64,6 +68,10 @@ def __tp_steganographie_text__(): def __tp_steganographie_image__(): + hiddenImage1 = Image.open("hiddenImage1.png") + hiddenImage2 = Image.open("hiddenImage2.png") + hiddenImage3 = Image.open("hiddenImage3.png") + hiddenImage4 = Image.open("hiddenImage4.png") hidden1 = get_hidden_image(hiddenImage1, 8) hidden1.show() hidden2 = get_hidden_image(hiddenImage2, 8) @@ -75,13 +83,16 @@ def __tp_steganographie_image__(): def __tp_steganographie_hide__(): + gladius = Image.open("Gladius.png") + zeusMK2CL = Image.open("Zeus-MKII-CL.png") + small = Image.open("small.png") gladius_with_hidden_text_delimiter = set_hidden_text_with_delimiter(gladius, "Vive Star Citizen!", "\0") gladius_decoded_delimiter = get_hidden_text_with_delimiter(gladius_with_hidden_text_delimiter, "\0") print(gladius_decoded_delimiter) zeus_with_hidden_text_length = set_hidden_text_of_length(zeusMK2CL, "I Held the line !", 4) zeus_decoded_length = get_hidden_text_of_length(zeus_with_hidden_text_length, 4) print(zeus_decoded_length) - gladius_with_hidden_image = set_hidden_image(gladius, zeusMK2CL, 8) + gladius_with_hidden_image = set_hidden_image(gladius, small, 8) gladius_with_hidden_image.show() gladius_decoded_image = get_hidden_image(gladius_with_hidden_image, 8) gladius_decoded_image.show() diff --git a/TP2/small.png b/TP2/small.png new file mode 100644 index 0000000..09e2d84 Binary files /dev/null and b/TP2/small.png differ diff --git a/TP2/tp_steganographie.py b/TP2/tp_steganographie.py index 22974d1..08d19da 100644 --- a/TP2/tp_steganographie.py +++ b/TP2/tp_steganographie.py @@ -1,16 +1,5 @@ from PIL import Image -hiddenText1 = Image.open("hiddenText1.png") -hiddenText2 = Image.open("hiddenText2.png") -hiddenText3 = Image.open("hiddenText3.png") -hiddenText4 = Image.open("hiddenText4.png") -hiddenImage1 = Image.open("hiddenImage1.png") -hiddenImage2 = Image.open("hiddenImage2.png") -hiddenImage3 = Image.open("hiddenImage3.png") -hiddenImage4 = Image.open("hiddenImage4.png") -gladius = Image.open("Gladius.jpg") -zeusMK2CL = Image.open("Zeus MKII CL.jpeg") - def image_to_array(image): array = [] @@ -110,9 +99,11 @@ def set_hidden_text_with_delimiter(image, text, delimiter): for i in range(len(text)): char = ord(text[i]) for j in range(7): - image_array[i * 7 + j] = (image_array[i * 7 + j] & 0b11111110) | ((char >> (6 - j)) & 1) + image_array[i * 7 + j] = ((image_array[i * 7 + j] & 0b11111110) | + ((char >> (6 - j)) & 1)) for j in range(7): - image_array[len(text) * 7 + j] = (image_array[len(text) * 7 + j] & 0b11111110) | (ord(delimiter) >> (6 - j) & 1) + image_array[len(text) * 7 + j] = ((image_array[len(text) * 7 + j] & 0b11111110) | + (ord(delimiter) >> (6 - j) & 1)) image.putdata([(image_array[i], image_array[i + 1], image_array[i + 2]) for i in range(0, len(image_array), 3)]) return image @@ -123,26 +114,35 @@ def set_hidden_text_of_length(image, text, nb_bits_for_length): for i in range(nb_bits_for_length): image_array[i] = (image_array[i] & 0b11111110) | ((length >> (nb_bits_for_length - 1 - i)) & 1) for i in range(length * 7): - char = ord(text[i // 7]) - image_array[i + nb_bits_for_length] = (image_array[i + nb_bits_for_length] & 0b11111110) | ((char >> (6 - (i % 7))) & 1) + char_index = ord(text[i // 7]) + char_offset = i % 7 + if char_index < len(text): + char = ord(text[char_index]) + image_array[i + nb_bits_for_length] = ((image_array[i + nb_bits_for_length] & 0b11111110) | + ((char >> (6 - char_offset)) & 1)) + else: + image_array[i + nb_bits_for_length] = (image_array[i + nb_bits_for_length] & 0b11111110) image.putdata([(image_array[i], image_array[i + 1], image_array[i + 2]) for i in range(0, len(image_array), 3)]) return image def set_hidden_image(image_original, image_to_hide, nb_bits_for_size, nb_last_bits = 1): list_bit = get_list_last_k_bits(image_original, nb_last_bits) - bin = "" + binary_data = "" width, height = image_to_hide.size for i in range(nb_bits_for_size // nb_last_bits): - bin += '{0:08b}'.format(width)[i * nb_last_bits:(i + 1) * nb_last_bits:] - bin += '{0:08b}'.format(height + 1)[i * nb_last_bits:(i + 1) * nb_last_bits:] + binary_data += '{0:08b}'.format(width, f'0{nb_last_bits}b') + binary_data += '{0:08b}'.format(height, f'0{nb_last_bits}b') for i in range(height): for j in range(width): r, g, b = image_to_hide.getpixel((j, i)) - bin += '{0:08b}'.format(r) - bin += '{0:08b}'.format(g) - bin += '{0:08b}'.format(b) - for k in range(len(bin)): - list_bit[k] = list_bit[k][:-nb_last_bits:] + bin[k] - image_original.putdata([(int(list_bit[i], 2), int(list_bit[i + 1], 2), int(list_bit[i + 2], 2)) for i in range(0, len(list_bit), 3)]) + binary_data += '{0:08b}'.format(r) + binary_data += '{0:08b}'.format(g) + binary_data += '{0:08b}'.format(b) + while len(binary_data) < len(list_bit): + binary_data += '0' + for k in range(len(list_bit)): + list_bit[k] = list_bit[k][:-nb_last_bits:] + binary_data[k] + image_original.putdata([(int(list_bit[i], 2), int(list_bit[i + 1], 2), int(list_bit[i + 2], 2)) + for i in range(0, len(list_bit), 3)]) return image_original