在做CTF密碼題時很大的坑點就在編碼,中間有一個弄錯就出不來結果。正好python在這塊比較坑,記錄一下。以下是各種需求對應的輸出:
1. 字符串轉16進制ascii碼串:
txt='ABC' new=txt.encode('utf-8').hex() print(type(new), new)
輸出:
<class 'str'> 414243
2.ascii碼串轉字符串:
code='3041' new=bytes.fromhex(code).decode() print(type(new), new)
輸出:
<class 'str'> 0A
3.字符串形式的16進制,轉字節串
str='A7B7' c=bytes.fromhex( str ) print(c)
輸出:
b'\xa7\xb7'
4.字節串轉16進制串
code=b'\xa7\xb7' new=code.hex() print(new)
輸出:
a7b7
5.base64編碼解碼:
from base64 import *
txt='aGVsbG8=' print(b64decode(txt))
輸出:
b'hello'
輸出是bytes,如果想要字符串就decode一下. 因為往往解完base64后還要其他的操作, 默認輸出bytes就很方便了.
編碼也類似,用b64encode, 此處省略.
.