mirror of
https://github.com/BreizhHardware/py_A2.git
synced 2026-01-18 16:47:38 +01:00
Base for the encoding
This commit is contained in:
45
TP2/main.py
45
TP2/main.py
@@ -6,15 +6,15 @@ from sys import argv as args
|
||||
|
||||
def __main__():
|
||||
if len(args) == 2:
|
||||
if (args[1] == "tp2"):
|
||||
if args[1] == "tp2":
|
||||
__tp2__()
|
||||
elif (args[1] == "tp2_optionnel"):
|
||||
elif args[1] == "tp2_optionnel":
|
||||
__tp2_optionnel__()
|
||||
elif (args[1] == "tp_steganographie_text"):
|
||||
elif args[1] == "tp_steganographie_text":
|
||||
__tp_steganographie_text__()
|
||||
elif (args[1] == "tp_steganographie_image"):
|
||||
elif args[1] == "tp_steganographie_image":
|
||||
__tp_steganographie_image__()
|
||||
elif (args[1] == "tp_steganographie_hide"):
|
||||
elif args[1] == "tp_steganographie_hide":
|
||||
__tp_steganographie_hide__()
|
||||
else:
|
||||
print("Usage: python3 main.py")
|
||||
@@ -57,31 +57,34 @@ def __tp2_optionnel__():
|
||||
|
||||
|
||||
def __tp_steganographie_text__():
|
||||
print(getHiddenTextWithDelimiter(hiddenText1, "\0"))
|
||||
print(getHiddenTextWithDelimiter(hiddenText2, "\0"))
|
||||
print(getHiddenTextOfLength(hiddenText3, 4))
|
||||
print(getHiddenTextOfLength(hiddenText4, 16))
|
||||
print(get_hidden_text_with_delimiter(hiddenText1, "\0"))
|
||||
print(get_hidden_text_with_delimiter(hiddenText2, "\0"))
|
||||
print(get_hidden_text_of_length(hiddenText3, 4))
|
||||
print(get_hidden_text_of_length(hiddenText4, 16))
|
||||
|
||||
|
||||
def __tp_steganographie_image__():
|
||||
hidden1 = getHiddenImage(hiddenImage1, 8, 1)
|
||||
hidden1 = get_hidden_image(hiddenImage1, 8)
|
||||
hidden1.show()
|
||||
hidden2 = getHiddenImage(hiddenImage2, 8, 1)
|
||||
hidden2 = get_hidden_image(hiddenImage2, 8)
|
||||
hidden2.show()
|
||||
hidden3 = getHiddenImage(hiddenImage3, 8, 2)
|
||||
hidden3 = get_hidden_image(hiddenImage3, 8, 2)
|
||||
hidden3.show()
|
||||
hidden4 = getHiddenImage(hiddenImage4, 16, 2)
|
||||
hidden4 = get_hidden_image(hiddenImage4, 16, 2)
|
||||
hidden4.show()
|
||||
|
||||
|
||||
def __tp_steganographie_hide__():
|
||||
gladiusWithHiddenTextDelimiter = setHiddenTextWithDelimiter(gladius, "Vive Star Citizen!", "\0")
|
||||
gladiusWithHiddenTextDelimiter.show()
|
||||
gladuisDecodedDelimiter = getHiddenTextWithDelimiter(gladiusWithHiddenTextDelimiter, "\0")
|
||||
print(gladuisDecodedDelimiter)
|
||||
zeusWithHiddenTextLength = setHiddenTextOfLength(zeusMK2CL, "I Held the line !", 4)
|
||||
zeusWithHiddenTextLength.show()
|
||||
zeusDecodedLength = getHiddenTextOfLength(zeusWithHiddenTextLength, 4)
|
||||
print(zeusDecodedLength)
|
||||
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.show()
|
||||
gladius_decoded_image = get_hidden_image(gladius_with_hidden_image, 8)
|
||||
gladius_decoded_image.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -12,7 +12,7 @@ gladius = Image.open("Gladius.jpg")
|
||||
zeusMK2CL = Image.open("Zeus MKII CL.jpeg")
|
||||
|
||||
|
||||
def imageToArray(image):
|
||||
def image_to_array(image):
|
||||
array = []
|
||||
for pixel in image.getdata():
|
||||
array.append(pixel[0])
|
||||
@@ -21,41 +21,41 @@ def imageToArray(image):
|
||||
return array
|
||||
|
||||
|
||||
def getLength(imageArray, nbBitsForLength, index=0):
|
||||
length, i = 0, nbBitsForLength - 1
|
||||
for len in range(index, index + nbBitsForLength):
|
||||
length += imageArray[len] % 2 * (2 ** i)
|
||||
def get_length(image_array, nb_bits_for_length, index=0):
|
||||
length, i = 0, nb_bits_for_length - 1
|
||||
for len in range(index, index + nb_bits_for_length):
|
||||
length += image_array[len] % 2 * (2 ** i)
|
||||
i -= 1
|
||||
return length
|
||||
|
||||
|
||||
def getChar(imageArray, index):
|
||||
def get_char(image_array, index):
|
||||
binN, i = 0, 6
|
||||
for j in range(index, index + 7):
|
||||
binN += (imageArray[j] % 2) * (2 ** i)
|
||||
binN += (image_array[j] % 2) * (2 ** i)
|
||||
i -= 1
|
||||
return chr(binN)
|
||||
|
||||
|
||||
def getHiddenTextWithDelimiter(image, delimiter):
|
||||
imageArray = imageToArray(image)
|
||||
hiddenText = ""
|
||||
def get_hidden_text_with_delimiter(image, delimiter):
|
||||
image_array = image_to_array(image)
|
||||
hidden_text = ""
|
||||
i = 0
|
||||
while getChar(imageArray, i) != delimiter:
|
||||
hiddenText += getChar(imageArray, i)
|
||||
while get_char(image_array, i) != delimiter:
|
||||
hidden_text += get_char(image_array, i)
|
||||
i += 7
|
||||
return hiddenText
|
||||
return hidden_text
|
||||
|
||||
|
||||
def getHiddenTextOfLength(image, nbBitsForLength):
|
||||
imageArray = imageToArray(image)
|
||||
hiddenText = ""
|
||||
for i in range(nbBitsForLength, ((getLength(imageArray, nbBitsForLength) * 7) + nbBitsForLength), 7):
|
||||
hiddenText += getChar(imageArray, i)
|
||||
return hiddenText
|
||||
def get_hidden_text_of_length(image, nb_bits_for_length):
|
||||
image_array = image_to_array(image)
|
||||
hidden_text = ""
|
||||
for i in range(nb_bits_for_length, ((get_length(image_array, nb_bits_for_length) * 7) + nb_bits_for_length), 7):
|
||||
hidden_text += get_char(image_array, i)
|
||||
return hidden_text
|
||||
|
||||
|
||||
def getListLastKBits(image, k):
|
||||
def get_list_last_k_bits(image, k):
|
||||
list_pixel = list(image.getdata())
|
||||
list_bit = []
|
||||
for t in list_pixel:
|
||||
@@ -65,32 +65,32 @@ def getListLastKBits(image, k):
|
||||
return list_bit
|
||||
|
||||
|
||||
def parseDimensions(list_bit, nbBitsForSize, nbLastBits):
|
||||
first_bits = list_bit[:nbBitsForSize // nbLastBits:]
|
||||
second_bits = list_bit[nbBitsForSize // nbLastBits: 2 * (nbBitsForSize // nbLastBits):]
|
||||
def parse_dimensions(list_bit, nb_bits_for_size, nb_last_bits):
|
||||
first_bits = list_bit[:nb_bits_for_size // nb_last_bits:]
|
||||
second_bits = list_bit[nb_bits_for_size // nb_last_bits: 2 * (nb_bits_for_size // nb_last_bits):]
|
||||
width = int("".join(first_bits), 2)
|
||||
height = int("".join(second_bits), 2) - 1
|
||||
return width, height
|
||||
|
||||
|
||||
def parseRGB(binarie):
|
||||
r = int(binarie[:8:], 2)
|
||||
g = int(binarie[8:16:], 2)
|
||||
b = int(binarie[16:32:], 2)
|
||||
def parse_RGB(binary):
|
||||
r = int(binary[:8:], 2)
|
||||
g = int(binary[8:16:], 2)
|
||||
b = int(binary[16:32:], 2)
|
||||
return r, g, b
|
||||
|
||||
|
||||
def getHiddenImage(image, nbBitsForSize, nbLastBits):
|
||||
list_bit = getListLastKBits(image, nbLastBits)
|
||||
def get_hidden_image(image, nb_bits_for_size, nb_last_bits = 1):
|
||||
list_bit = get_list_last_k_bits(image, nb_last_bits)
|
||||
bin = ""
|
||||
|
||||
width, height = parseDimensions(list_bit, nbBitsForSize, nbLastBits)
|
||||
width, height = parse_dimensions(list_bit, nb_bits_for_size, nb_last_bits)
|
||||
|
||||
secret_image = Image.new("RGB", (width, height), (0, 0, 0))
|
||||
col = -1
|
||||
row = -1
|
||||
|
||||
for k in range(2 * (nbBitsForSize // nbLastBits), len(list_bit)):
|
||||
for k in range(2 * (nb_bits_for_size // nb_last_bits), len(list_bit)):
|
||||
bin += list_bit[k]
|
||||
if len(bin) == 24:
|
||||
if col == width:
|
||||
@@ -99,32 +99,50 @@ def getHiddenImage(image, nbBitsForSize, nbLastBits):
|
||||
if row == height:
|
||||
return secret_image
|
||||
|
||||
r, g, b = parseRGB(bin)
|
||||
r, g, b = parse_RGB(bin)
|
||||
secret_image.putpixel((col, row), (r, g, b))
|
||||
col += 1
|
||||
bin = ""
|
||||
|
||||
|
||||
def setHiddenTextWithDelimiter(image, text, delimiter):
|
||||
imageArray = imageToArray(image)
|
||||
def set_hidden_text_with_delimiter(image, text, delimiter):
|
||||
image_array = image_to_array(image)
|
||||
for i in range(len(text)):
|
||||
char = ord(text[i])
|
||||
for j in range(7):
|
||||
imageArray[i * 7 + j] = (imageArray[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):
|
||||
imageArray[len(text) * 7 + j] = (imageArray[len(text) * 7 + j] & 0b11111110) | (ord(delimiter) >> (6 - j) & 1)
|
||||
image.putdata([(imageArray[i], imageArray[i + 1], imageArray[i + 2]) for i in range(0, len(imageArray), 3)])
|
||||
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
|
||||
|
||||
|
||||
def setHiddenTextOfLength(image, text, nbBitsForLength):
|
||||
imageArray = imageToArray(image)
|
||||
def set_hidden_text_of_length(image, text, nb_bits_for_length):
|
||||
image_array = image_to_array(image)
|
||||
length = len(text)
|
||||
for i in range(nbBitsForLength):
|
||||
imageArray[i] = (imageArray[i] & 0b11111110) | ((length >> (nbBitsForLength - 1 - i)) & 1)
|
||||
for i in range(len(text)):
|
||||
char = ord(text[i])
|
||||
for j in range(7):
|
||||
imageArray[i * 7 + nbBitsForLength] = (imageArray[i * 7 + nbBitsForLength] & 0b11111110) | ((char >> (6 - j)) & 1)
|
||||
image.putdata([(imageArray[i], imageArray[i + 1], imageArray[i + 2]) for i in range(0, len(imageArray), 3)])
|
||||
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)
|
||||
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 = ""
|
||||
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:]
|
||||
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)])
|
||||
return image_original
|
||||
|
||||
Reference in New Issue
Block a user