[英]stuck in a python code for encryption. error is Unknown format code 'x' for object of type 'str'
我對編程和密碼學很陌生,但我參加了一個 CTF 競賽,那里為我們提供了一個十六進制,我們應該破解它。 通過一些工作和研究,我得到了這個代碼
import binascii
my_ciphertext = "0f05080e1220360106190c3610061c360207061e361e01081d4e1a2e0600070e362607210c1b0c4814"
binary_rep_of_ciphertext = binascii.unhexlify(my_ciphertext)# makes it binary
array_of_ciphertext = bytearray(binary_rep_of_ciphertext)#makes binary things to array elements
def xor_string_and_char(my_char_value):
result= ''.join([chr(cc ^ my_char_value) for cc in array_of_ciphertext])
return '{:x}'.format(result) # convert back to hexadecimal
x = 0
assert x==0
while x in range(255):
my_plaintext = xor_string_and_char(x)
print('b' + my_plaintext)
x=x+1
但我不斷收到錯誤,我不知道如何解決它。 我不確定代碼有什么問題,因為我根本不擅長 python(ps:請使用新手語言解釋)錯誤:未知格式代碼 'x' for object of type 'str'
問題是沒有為字符串提供十六進制格式,這些字符串用於表示代碼中的單個字符/字節。 實際上,您應該使用整數來表示字節,並且 integer arrays 來表示多個字節,其中每個 integer 都在 0..255 范圍內
Python 中的字符串幸運地用於文本,由字符而不是字節組成,它們不應該用於表示字節。 如果您的加密算法輸入 / output 字節(如果您使用 XOR,通常會出現這種情況),那么除了報告/調試目的之外,不應使用字符串。
如果要將字符串顯示為十六進制,則可以使用hexlify
,它與代碼中使用的unhexlify
相反。 但這似乎是一種老派的方法,因為 Python >= 3.5 似乎提供了其他方法來處理字節 arrays 和十六進制編碼/解碼......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.