ctf經(jīng)常遇到進(jìn)制轉(zhuǎn)換的問(wèn)題,就正好做一個(gè)進(jìn)制轉(zhuǎn)換總結(jié),分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)一起看看詳細(xì)的介紹:
惠濟(jì)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
字符串與十六進(jìn)制轉(zhuǎn)換
例如百度ctf 12月的第二場(chǎng)第一個(gè)misc
?
1
666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D
比較簡(jiǎn)單的一種做法就是直接調(diào)用字符串的.decode('hex')解密即可, 但如果不用這個(gè)函數(shù)你會(huì)怎么解呢?
一種思路就是先2個(gè)分組,解出每組的ascii值,合并下字符串即可得到,具體代碼如下
?
1234567
import res='666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D's = re.findall(r'.{2}',s)s = map(lambda x:chr(int(x,16)),s)print ''.join(s)flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
前面說(shuō)了字符串的decode('hex')函數(shù),另外還有兩個(gè)轉(zhuǎn)16進(jìn)制的函數(shù),這里都總結(jié)一下
內(nèi)置函數(shù)hex()
只能轉(zhuǎn)換10進(jìn)制整數(shù)為十六進(jìn)制,不能轉(zhuǎn)字符串
binascii庫(kù)的hexlify()和b2a_hex()
這兩個(gè)函數(shù)的功能是將字符串轉(zhuǎn)換成十六進(jìn)制,對(duì)應(yīng)的解密函數(shù)分別為 unhexlify()和a2b_hex()
進(jìn)制互轉(zhuǎn)
二進(jìn)制,八進(jìn)制,十六進(jìn)制轉(zhuǎn)10進(jìn)制比較簡(jiǎn)單,直接調(diào)用
int函數(shù)
?
1
int(str,base) //返回十進(jìn)制整數(shù),但注意此時(shí)第一個(gè)參數(shù)為字符串
對(duì)應(yīng)的解密函數(shù)分別是
?
12345
bin() //10進(jìn)制轉(zhuǎn)二進(jìn)制 oct() //十進(jìn)制轉(zhuǎn)八進(jìn)制 hex() //十進(jìn)制轉(zhuǎn)十六進(jìn)制
但二進(jìn)制直接轉(zhuǎn)16進(jìn)制就需要多走一步了,先用int轉(zhuǎn)十進(jìn)制,在用上面提到的hex()函數(shù)將十進(jìn)制轉(zhuǎn)換成十六進(jìn)制,比較精簡(jiǎn)的寫(xiě)法是
?
1
map(lambda x:hex(int(x,2)),['0011']) //lambda表達(dá)式
或者是
?
1
[hex(int(x,2)) for x in ['0011']] //列表解析
對(duì)應(yīng)的解密函數(shù)就是
?
1
map(lambda x:bin(int(x,16)),['ef'])
最后在附上自己用python寫(xiě)的一個(gè)進(jìn)制轉(zhuǎn)換小工具,主要功能是對(duì)一組二進(jìn)制,或者ascii,或十六進(jìn)制轉(zhuǎn)換成字符串,想必ctf上也經(jīng)常會(huì)遇到這類(lèi)題型吧
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344
# make by 江sir#coding:utf-8import reimport argparse def bintostr(text): text = text.replace(' ','') text = re.findall(r'.{8}',text) s = map(lambda x:chr(int(x,2)),text) #批量二進(jìn)制轉(zhuǎn)十進(jìn)制 flag = ''.join(s) return flag def asciitostr(text): if ' ' in text: text = text.split(' ') elif ',' in text: text = text.split(',') s = map(lambda x:chr(int(x)),text) flag = ''.join(s) return flag def hextostr(text): text = re.findall(r'.{2}',text) #print text s = map(lambda x:chr(int(x,16)),text) #print s flag = ''.join(s) return flag if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-b") parser.add_argument("-a") parser.add_argument("-x") argv = parser.parse_args() #print argv if argv.b: res = bintostr(argv.b) elif argv.a: res = asciitostr(argv.a) elif argv.x: res = hextostr(argv.x) print res
用法:
十六進(jìn)制轉(zhuǎn)字符串:
666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D
?
12
bintostr.py -x "666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D"flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
二進(jìn)制轉(zhuǎn)字符串:
可以有空格,也可以無(wú)空格
00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100
?
12
bintostr.py -b "00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100"/f6732410aadc037fb0cbaa00c7531373.txt
ascii轉(zhuǎn)字符串
可以是空格分隔,也可以是,分隔
s='45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32'
?
12
bintostr.py -a "45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32"-.-. - ..-. .-.. ... -... ..--.- -... ... .-..
以上實(shí)例均來(lái)自某些ctf賽題
總結(jié)
把十六進(jìn)制的字串轉(zhuǎn)為十進(jìn)制數(shù)字:
Python代碼
print int('ff', 16)
255
print int('ff', 16)
255
把十進(jìn)制數(shù)字轉(zhuǎn)換為以十六進(jìn)制表示之字串,可調(diào)用內(nèi)置的hex()函數(shù):
Python代碼
print hex(255)
0xff
print hex(255)
0xff
調(diào)用BinAscii模塊其中的b2a_hex()函數(shù),可把以ASCII編碼的文字以十六進(jìn)制表示:
Python代碼
print binascii.b2a_hex('A')
41
print binascii.b2a_hex('A')
41
反之也可把以十六進(jìn)制表示的文字,換成以ASCII編碼的文字:
Python代碼
print binascii.a2b_hex('41')
“A”
#coding=gbkvar=input("請(qǐng)輸入十六進(jìn)制數(shù):")b=bin(int(var,16))print(b[2:])運(yùn)行結(jié)果 詳細(xì)請(qǐng)參考python自帶int函數(shù)、bin函數(shù)用法 參考網(wǎng)址: class int(x, base=10) Return...
在python中,十進(jìn)制轉(zhuǎn)換十六進(jìn)制使用hex()函數(shù)。
如:hex(10),十六進(jìn)制數(shù)為0xa
hex(17),十六進(jìn)制數(shù)為0x11
#coding=gbk
var=input("請(qǐng)輸入十六進(jìn)制數(shù):")
b=bin(int(var,16))
print(b[2:])
運(yùn)行結(jié)果
詳細(xì)請(qǐng)參考python自帶int函數(shù)、bin函數(shù)用法
參考網(wǎng)址:
class?int(x,?base=10)
Return an?integer object constructed from a number or string?x, or return?0?if no arguments are given. If?x?is a number, return?x.__int__(). For floating point numbers, this truncates towards zero.
If?x?is not a number or if?base?is given, then?x?must be a string,?bytes, or?bytearray?instance representing an?integer literal?in radix?base. Optionally, the literal can be preceded by?+?or?-?(with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with?a?to?z?(or?A?to?Z) having values 10 to 35. The default?base?is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with?0b/0B,?0o/0O, or?0x/0X, as with?integer literals in code. Base 0 means to?interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that?int('010',?0)?is not legal, while?int('010')?is, as well as?int('010',?8).
The?integer type is described in?Numeric Types — int, float, complex.
Changed in version 3.4:?If?base?is not an instance of?int?and the?base?object has a?base.__index__?method, that method is called to obtain an?integer for the base. Previous versions used?base.__int__?instead of?base.__index__.
Changed in version 3.6:?Grouping digits with underscores as in code literals is allowed.
2.bin(x)
Convert an?integer number to a binary string. The result is a valid Python expression. If?x?is not a Python?int?object, it has to define an?__index__()?method that returns an integer.
a='0x0012e' b= hex(eval(a)) print b 輸出 0x12e 注意,一般計(jì)算機(jī)的十六進(jìn)制數(shù)直接輸出的時(shí)候是不補(bǔ)0的,所以 0x12e 就是 0x0012e,就好像 0005和5在整型數(shù)是存儲(chǔ)成一樣的值。
新聞標(biāo)題:關(guān)于十六進(jìn)制函數(shù)python的信息
分享網(wǎng)址:http://jinyejixie.com/article40/dossieo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、虛擬主機(jī)、關(guān)鍵詞優(yōu)化、ChatGPT、全網(wǎng)營(yíng)銷(xiāo)推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)