gpt4 book ai didi

python - 如何在 Django Models 中存储密文

转载 作者:行者123 更新时间:2023-12-01 04:18:16 25 4
gpt4 key购买 nike

我正在 Django 1.8 中开发加密应用程序,并尝试在我的模型字段中存储密文。以下是我的 Message 模型:

class Message(models.Model):
user_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user_name = models.ForeignKey(User)
message = models.TextField()
encrypted_message = models.CharField(max_length=200, null=True, blank=True)
hashed_message = models.CharField(max_length=100, null=True, blank=True)

def __unicode__(self):
return unicode(self.user_id)

我正在 Python 中使用以下 pycrypto 模块来加密消息并将密文存储在我的 Django 模型中。

加密和解密的代码在这里:

from Crypto.Cipher import AES
# Encryption

encryption_suite = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
cipher_text = encryption_suite.encrypt("Life is Beautiful")

# Decryption

decryption_suite = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
plain_text = decryption_suite.decrypt(cipher_text)

现在假设用户输入一条消息My life is Beautiful,那么您可以看到加密的消息将是:

'encrypted_message':
> u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd',

{'message': u'Life is beautiful ', 'user_name': , 'encrypted_message': u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd', 'hashed_message': u'8ada92984f1fc55010c4d2fa38d0fba499691bc746f83eff089ba5212a65f083a947aa1fe6209f05278a5dc7ee12b361'}

但问题是,当我将这个密文存储在我的模型中时,它会出现一些奇怪的字符,我无法再次解密。谁能帮助我如何将密文存储在我的模型字段中,然后解密它。

enter image description here

最佳答案

您可以使用 base64.b64encode()base64.b64decode() 将超文本转换为不会破坏 HTML 表单的可读形式。

关于python - 如何在 Django Models 中存储密文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34057451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com