简体   繁体   English

卡在 python 代码中进行加密。 错误是“str”类型的 object 的未知格式代码“x”

[英]stuck in a python code for encryption. error is Unknown format code 'x' for object of type 'str'

I am very new to programming and cryptography, but I took part in a CTF competition where the provided us with a hex and we are supposed to crack it.我对编程和密码学很陌生,但我参加了一个 CTF 竞赛,那里为我们提供了一个十六进制,我们应该破解它。 Through some work and research, I got this code通过一些工作和研究,我得到了这个代码

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

but I keep getting an error, and I dont know how to fix it.但我不断收到错误,我不知道如何解决它。 I am not sure what is wrong with the code cause I am not good at python at all (ps: please use newbie language to explain) error: Unknown format code 'x' for object of type 'str'我不确定代码有什么问题,因为我根本不擅长 python(ps:请使用新手语言解释)错误:未知格式代码 'x' for object of type 'str'

The problem is that the formatting to hexadecimals is not provided for strings, which are used to represent a single character / byte in your code.问题是没有为字符串提供十六进制格式,这些字符串用于表示代码中的单个字符/字节。 Actually, you should use integers to represent bytes, and integer arrays to represent multiple bytes where each integer is in the range 0..255 inclusive.实际上,您应该使用整数来表示字节,并且 integer arrays 来表示多个字节,其中每个 integer 都在 0..255 范围内

Strings in Python are fortunately used for text, consisting of characters rather than bytes, and they should not be used to represent bytes. Python 中的字符串幸运地用于文本,由字符而不是字节组成,它们不应该用于表示字节。 If your cryptographic algorithm inputs / output bytes (which is the commonly the case if you use XOR) then strings should not be used other than for reporting / debugging purposes.如果您的加密算法输入 / output 字节(如果您使用 XOR,通常会出现这种情况),那么除了报告/调试目的之外,不应使用字符串。

If you want to present a string as hex, then you can use hexlify which is the reverse of unhexlify used in your code.如果要将字符串显示为十六进制,则可以使用hexlify ,它与代码中使用的unhexlify相反。 But it seems rather an old school method as Python >= 3.5 seems to supply other methods to handle byte arrays and hexadecimal encoding / decoding ...但这似乎是一种老派的方法,因为 Python >= 3.5 似乎提供了其他方法来处理字节 arrays 和十六进制编码/解码......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ str”的对象的未知格式代码“ b” - Unknown format code 'b' for object of type 'str' “str”类型对象的未知格式代码“g” - Unknown format code 'g' for object of type 'str' Rc4 decrypt ValueError:object 类型为“str”的未知格式代码“x” - Rc4 decrypt ValueError: Unknown format code 'x' for object of type 'str' ValueError:类型为“ str”的对象的未知格式代码“ g” - ValueError: Unknown format code 'g' for object of type 'str' ValueError: 类型“str”的对象的格式代码“e”未知 - ValueError: Unknown format code 'e' for object of type 'str' ValueError:类型为“str”的 object 的未知格式代码“f” - ValueError: Unknown format code 'f' for object of type 'str' 类型为&#39;str&#39;的对象的未知格式代码&#39;f&#39;-Folium - Unknown format code 'f' for object of type 'str'- Folium python 中的 DES 加密错误(TypeError: Object 类型<class 'str'>不能传给 C 码)</class> - DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code) 在代码中格式化不断导致 ValueError: Unknown format code 'f' for object of type 'str'? - Formatting in a code constantly causes the ValueError: Unknown format code 'f' for object of type 'str'? ValueError:类型为&#39;str&#39;的对象的未知格式代码&#39;f&#39;-为什么第二次却不是第一次? - ValueError: Unknown format code 'f' for object of type 'str' - why do I get this the second time but not the first time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM