简体   繁体   中英

How to convert hex string again into cipher text for decryption

I am using pycrypto module for generating a cipher text. The problem is the generated cipher text cannot be saved somewhere since it is not human readable.

 >>> a=AES.new("1234567890123456")
    >>> m='aaaabbbbccccdddd'
    >>> a.encrypt(m)
    'H\xe7\n@\xe0\x13\xe0M\xc32\xce\x16@\xb2B\xd0'

I want to save this encrypted message in my database, and right now I can't do it since the its not human readable. I can save the data by converting into hexcode:

a.encrypto(m).encode('hex')

and generate the hex code and save it my database. The problem is how will I convert this encrypted hex code back to into same form for decryption? Can anyone help to me here?

My user will access the encrypted message from the database, decrypt it and then check the integrity of the message! Could anyone help me convert hex code back into cipher text?

Do reverse - decode using hex :

>>> encrypted = 'H\xe7\n@\xe0\x13\xe0M\xc32\xce\x16@\xb2B\xd0'
>>> encoded = encrypted.encode('hex')
>>> decoded = encoded.decode('hex')
>>> encrypted == decoded
True

Issue resolve! :)

You can use base64.b64encode(cipher_text) to save your data into database, and then base64.b64decode(instance.encrypted_message) to retrieve data. The solution is working for me.

You also need to import base64

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM