我正在尝试读取一个二进制文件,然后用一个字节读取它的每个字节,然后将每个字节写入一个单独的输出文件(也作为二进制文件),但是我遇到了很多麻烦在python工作。
编辑:现在我在.write()行的类型有错误。我没有编码就得到字符串参数。但原始文件是非文本文件,因此不应该用utf-8或ascii编码?我做错了什么?
#!/usr/bin/python3
#I originally had the utf-8 encoding line here but it didn't seem to be helping so I removed it
import sys
def xor(in,key):
if (in == key or key == chr(0x00)):
return in
else:
return chr(ord(in) ^ ord(key))
def main():
inFile = open(sys.argv[1], 'rb')
b = bytearray(inFile.read())
#output = []
outFile = open('decryptedFile.bmp', 'wb+')
for i in range(len(b)):
temp = xor(chr(b[i]),chr(0xf1))
outFile.write(bytearray(temp))
#output.append(temp)
#print(output)
#outFile.write(output)
outFile.close()
inFile.close()
if __name__ == "__main__":
main()
编辑:这里有注释,但是当打印输出时会打印出类似['D','K','Ò'等'的东西,这样看起来似乎有效?