You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于LSB替换与XOR替换的隐写算法实现故障排查求助

隐写算法实现问题求助

我正在实现一篇IEEE论文中的隐写算法,大部分开发已完成,但遇到两个核心问题:

  • 嵌入功能仅对PNG图像有效,处理JPEG图像时会触发越界错误;
  • 提取功能虽能取出文本,但无法得到连贯可读内容,输出为乱码(示例:HådlÿÿÿI$I$I$I$...)。

嵌入伪代码

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: Finishes and Return msgB

Python嵌入实现代码

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)

Python提取实现代码

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)

无法定位问题所在,12月5日需完成展示,恳请技术指导。


内容的提问来源于stack exchange,提问作者user10116100

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:17:11