title: AES encrypted File transfer over TCP socket; padding issue
body: <p>I'm trying to do a file transfer using encrypted TCP socket with AES 256.</p>
  
  <ul>
  <li>If I transfer a file without encryption, it works fine.</li>
  <li>If I send small commands (such as 'ipconfig') to the client or server, the encryption works fine.</li>
  <li><p>No matter the file size is, I keep receiving the following error message:</p>
  
  <pre><code>DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
    File "/usr/lib/python2.6/base64.py", line 76, in b64decode
      raise TypeError(msg)
  TypeError: Incorrect padding
  </code></pre></li>
  </ul>
  
  <p>My function for encoding and decoding is the following (the <code>hat</code> variable is the message):</p>
  
  <pre><code>def AESENC(hat,typ):
  
      BLOCK_SIZE = 32
      PADDING = '{'
      pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
      EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
      DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  
      secret = '01234567890123456789012345678912'
      IV = 'wir&amp;/&gt;H54mgd9a";'
  
      cipher = AES.new(secret,AES.MODE_CFB,IV)
  
      if typ == 0:
          encoded = EncodeAES(cipher, hat)
          return encoded
      else:
          decoded = DecodeAES(cipher, hat)
          return decoded
  </code></pre>
  
  <p><strong>Client side</strong></p>
  
  <pre><code>if os.path.exists(df):
      print ' found the file '
      f = open(df, 'rb')
      packet = f.read(1024)
      while packet != '':
          s.send(AESENC(packet,0)) 
          s.send(   AESENC('123XXX',0)  ) 
      s.send('123XXX')
      f.close()
  </code></pre>
  
  <p><strong>Server side</strong></p>
  
  <pre><code>f = open('/root/Desktop/Transfer.mp3','wb')
  while True:
      bits =  AESENC ( conn.recv(1024) , 1 )
      while (bits):
          f.write(bits)
          bits =  AESENC ( conn.recv(1024) , 1 )
          if bits.endswith('123XXX'):
              print '[+] Transfer completed '
              break
  
      f.close()
      break
  return
  </code></pre>
  
  <p>Does anybody know how to fix this?</p>