我使用stepic3隐藏一些数据。多个文件被压缩成一个zip文件,这将是隐藏的消息。但是,当我使用以下代码时
from PIL import Image
import stepic
def enc_():
im = Image.open("secret.png")
text = str(open("source.zip", "rb").read())
im = stepic.encode(im, text)
im.save('stegolena.png','PNG')
def dec_():
im1=Image.open('stegolena.png')
out = stepic.decode(im1)
plaintext = open("out.zip", "w")
plaintext.write(out)
plaintext.close()我知道错误了
完全回溯 回溯(最近一次调用): 文件"C:\Users\Sherif\OneDrive\Pyhton Projects\Kivy Tests\simple.py",第28行,在enc_()中 文件"C:\Users\Sherif\OneDrive\Pyhton Projects\Kivy Tests\simple.py",第8行,在enc_中 im = stepic.encode(im,text) 文件"C:\Users\Sherif\OneDrive\Pyhton Projects\Kivy Tests\stepic.py",第89行,编码 Encode_inplace(图像,数据) 文件"C:\Users\Sherif\OneDrive\Pyhton Projects\Kivy Tests\stepic.py",第75行,在encode_inplace中 对于encode_imdata中的像素(image.getdata(),data): 文件"C:\Users\Sherif\OneDrive\Pyhton Projects\Kivy Tests\stepic.py",第58行,在encode_imdata中 字节= ord(datai) TypeError: ord()期望字符串长度为1,但int找到
有两种转换为字符串的方法。
text = open("source.zip", "r", encoding='utf-8', errors='ignore').read()带输出
PKn!K\Z
sec.txt13 byte 1.10mPKn!K\Z
sec.txtPK52或
text = str(open("source.zip", "rb").read())带输出
b'PK\x03\x04\x14\x00\x00\x00\x00\x00n\x8f!K\\\xac\xdaZ\r\x00\x00\x00\r\x00\x00\x00\x07\x00\x00\x00sec.txt13 byte 1.10mPK\x01\x02\x14\x00\x14\x00\x00\x00\x00\x00n\x8f!K\\\xac\xdaZ\r\x00\x00\x00\r\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xb6\x81\x00\x00\x00\x00sec.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x005\x00\x00\x002\x00\x00\x00\x00\x00'我用了第二个,从检索中得到了同样的字符串。
为了重建zip文件(输出是字符串),我使用以下代码
plaintext = open("out.zip", "w")
plaintext.write(output)
plaintext.close()但是当我试图打开它时,写的文件被破坏了。当我试着阅读它所写的东西时,
output = output.encode(encoding='utf_8', errors='strict')或
output = bytes(output, 'utf_8')输出是
b"b'PK\\x03\\x04\\x14\\x00\\x00\\x00\\x00\\x00n\\x8f!K\\\\\\xac\\xdaZ\\r\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x07\\x00\\x00\\x00sec.txt13 byte 1.10mPK\\x01\\x02\\x14\\x00\\x14\\x00\\x00\\x00\\x00\\x00n\\x8f!K\\\\\\xac\\xdaZ\\r\\x00\\x00\\x00\\r\\x00\\x00\\x00\\x07\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xb6\\x81\\x00\\x00\\x00\\x00sec.txtPK\\x05\\x06\\x00\\x00\\x00\\x00\\x01\\x00\\x01\\x005\\x00\\x00\\x002\\x00\\x00\\x00\\x00\\x00'"这与源文件不同。
我需要什么才能忠实地重建嵌入的文件?
发布于 2017-09-12 01:37:32
当您以rb模式读取文件时,您将得到一个字节数组。如果您打印它,它可能看起来像一个字符串,但每个单独的元素实际上是一个整数。
>>> my_bytes = b'hello'
>>> my_bytes
b'hello'
>>> my_bytes[0]
104这解释了错误
“C:\Users\Sherif\OneDrive\ Projects\Kivy Tests\stepic.py",第58行,以encode_imdata字节= ord(datai) TypeError: ord()预期长度1的字符串表示,但int找到
ord()需要一个字符串,所以您必须将所有字节转换为字符串。不幸的是,str(some_byte_array)并没有做你认为它所做的事情。它为字节数组创建一个文字字符串表示,包括前面的"b“和周围的引号。
>>> string = str(my_bytes)
>>> string[0]
'b'
>>> string[1]
"'"
>>> string[2]
'h'相反,您需要的是将每个字节(整数)分别转换为字符串。map(chr, some_byte_array)会为你这么做的。我们必须这样做,因为stepic需要一个字符串。当它嵌入一个字符时,它执行ord(data[i]),它将长度为1的字符串转换为其Unicode代码(整数)。
此外,我们不能将字符串保留为map对象,因为代码在嵌入字符串之前需要计算整个字符串的长度。因此,''.join(map(chr, some_bytearray))是我们必须使用的输入秘密。
对于提取,stepic做的恰恰相反。它逐字节提取秘密字节,并使用chr(byte)将它们转换为字符串。为了扭转这种情况,我们需要得到每个字符的序数值。map(ord, out)应该能做到这一点。而且,由于我们希望以二进制方式编写我们的文件,进一步将其输入bytearray()将处理所有问题。
总的来说,这些是您应该对代码进行的更改。
def enc_():
im = Image.open("secret.png")
text = ''.join(map(chr, open("source.zip", "rb").read()))
im = stepic.encode(im, text)
im.save('stegolena.png','PNG')
def dec_():
im1=Image.open('stegolena.png')
out = stepic.decode(im1)
plaintext = open("out.zip", "wb")
plaintext.write(bytearray(map(ord, out)))
plaintext.close()https://stackoverflow.com/questions/46165249
复制相似问题