在python中循环使用8byte十六进制代码

2024-04-27 18:47:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个8字节的十六进制代码,我试图从最后一个字节循环到第一个字节的00-FF,但我对python还不熟悉,我被卡住了。在

以下是循环00-FF的代码:

for h in range(0, 256):
    return "{:02x}".format(h) 

基本上我所拥有的是

^{pr2}$

编辑:我将添加一些关于这个的背景信息,并删除我之前的解释。我在写一个用DES的填充攻击算法。需要做的是:

hexcode = 'f20bdba6ff29eed7'
for i in range(0,8):
    (FOR i = 0 ) Loop through 00-FF for the last byte (i.e. 'd7') until you find the value that works
    (FOR i = 1 ) Loop through 00-FF for the 7th byte (i.e. 'ee') until you find the value that works
    and so on until the 1st byte

我的问题:我的问题是我不知道如何循环通过所有8字节的十六进制。当它是第8个字节时,这很简单,因为我只删除最后两个元素,循环00-FF并将其添加回hex,看看它是否是正确的代码。基本上,代码的作用是:

remove last two elements of hex
try 00 as the last two elements
if the test returns true then stop
if it doesn't move to 01 (cont. through FF, but stop when you find the correct value)

我的问题是,当字节在中间(2-7)时,我如何循环获取一个中间值,然后将其全部相加。本质上

For byte 7:
Remove elements 13,14 from hex
try 00 as element 13,14
add hex[0:12] + 00 + hex [15:16]
if 00 returns true then stop, if not then loop through 01-ff until it does

对于字节6、5、4、3、2、1,以此类推


Tags: the代码youforif字节valueelements
1条回答
网友
1楼 · 发布于 2024-04-27 18:47:50

这里有一个快速的方法将字符串分成块。我们假设它的长度相等。在

hexcode = 'f20bdba6ff29eed7'
l = len(hexcode)
for n in range(0, len(hexcode)/2):
    index = n * 2
    print hexcode[index:index+2]

如果需要对字符串表示进行操作,可以使用类似的方法轻松地生成一个由两个字符组成的字节码列表。就个人而言,我更喜欢对字节进行操作,并且只对IO使用hexcode。在

^{pr2}$

相关问题 更多 >