title: How does openssl command handle PKCS#7 padding added with python
body: <p>I encrypt a (tar) file use python and padding using PKCS#7. This encrypted file is then to a client (a router with OpenWRT). This router uses the (linux) shell with the <code>openssl</code> command to decrypt it. This last step however fails with the following info:</p>
  
  <pre><code>bad decrypt
  
  2011837512:error:06065064:lib(6):func(101):reason(100):NA:0:
  
  tar: short read 
  </code></pre>
  
  <p>Perhaps the problem lies in the padding, so I want to know what arguments to use for <code>openssl</code> to avoid this issue. Are there any other commands than <code>-nopad</code> related to padding? </p>
  
  <p>The following Python code is used to encrypt the tar file: </p>
  
  <pre><code># Encrypt file
  def aes_encrypt_file(in_filename, out_filename, key, iv):
  block_size = AES.block_size
  pad = lambda s: s + (block_size - len(s) % block_size) \
                      * chr(block_size - len(s) % block_size)
  cipher = aes_build_cipher(key, iv)
  with open(in_filename, 'rb') as infile:
      with open(out_filename, 'wb') as outfile:
          while True:
              buf = infile.read(1024)
              if len(buf) == 0:
                  break
              elif len(buf) % block_size != 0:
                  buf = pad(buf)
              outfile.write(cipher.encrypt(buf))
  </code></pre>
  
  <p>The following shell command is used to decrypt the tar file on the router:</p>
  
  <pre><code>openssl aes-256-cbc -d -nosalt -K $sum256 -iv $iv -in ${PACKAGE} | tar xzf -
  </code></pre>