I use the PyCrypto module to generate a cipher text for a message.
>>> a=AES.new("1234567890123456")
>>> m='aaaabbbbccccdddd'
>>> a.encrypt(m)
'H\xe7\n@\xe0\x13\xe0M\xc32\xce\x16@\xb2B\xd0'
I would like to have this output like the one by hashlib
>>> from hashlib import sha1
>>> sha1(m).hexdigest()
'68b69b51da162fcf8eee65641ee867f02cfc9c59'
That is, I would need a clean string instead of the string with the hex markers like \x
and stuff.
Is there any way in PyCrypto to achieve this?
If yes,How can encryption and decryption be performed?
If no, how can I convert the string to the string I need?
Use binascii.hexlify
(or binascii.b2a_hex
):
>>> from Crypto.Cipher import AES
>>> a = AES.new("1234567890123456")
>>> m = 'aaaabbbbccccdddd'
>>> a.encrypt(m)
'H\xe7\n@\xe0\x13\xe0M\xc32\xce\x16@\xb2B\xd0'
>>> import binascii
>>> binascii.hexlify(a.encrypt(m))
'48e70a40e013e04dc332ce1640b242d0'
If the encrypt function returns a Python string, then you can do:
a.encrypt(m).encode('hex')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With