如何加密 pyaes 包中的纯文本?
how to encrypt a plain text in pyaes package?
我尝试使用 pyaes
包加密 python 中的文本:
import os
import pyaes
aes = pyaes.AESModeOfOperationCTR(os.urandom(16))
result = aes.encrypt("test")
但我收到一个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte
我也试过了aes.encrypt("test".encode())
我哪里做错了?
您可能正在尝试将结果序列化为 json
。
加密结果为bytes
,这些字节无法直接解码为字符串
改用base64,如问题
result = aes.encrypt("test")
to_dump = base64.b64encode(result).decode()
import json
json.dumps(to_dump) # this would output a b64encoded version of your secret, which could later be decoded and decrypted on the client.
我尝试使用 pyaes
包加密 python 中的文本:
import os
import pyaes
aes = pyaes.AESModeOfOperationCTR(os.urandom(16))
result = aes.encrypt("test")
但我收到一个错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid continuation byte
我也试过了aes.encrypt("test".encode())
我哪里做错了?
您可能正在尝试将结果序列化为 json
。
加密结果为bytes
,这些字节无法直接解码为字符串
改用base64,如
result = aes.encrypt("test")
to_dump = base64.b64encode(result).decode()
import json
json.dumps(to_dump) # this would output a b64encoded version of your secret, which could later be decoded and decrypted on the client.