我想将以下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(最近一次调用最后一次):文件 " C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第93行,在 main()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第89行,主要 stega_instance.run(TextToHide)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第80行,运行中 self.read_header()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第28行,在read_header中 self.new_image_data + = self.original_image [x] TypeError:无法转换' int'隐含地反对str
所以我改变了:
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(最近一次调用最后一次):文件 " C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第93行,在 main()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第89行,主要 stega_instance.run(TextToHide)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第81行,运行中 self.hide_text_size()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第35行,在hide_text_size中 self.do_steg(s_sz)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第54行,在do_steg中 current_image_binary =' {0:08b}' .format(ord(self.original_image [self.image_byte_counter])) TypeError:ord()期望的字符串长度为1,但是找到了int
所以我删除了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(最近一次调用最后一次):文件 " C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第104行,in main()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第100行,主要 stega_instance.run(TextToHide)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第92行,运行中 self.hide_text_size()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第35行,在hide_text_size中 self.do_steg(s_sz)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第55行,在do_steg中 current_char_binary =' {0:08b}' .format((current_char))#获取当前字符的二进制值ValueError:未知格式代码' b' 对于类型' str'
的对象
我尝试将所有函数转换为byte:
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(最近一次调用最后一次):文件 " C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第94行,in main()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第90行,主要 stega_instance.run(TextToHide)文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第84行,运行中 copy.crest中的self.copy_rest()文件" C:\ Users \ Sherif \ OneDrive \ P3 \ simple.py",第72行 self.new_image_data + = self.original_image [self.image_byte_counter:] TypeError:无法转换 '字节'隐含地反对str
如何在没有这些错误的情况下转换为Python 3?