I am working on a Cryptography Application
in Django 1.8 and trying to store Cipher Text
in my model field. Below is my Message
model:
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)
I am using the following pycrypto module in Python to encrypt the message and store the cipher text in my Django Model.
Code for Encryption and Decryption is here:
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)
Now suppose User
enters a message My life is Beautiful
, then you can see the encrypted message will be:
'encrypted_message':
> u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd',
{'message': u'Life is beautiful ', 'user_name': , 'encrypted_message': u'\�H\\x060\�!W\�ooK8\�g\�\�\�\�', 'hashed_message': u'8ada92984f1fc55010c4d2fa38d0fba499691bc746f83eff089ba5212a65f083a947aa1fe6209f05278a5dc7ee12b361'}
But the problem is when I am storing this Cipher Text
inside my model, it is coming out be some weird characters which I cannot decrypt
again. Could anyone help me how can I store cipher text
inside my model field and then decrypt
it.
您可以使用base64.b64encode()
和base64.b64decode()
文本带入不会破坏HTML形式的可读形式。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.