我想将以下Python 2代码转换为Python 3:
# Consts
HEADER_SIZE = 54 # Header size of BMP
DELIMITER = "$" # Separator between number of characters and text
# User Configurations
TextToHide = "I Am Secret Information!"
ImageFile = "C:/Users/Sherif/OneDrive/P3/FW.bmp"
StegImageFile = "C:/Users/Sherif/OneDrive/P3/hidden_cat.bmp"
class LSBEncrypter(object):
def __init__(self):
self.image_byte_counter = 0
self.new_image_data = ''
self.original_image = ''
self.text_to_hide = ''
def open_image(self):
# Open the image file
with open(ImageFile, "rb") as f:
self.original_image = f.read()
# Reading first chunk of the file - we don't want to overwrite the header
def read_header(self):
for x in range(0, HEADER_SIZE):
self.new_image_data += self.original_image[x]
self.image_byte_counter += 1
def hide_text_size(self):
sz = len(self.text_to_hide)
s_sz = str(sz)
s_sz += DELIMITER # s_sz now equal to size of text to hide + Delimiter
self.do_steg(s_sz)
# Hides the text in the image.
# Does that by replacing the bytes LSB (Least significant bit) to be our bit
def do_steg(self, steg_text):
# Goes through the text we want to hide, char by char
for ch in range(0, len(steg_text)):
current_char = steg_text[ch] # Gets current Char
current_char_binary = '{0:08b}'.format(ord(current_char)) # Gets the binary value of current character
# Goes through current char binary - bit by bit
for bit in range(0, len(current_char_binary)):
new_byte_binary = ''
### Overwriting the image's byte LSB with our current Bit
# Gets the binary value of original image byte
current_image_binary = '{0:08b}'.format(ord(self.original_image[self.image_byte_counter]))
# Copies the first 7 bits (we want them to be the same)
new_byte_binary = current_image_binary[:7]
# Set the last bit to be our bit
new_byte_binary += current_char_binary[bit]
# Gets the new char value by it's binary
new_byte = chr(int(new_byte_binary, 2))
# Adds new byte to output
self.new_image_data += new_byte
self.image_byte_counter += 1
def copy_rest(self):
# Copies the what's left of the file
self.new_image_data += self.original_image[self.image_byte_counter:]
def close_file(self):
with open(StegImageFile, "wb") as out:
out.write(self.new_image_data)
def run(self, stega_text):
self.text_to_hide = stega_text
self.open_image()
self.read_header()
self.hide_text_size()
self.do_steg(self.text_to_hide)
self.copy_rest()
self.close_file()
def main():
global TextToHide
stega_instance = LSBEncrypter()
stega_instance.run(TextToHide)
print ("Successfully finished hiding text")
if __name__ == '__main__':
main()
但是,我得到以下错误:
Traceback (most recent call last): File "C:\Users\Sherif\OneDrive\P3\simple.py", line 93, in main() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 89, in main stega_instance.run(TextToHide) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 80, in run self.read_header() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 28, in read_header self.new_image_data += self.original_image[x] TypeError: Can't convert 'int' object to str implicitly
所以我改变了:
def read_header(self):
for x in range(0, HEADER_SIZE):
self.new_image_data += str(self.original_image[x])
self.image_byte_counter += 1
但出现以下错误:
Traceback (most recent call last): File "C:\Users\Sherif\OneDrive\P3\simple.py", line 93, in main() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 89, in main stega_instance.run(TextToHide) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 81, in run self.hide_text_size() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 35, in hide_text_size self.do_steg(s_sz) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 54, in do_steg current_image_binary = '{0:08b}'.format(ord(self.original_image[self.image_byte_counter])) TypeError: ord() expected string of length 1, but int found
所以我删除了0rd():
def do_steg(self, steg_text):
for ch in range(0, len(steg_text)):
current_char = steg_text[ch]
current_char_binary = '{0:08b}'.format((current_char))
并得到以下错误:
Traceback (most recent call last): File "C:\Users\Sherif\OneDrive\P3\simple.py", line 104, in main() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 100, in main stega_instance.run(TextToHide) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 92, in run self.hide_text_size() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 35, in hide_text_size self.do_steg(s_sz) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 55, in do_steg current_char_binary = '{0:08b}'.format((current_char)) # Gets the binary value of current character ValueError: Unknown format code 'b' for object of type 'str'
我尝试将所有函数转换为字节:
def do_steg(self, steg_text):
for ch in range(0, len(steg_text)):
current_char = steg_text[ch]
current_char_binary = '{}'.format(current_char.encode('utf-8')) # Gets the binary value of current character
for bit in range(0, len(current_char_binary)):
new_byte_binary = ''
current_image_binary = '{}'.format((self.original_image[self.image_byte_counter]))
new_byte_binary = current_image_binary[:7]
new_byte_binary += current_char_binary[bit]
new_byte = new_byte_binary
self.new_image_data += new_byte
self.image_byte_counter += 1
但我还是犯了个错误:
Traceback (most recent call last): File "C:\Users\Sherif\OneDrive\P3\simple.py", line 94, in main() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 90, in main stega_instance.run(TextToHide) File "C:\Users\Sherif\OneDrive\P3\simple.py", line 84, in run self.copy_rest() File "C:\Users\Sherif\OneDrive\P3\simple.py", line 72, in copy_rest self.new_image_data += self.original_image[self.image_byte_counter:] TypeError: Can't convert 'bytes' object to str implicitly
如果没有这些错误,如何转换为python3?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐