python - 属性错误 : 'str' object has no attribute 'decode' Having this error on the second line of my code

标签 python algorithm encryption

这是我的代码,

if __name__ == "__main__":
    key = "0123456789abcdef0123456789abcdef".decode('hex')  /this line is having error
    plain_1 = "1weqweqd"
    plain_2 = "23444444"
    plain_3 = "dddd2225"
    print(plain_1)
    print(plain_2)
    print(plain_3)

    cipher = Present(key)

输出

AttributeError: 'str' object has no attribute 'decode'

最佳答案

这是因为你试图解码一个字符串。 bytes类型可以解码,str类型不能解码。你应该编码 (key.encode()) this (or use b"foo") before, to convert the string to a bytes object .

>>> foo = "adarcfdzer"
>>> type(foo)
<class 'str'>
>>> foo = foo.encode()
>>> type(foo)
<class 'bytes'>
>>> foo = foo.decode()
>>> type(foo)
<class 'str'>

关于python - 属性错误 : 'str' object has no attribute 'decode' Having this error on the second line of my code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478951/

相关文章:

Python Regex - 在日期之前或之后高效

python - 在 Flask 中迁移 ValueError : invalid interpolation syntax in connection string at position 15

c# - 用于所有匹配的 Boyer-Moore-Horspool 算法(在字节数组中查找字节数组)

python - 查找可能的唯一固定长度排列数的最有效方法?

Java 7 : output from encryption with public RSA key using cipher "RSA/ECB/OAEPWithSHA1AndMGF1Padding" does not match with openssl command

使用 BouncyCaSTLe SSL 使用 keyFile 进行 Java AES 加密

python - 每隔 x 小时运行一个进程

python - 将 Spark Dataframe (pyspark.pandas.Dataframe) 从 Azure DataBricks 导出到 Excel 文件

python - 在 Python 中查找第 K 个最大元素的总体复杂性

encryption - 识别密码学密码