隐身程序--将python 2转换为3,语法错误:base64.b64decode("".join(chars))

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

我在Steg程序的最后一部分有语法问题。我试着把Python 2版本(工作代码)转换成Python 3,这是最后一部分。

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

程序1. 在图像的最后几位加密信息,并将其保存为一个新的图像。然后2.解密信息,存储在 "flag "中,并打印出来。*是否会因为输入错误而导致错误?

message = input("Your message: ")

下面是: UNHIDING程序

    #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
python steganography python-2to3
1个回答
0
投票

help 对于 base64.b64decode 说。

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

考虑到在Python 2中,有 "正常的" str和unicode-str(u-prefixed),我建议仔细研究一下什么是产生的 "".join(chars). 它是否只包含ASCII字符?我建议在前面加上

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

直接加在前面。

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

如果代码里面有数字>127,这意味着它可能无法工作,因为它只适合于纯ASCII码。strs.

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