问题描述
我有一个脚本“编码”用户输入的文本,现在默认情况下它只是一个字符串“ python”。我在解码时遇到问题。
这是f.tell的输出,在这里我可以看到它仅从每一行读取第一个字节,并且仅输出“ pto”而不是“ python”。
2
p2
2
2
t2
2
2
o2
2
2
Traceback (most recent call last):
File "...\file.py",line 73,in <module>
oa=ord(a)
TypeError: ord() expected a character,but string of length 0 found
>>>
它以某种方式读取没有问题,直到找到长度为0的字符串为止,我找不到它在哪里,因为text.bin的间距相等,就像key.bin一样。
在text.bin文件上运行xxd -b显示文件包含解码所需的字节。
00000000: 00000000 00000000 01110000 00000000 00000000 01111001 ..p..y
00000006: 00000000 00000000 01110100 00000000 00000000 01101000 ..t..h
0000000c: 00000000 00000000 01101111 00000000 00000000 01101110 ..o..n
00000012: 00000000 00000000 ..
key.bin也包含它们。这里的第一个字节(odd)是text中的每个字符之间的偏移量。第二个字节(偶数)是XOR掩码。之所以将其设置为0,是因为我没有想到一种生成 symetrical 字节的方法来在最后执行xor。我想我需要为此使用XOR密码。
00000000: 00000010 00000000 00000010 00000000 00000010 00000000 ......
00000006: 00000010 00000000 00000010 00000000 00000010 00000000 ......
0000000c: 00000010 00000000 00000010 00000000 00000010 00000000 ......
00000012: 00000010 00000000 00000010 00000000 00000010 00000000 ......
00000018: 00000010 .
这是当前代码
fdata = open("text.bin","wb") ; fMeta = open("key.bin","w+b")
print('Enter a text:')
txt='python' # input()
print('Binary:')
print(" ".join(txt))
l=len(txt)
print(l,'bytes')
strtobin= ' '.join(format(x,'b') for x in bytearray(txt,'utf-8'))
print(strtobin)
shift=int(2)
sh=nobv.to_bytes(1,byteorder='big')
# even bytes in key.bin
for v in range(0,25,2): # len(txt)
#print(v)
fMeta.seek(v)
fMeta.write(sh)
# odd bytes in key.bin (contains first part for XOR)
for a in range(0,25): #len(txt)+1
if a % 2 != 0:
#print(a)
fMeta.seek(a)
fMeta.write(b'\x00')
pad = b'\x00\00'
for line in txt:
for char in line:
fdata.write(pad)
fdata.write(char.encode())
fdata.write(pad)
fdata.close() ; fMeta.close()
f = open ("key.bin","rb"); d = open ("text.bin","rb")
f.seek(0); d.seek(0) ; position = 0
while 1:
#f.seek(2,0)
offset = f.read(1)
f.seek(1,0)
mask = f.read(1)
if not offset: break;
if not mask: break;
shift = int(ord(offset))
position = position + shift
d.seek(position)
print(f.tell())
a = d.read(1)
oa=ord(a)
om=ord(mask)
output = chr(oa^om)
print (output,end="")
f.close() ; d.close()
解决方法
您的脚本看起来像我可以帮助的东西,所以我认为我应该尝试旧的大学尝试。
第一个问题在第15行:您没有定义“ sh”;同样,您编写“范围内”块的方式在很多系统上也不起作用,因此我使它看起来更像您的其他循环。
for v in range(0,25):
if v % 2 == 0:
#print(v)
fmeta.seek(v)
fmeta.write(sh)
具体来说,我不确定您在使用该变量的用途,所以我只添加了这一行
sh = b'\x01'
在测试脚本时,将其移到顶部的变量列表中。弄清楚以后可以很容易地发现脚本的实际作用。
现在,我现在得到了错误
oa=ord(a)
TypeError: ord() expected a character,but string of length 0 found
我怀疑问题出在这两行:
38:offset = f.read(1)
和
45:shift = int(ord(offset))
您的程序正在从文件“ key.bin”中读取偏移量,然后将其逐步移至文件“ text.bin”中,并且当其移至a = d.read(1)
时,有时会读取EOF或a空值。
我不确定在这里如何解决脚本,因为我不了解您的目标输出是什么,但是无论如何我希望我能提供帮助:)