title: Padding errors using OpenSSL for encrypt/decrypt with Keys extracted from Digital Cert
body: <p>I am using some command line Open SSL commands to encrypt and decrypt data using Public and Private keys extracted from a Digital Cert.   When I try to decrypt I get PKCS padding errors.  Can someone tell me where I'm going wrong? </p>
  
  <p>These are the command I've been using: </p>
  
  <p>a) Extract Public key:  openssl x509 -pubkey -noout -in xxxxx.cer  > xxxxxpublickey.pem</p>
  
  <p>b) Extract Private Key:openssl pkcs12 -in xxxxxx.pfx -nocerts -out xxxxxprivatekey.pem -nodes</p>
  
  <p>c) Encypt a key (.bin file): openssl enc -aes-256-cbc -in kenkey.bin -out kenkey_Key -pass file:xxxxxpublickey.pem</p>
  
  <p>d) Decrypt key produced in c) openssl rsautl -decrypt -hexdump -in kenkey_key -inkey xxxxxprivatekey.key -out aeskey.txt </p>
  
  <p>This produces errors like this: </p>
  
  <blockquote>
    <p>RSA operation error 3248:error:0407109F:rsa
    routines:RSA_padding_check_PKCS1_type_2:pkcs decoding
    error:.\crypto\rsa\rsa_pk1.c:273: 3248:error:04065072:rsa
    routines:RSA_EAY_PRIVATE_DECRYPT:padding check
    failed:.\crypto\rsa\rsa_eay.c:602:</p>
  </blockquote>