title: How to store cipher text in Django Models
body: <p>I am working on a <code>Cryptography Application</code> in Django 1.8 and trying to store <code>Cipher Text</code> in my model field. Below is my <code>Message</code> model:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I am using the following <a href="http://docs.python-guide.org/en/latest/scenarios/crypto/" rel="nofollow noreferrer">pycrypto</a> module in Python to encrypt the message and store the cipher text in my Django Model. </p>
  
  <p>Code for Encryption and Decryption is here:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Now suppose <code>User</code> enters a message <code>My life is Beautiful</code>, then you can see the encrypted message will be:</p>
  
  <pre><code>'encrypted_message':
  &gt; u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd',
  </code></pre>
  
  <blockquote>
    <p>{'message': u'Life is beautiful ', 'user_name': ,
    'encrypted_message':
    u'\ufffdH\x060\ufffd!W\ufffdooK8\ufffdg\ufffd\ufffd\ufffd\ufffd',
    'hashed_message':
    u'8ada92984f1fc55010c4d2fa38d0fba499691bc746f83eff089ba5212a65f083a947aa1fe6209f05278a5dc7ee12b361'}</p>
  </blockquote>
  
  <p>But the problem is when I am storing this <code>Cipher Text</code> inside my model, it is coming out be some weird characters which I cannot <code>decrypt</code> again. Could anyone help me how can I store <code>cipher text</code> inside my model field and then <code>decrypt</code> it. </p>
  
  <p><a href="https://i.stack.imgur.com/3oPKZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3oPKZ.png" alt="enter image description here"></a></p>