title: Trouble with encode + encrypt + pad using same code for python2 and python3
body: <p>Disclaimer: I understand that the following is <strong>not</strong> suited to give &quot;security&quot; in a production environment. It is simply meant as &quot;a little bit better&quot; than using XOR or rot13 on sensitive data that is stored on my system.</p>
  <p>I put together the following code to allow me to use AES encryption for those sensitive values. AES requires 16 byte chunks; so I need padding. And I want to save that data in text files; so I added base64 encoding:</p>
  <pre><code>from __future__ import print_function
  from Crypto.Cipher import AES
  import base64
  
  crypto = AES.new('This is a key123', AES.MODE_CBC, 'This is an 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 = &quot;abc&quot;
  print(&quot;in: {}&quot;.format(incoming))
  scrambled = scramble(incoming)
  print(&quot;scrambled: {}&quot;.format(scrambled))
  andback= unscramble(scrambled)
  print(&quot;reversed : {}&quot;.format(andback))
  </code></pre>
  <p>For python2; that prints:</p>
  <blockquote>
  <p>in: abc</p>
  <p>scrambled: asEkqlUDiqlUpW1lw09UlQ==</p>
  <p>reversed :</p>
  </blockquote>
  <p>For python3; I run into</p>
  <pre><code>unpad = lambda s: s[0:-ord(s[-1])]
  TypeError: ord() expected string of length 1, but int found
  </code></pre>
  <p>Two questions:</p>
  <ul>
  <li>What is wrong with my &quot;reverse&quot; path with python2, why doesn't it print &quot;abc&quot;?</li>
  <li>I understand that error message using python3; but I am wondering: what is the correct, canonical way to solve this problem in a way that works for both python2 and python3?</li>
  </ul>