簡體   English   中英

循環遍歷python中的8字節十六進制代碼

[英]Looping through an 8-byte hex code in python

我有一個8字節的十六進制代碼,我試圖從最后一個到第一個循環開始每個字節循環00-FF,但我是python的新手,我被卡住了。

這是循環00-FF的代碼:

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

基本上我擁有的是一個

hexcode = 'f20bdba6ff29eed7'

編輯:我將在此添加一些背景信息並刪除我之前的解釋。 我正在用DES寫一個Padding Attack aglorithm。 這是需要發生的事情:

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並將其添加回十六進制以查看它是否是正確的代碼。 基本上代碼的作用是:

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等等

這是將字符串分成塊的快速方法。 我們假設它有均勻的長度。

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

如果您需要對字符串表示進行操作,則可以使用類似於此的內容輕松生成兩個字符字節碼的列表。 就個人而言,我更喜歡操作字節,並僅將十六進制代碼用於IO。

[hexcode[n*2:n*2+2] for n in range(len(hexcode)/2)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM