python - reconstruct the source file from string output -
i use stepic3 hide data. multiple files compressed zip file, hidden message. however, when use following code
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()
i error
complete trace traceback (most recent call last): file "c:\users\sherif\onedrive\pyhton projects\kivy tests\simple.py", line 28, in <module> enc_() file "c:\users\sherif\onedrive\pyhton projects\kivy tests\simple.py", line 8, in enc_ im = stepic.encode(im, text) file "c:\users\sherif\onedrive\pyhton projects\kivy tests\stepic.py", line 89, in encode encode_inplace(image, data) file "c:\users\sherif\onedrive\pyhton projects\kivy tests\stepic.py", line 75, in encode_inplace pixel in encode_imdata(image.getdata(), data): file "c:\users\sherif\onedrive\pyhton projects\kivy tests\stepic.py", line 58, in encode_imdata byte = ord(data[i]) typeerror: ord() expected string of length 1, int found
there 2 ways convert string.
text = open("source.zip", "r", encoding='utf-8', errors='ignore').read()
with output
pkn!k\z sec.txt13 byte 1.10mpkn!k\z sec.txtpk52
or
text = str(open("source.zip", "rb").read())
with output
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'
i used second , got same string retrival.
in order reconstruct zip file (output string), use code
plaintext = open("out.zip", "w") plaintext.write(output) plaintext.close()
but written file says corrupted when try open it. when try read written it, either
output = output.encode(encoding='utf_8', errors='strict')
or
output = bytes(output, 'utf_8')
the output
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'"
which different source file.
what have reconstruct embedded file faithfully?
when read file in rb
mode, you'll byte array. if print it, may string, each individual element integer.
>>> my_bytes = b'hello' >>> my_bytes b'hello' >>> my_bytes[0] 104
this explain error
"c:\users\sherif\onedrive\pyhton projects\kivy tests\stepic.py", line 58, in encode_imdata byte = ord(data[i]) typeerror: ord() expected string of length 1, int found
ord()
expects string, have convert bytes strings. unfortunately, str(some_byte_array)
doesn't think does. creates literal string representation of byte array, including preceeding "b" , surrounding quotes.
>>> string = str(my_bytes) >>> string[0] 'b' >>> string[1] "'" >>> string[2] 'h'
what want instead convert each byte (integer) string individually. map(chr, some_byte_array)
you. have because stepic expects string. when embeds character, ord(data[i])
, converts string of length 1 unicode code (integer).
furthermore, can't leave our string map object, because code needs calculate length of whole string before embedding it. therefore, ''.join(map(chr, some_bytearray))
have use our input secret.
for extraction stepic opposite. extracts secret byte byte , turns them strings chr(byte)
. in order reverse that, need ordinal value of each character individually. map(ord, out)
should trick. , since want write our file in binary, further feeding bytearray()
take care of everything.
overall, these changes should make code.
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()
Comments
Post a Comment