我正在使用Django 1.8中的Cryptography Application
,并试图在我的模型字段中存储Cipher Text
。下面是我Message
型号:如何在Django模型中存储密文
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)
我使用以下pycrypto模块在Python对消息进行加密并存储在我的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)
现在假设User
进入消息My life is Beautiful
,然后就可以看到加密的消息将是:
'encrypted_message':
> u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd',
{ '消息' :u'Life is beautiful','user_name':, 'encrypted_message': u'\ ufffdH \ x060 \ ufffd!W \ ufffdooK8 \ ufffdg \ ufffd \ ufffd \ ufffd \ ufffd', “hashed_message”: u'8ada92984f1fc55010c4d2fa38d0fba499691bc746f83eff089ba5212a65f083a947aa1fe6209f05278a5dc7ee12b361' }
但问题是,当我存储这些Cipher Text
我的模型中,它是我不能decrypt
再次出山是一些奇怪的字符。任何人都可以帮助我如何将cipher text
存储在我的模型字段中,然后decrypt
它。
''str'对象没有属性'base64''得到这个错误! – python
'base64.b64encode(your_string)' –
我想将密文转换为'hexcode'并保存到我的数据库中。但问题是如何将'hexcode'重新转换为密文解密? – python