构建具有 LSB 替换和 XOR 替换的隐写算法

问题描述 投票:0回答:1

我正在研究this论文中算法的实现。我已经基本完成了整个事情,但还有一些问题。首先,它可以很好地将数据嵌入到图像中,但很难将数据取出。图像内的数据只是文本。它取出了文本,但文本不是一个连贯的单词。

从伪代码开始,我将包含嵌入的伪代码:

1: Read Cover Image, CI
2: Determine: Height → h and width → w
3: Message Characters → msgB[N] where N=(1,2,...,n)
4: for i → h do
5: for j → w do
6: B(msgB[N] and p = 0
7: phw ← P (h, w)
8: Y 1 ← phw R7 ⊕ B[p++]
9: P (h, w) ← f(Y1,R) checkSUM p!=8 && ==N
10: Y 2 ← phw G7 ⊕ B[p++]
11: P (h, w) ← f(Y2,G) checkSUM p!=8 && ==N
12: Y 3 ← phw B7 ⊕ B[p++]
13: P (h, w) ← f(Y3,B) checkSUM p!=8 && ==N
14: end for
15: Rewrite CI → SCI
16: end for
17: Finishes and Return SC

然后是提取数据的伪代码:

1: Read Stego Object, SO
2: Determine: Height → h and width → w
3: Message Characters → msgB[N] where N=(1,2,...,n)
4: for i → h do
5: for j → w do
6: Get Lm & Create Binary[8]
7: if Lm then
8: phw ← P (h, w)
9: if phw R7 ⊕ 1 == phw R8 then
10: Y 1 ← 1
11: else
12: Y 1 ← 0
13: end if
14: Binary ← Y1 checkSUM p!=8
15: if phw G7 ⊕ 1 == phw G8 then
16: Y 2 ← 1
17: else
18: Y 2 ← 0
19: end if
20: Binary ← Y2 checkSUM p!=8
21: if phw B7 ⊕ 1 == phw B8 then
22: Y 3 ← 1
23: else
24: Y 3 ← 0
25: end if
26: Binary ← Y3 checkSUM p!=8
27: end if
28: end for
29: Insert → msgB[N]
30: end for
31: F inishes and Return msgB

我像这样实现了数据嵌入:

def embed_text(self, image_path, text, output_path):
    # Read Cover Image
    image = Image.open(image_path)

    # Determine Height (h) and Width (w)
    h, w = image.size

    # Message Characters
    binary_message = ''.join(format(ord(char), '08b') for char in text)

    # Embedding loop
    message_index = 0
    for i in range(h):
        for j in range(w):
            # B(msgB[N] and p = 0)
            B = int(binary_message[message_index]) if message_index < len(binary_message) else 0
            message_index += 1

            # phw ← P(h, w)
            phw = image.getpixel((j, i))

            # Y1 ← phw R7 ⊕ B[p++]
            Y1 = (phw[0] & 0xFE) | ((B >> 0) & 1)
            image.putpixel((j, i), (Y1, phw[1], phw[2]))

            # Y2 ← phw G7 ⊕ B[p++]
            B = int(binary_message[message_index]) if message_index < len(binary_message) else 0
            message_index += 1
            Y2 = (phw[1] & 0xFE) | ((B >> 0) & 1)
            image.putpixel((j, i), (phw[0], Y2, phw[2]))

            # Y3 ← phw B7 ⊕ B[p++]
            B = int(binary_message[message_index]) if message_index < len(binary_message) else 0
            message_index += 1
            Y3 = (phw[2] & 0xFE) | ((B >> 0) & 1)
            image.putpixel((j, i), (phw[0], phw[1], Y3))

            # P(h, w) ← f(Y1, R) checkSUM p!=8 && ==N
            # (Assuming f(Y1, R) is XOR operation with 7th bit of R component)
            # ... Repeat similar steps for G and B components ...

    # Rewrite CI → SCI
    image.save(output_path)

这是我用来提取的鳕鱼:

def extract_text(self, image_path):
    stego_image = Image.open(image_path)
    height, width = stego_image.size
    msgB = []  # Initialize an empty list to store the message characters
    binary_message = ""     
    for i in range(height):
        for j in range(width):
            # Get Lm & Create Binary[8]
            phw = stego_image.getpixel((j, i))
            Lm = self.get_Lm(phw)
            binary = "{:08b}".format(Lm)

            print(f"Lm: {Lm}, Binary: {binary}")

        # Process the pixel only if Lm is 1
            if Lm:
                # Process the RED component
                if phw[0] % 2 == int(binary[0]):
                    Y1 = 1
                else:
                    Y1 = 0

                # Process the GREEN component
                if phw[1] % 2 == int(binary[1]):
                    Y2 = 1
                else:
                    Y2 = 0

                # Process the BLUE component
                if phw[2] % 2 == int(binary[2]):
                    Y3 = 1
                else:
                    Y3 = 0

            # Append the processed pixel to msgB
            binary_message += f"{Y1}{Y2}{Y3}"
    msgB = self.binary_to_string(binary_message)
    print("msgB:", msgB)
    return msgB
def binary_to_string(self, binary_message):
    # Split the binary message into 8-bit substrings
    eight_bit_chunks = [binary_message[i:i + 8] for i in range(0, len(binary_message), 8)]

    # Convert each 8-bit substring to an integer and then to a character
    characters = [chr(int(chunk, 2)) for chunk in eight_bit_chunks]

    # Join the characters to form the final string
    return ''.join(characters)

嵌入过程非常不一致。有时它会嵌入(主要是 png 图像),但当我添加 jpeg 图像时,它会失败并出现超出范围错误。至于提取方法,我认为是对的,但是没能取出正确的文本。我得到一些有趣的东西,比如:

HådlÿÿÿI$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I$I...

我不确定如何继续,因为我的实施步骤似乎很合理,但我对这个过程哪里出了问题感到困惑。我真的很感谢您提供一些关于如何继续进行此工作的指导,因为我必须完成此工作并在 12 月 5 日提交,所以我的时间正在流逝。

python security steganography computer-forensics
1个回答
0
投票

在你的 embed_text 函数中,它看起来像你

  1. 获取像素数据。
  2. 更改红色LSB,并保存像素。
  3. 更改绿色LSB,并保存像素。
  4. 更改蓝色LSB,并保存像素。

这只会保留最后的更改。您应该在更改所有三个值后写入一次像素。

image.putpixel((j,i),(Y1,Y2,Y3))

就尝试在 JPEG 图像上使用 LSB 而言,这是行不通的。 Jpeg 是一种有损格式,您不能指望 LSB 保持不变。如果您确实想实现这一点,请考虑在 DCT 中保存数据。

© www.soinside.com 2019 - 2024. All rights reserved.