0

我对 steg 程序最后一部分的语法有疑问。我试图将 python 2 版本(工作代码)转换为 python 3,这是它的最后一部分:

flag = base64.b64decode("".join(chars)) <- error
print(flag)

程序 1. 加密图像最后有效位中的消息,将其保存为新图像。然后 2.解密存储在“flag”中的消息,并打印它。* 错误是否是由错误的输入类型引起的?:

message = input("Your message: ")

下图:取消隐藏程序

    #coding: utf-8
import base64
from PIL import Image


image = Image.open("after.png")

extracted = ''

pixels = image.load()
#Iterating in 1st row
for x in range(0,image.width):
    r,g,b = pixels[x,0]
    # Storing LSB of each color
    extracted += bin(r)[-1]
    extracted += bin(g)[-1]
    extracted += bin(b)[-1]

chars = []
for i in range(len(extracted)/8):
    byte = extracted[i*8:(i+1)*8]
    chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))

flag = base64.b64decode(''.join(chars))
print flag

下图:隐藏程序:

    import bitarray
import base64
from PIL import Image
with Image.open('before.png') as im:
    pixels=im.load()

message = input("Your message: ")

encoded_message = base64.b64encode(message.encode('utf-8'))

#Convert the message into an array of bits
ba = bitarray.bitarray()
ba.frombytes(encoded_message)
bit_array = [int(i) for i in ba]

#Duplicate the original picture
im = Image.open("before.png")
im.save("after.png")

im = Image.open("after.png")
width, height = im.size
pixels = im.load()

#Hide message in the first row
i = 0
for x in range(0,width):
    r,g,b = pixels[x,0]
    #print("[+] Pixel : [%d,%d]"%(x,0))
    #print("[+] \tBefore : (%d,%d,%d)"%(r,g,b))
    #Default values in case no bit has to be modified
    new_bit_red_pixel = 255
    new_bit_green_pixel = 255
    new_bit_blue_pixel = 255

    if i<len(bit_array):
        #Red pixel
        r_bit = bin(r)
        r_last_bit = int(r_bit[-1])
        r_new_last_bit = r_last_bit & bit_array[i]
        new_bit_red_pixel = int(r_bit[:-1]+str(r_new_last_bit),2)
        i += 1

    if i<len(bit_array):
        #Green pixel
        g_bit = bin(g)
        g_last_bit = int(g_bit[-1])
        g_new_last_bit = g_last_bit & bit_array[i]
        new_bit_green_pixel = int(g_bit[:-1]+str(g_new_last_bit),2)
        i += 1

    if i<len(bit_array):
        #Blue pixel
        b_bit = bin(b)
        b_last_bit = int(b_bit[-1])
        b_new_last_bit = b_last_bit & bit_array[i]
        new_bit_blue_pixel = int(b_bit[:-1]+str(b_new_last_bit),2)
        i += 1

    pixels[x,0] = (new_bit_red_pixel,new_bit_green_pixel,new_bit_blue_pixel)
    #print("[+] \tAfter: (%d,%d,%d)"%(new_bit_red_pixel,new_bit_green_pixel,new_bit_blue_pixel))
    im.save('after.png')

错误

ValueError: string argument should contain only ASCII characters
4

1 回答 1

0

help对于base64.b64decode说:

b64decode(s, altchars=None, validate=False)
    Decode the Base64 encoded bytes-like object or ASCII string s.
    ...

考虑到在 Python 2 中存在“普通” strs 和 unicode str-s(以 u 为前缀),我建议仔细研究一下产生了什么"".join(chars)。它是否仅包含 ASCII 字符?我建议添加:

print("Codes:",[ord(c) for c in chars])

直接在之前:

flag = base64.b64decode("".join(chars))

如果代码中的数字 >127,则意味着它可能无法正常工作,因为它仅适用于纯 ASCII strs。

于 2020-05-07T13:35:47.340 回答