title: PyCriptodome AES ValueError("PKCS#7 padding is incorrect.")
body: <pre><code>from Crypto.Cipher import AES
  
  import hashlib
  
  def encryptString(plaintext, key):
      # Encryption#
      plaintext = Padding.pad(plaintext, AES.block_size);
  
      iv = get_random_bytes(AES.block_size)
      print('How many:' , sys.getsizeof(key));
      cipher = AES.new(key, AES.MODE_CBC, iv)
  
      ciphertext = cipher.encrypt(plaintext);
      return (iv + ciphertext).hex();
  
  def decryptString(ciphertextHex, key):
      ciphertext = binascii.unhexlify(ciphertextHex)
      iv = ciphertext[:AES.block_size]
      ciphertext = ciphertext[AES.block_size:]
  
      cipher = AES.new(key, AES.MODE_CBC, iv)
  
      plaintext = cipher.decrypt(ciphertext)
      plaintext = Padding.unpad(plaintext, AES.block_size)
  
      return plaintext.decode('utf-8');
  </code></pre>
  
  <p>I created an AES encryption/decryption wrapper. It works with an example key like:</p>
  
  <blockquote>
    <p>key = b'0123456789abcdef0123456789abcdef'</p>
  </blockquote>
  
  <p>But if I am generating a random AES key like this (from a string)</p>
  
  <pre><code>def convertKey(self,key):
      return hashlib.sha256(key.encode()).digest();
  </code></pre>
  
  <p>Then it returns this error:</p>
  
  <blockquote>
    <p>File "C:\Python36\lib\site-packages\Crypto\Util\Padding.py", line 93,
    in unpad
        raise ValueError("PKCS#7 padding is incorrect.") ValueError: PKCS#7 padding is incorrect.</p>
  </blockquote>
  
  <p>Any ideas why it returns this, and works with a sample key?</p>