python - Trouble with encode + encrypt + pad using same code for python2 and python3 -


disclaimer: understand following not suited give "security" in production environment. meant "a little bit better" using xor or rot13 on sensitive data stored on system.

i put following code allow me use aes encryption sensitive values. aes requires 16 byte chunks; need padding. , want save data in text files; added base64 encoding:

from __future__ import print_function crypto.cipher import aes import base64  crypto = aes.new('this key123', aes.mode_cbc, 'this iv456')  bs = 16 pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs) unpad = lambda s: s[0:-ord(s[-1])]   def scramble(data):     return base64.b64encode(crypto.encrypt(pad(data)))   def unscramble(data):     return unpad(crypto.decrypt(base64.b64decode(data)))  incoming = "abc" print("in: {}".format(incoming)) scrambled = scramble(incoming) print("scrambled: {}".format(scrambled)) andback= unscramble(scrambled) print("reversed : {}".format(andback)) 

for python2; prints:

in: abc

scrambled: asekqludiqlupw1lw09ulq==

reversed :

for python3; run

unpad = lambda s: s[0:-ord(s[-1])] typeerror: ord() expected string of length 1, int found 

two questions:

  • what wrong "reverse" path python2, why doesn't print "abc"?
  • i understand error message using python3; wondering: correct, canonical way solve problem in way works both python2 , python3?

one problem code using same cipher object both encryption , decryption. won't work, cipher objects stateful:pycrypto documentation

you can create object decrypting, in: crypto2 = aes.new('this key123', aes.mode_cbc, 'this iv456'), , use object decrypt.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -