0

I am using pycrypto to encrypt and decrypt a message using a key. I followed some examples and finally wrote my own. But it turns out that the encrypted message is not in unicode or ascii. I need to store the encrypted message on a mysql database but I won't be able to store such a thing - Oք��fo�A?"���������Ϩ��w}�7. What can I do about this? Do I need to modify my mysql database or there's some other way to use Crypto?

Here is my code.

from Crypto.Cipher import AES
import random

mode = AES.MODE_CBC

def encryptor(key, message):
    IV = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
    encrypto = AES.new(key, mode, IV=IV)
    crypt_message = encrypto.encrypt(message)
    cipher = crypt_message + IV
    return cipher

def decryptor(key, cipher):
    IV = cipher[-16:]
    decrypt_message = cipher[:-16]
    decrpyto = AES.new(key, mode, IV=IV)
    message = decrpyto.decrypt(decrypt_message)
    return message
Himanshu Mishra
  • 8,510
  • 12
  • 37
  • 74
  • 1
    Encryption and base64 are _not_ the same thing. This is _not_ a duplicate posting. Voting to reopen. The problem here concerns how to store binary data in MySQL. Base64 is _part_ of _one_ solution. – Rick James Dec 10 '15 at 04:40
  • Plan A: encrypt via AES; convert to base64; store in a `VARCHAR` or `TEXT` column that is `CHARACTER SET ascii`. – Rick James Dec 10 '15 at 04:41
  • Plan B: encrypt via AES; convert to hex; `INSERT ... VALUES (... UNHEX(?)...)`. – Rick James Dec 10 '15 at 04:42

0 Answers0