title: java.security.InvalidKeyException: invalid key format while generating public, private key from PEM file
body: <p>I have gone through many similar threads but no luck!!</p>
  
  <p>I want to generate public and private keys using a PEM file. Following is the code I am using for the same:</p>
  
  <pre><code>        String pemFileNme = "C:\\Users\\amitmm\\Desktop\\clean\\key.pem";
  
          File pubKeyFile = new File(pemFileNme);
          File privKeyFile = new File(pemFileNme);
  
          // read public key DER file
          DataInputStream dis = new DataInputStream(new 
          FileInputStream(pubKeyFile));
          byte[] pubKeyBytes = new byte[(int)pubKeyFile.length()];
          dis.readFully(pubKeyBytes);
          dis.close();
  
          // read private key DER file
          dis = new DataInputStream(new FileInputStream(privKeyFile));
          byte[] privKeyBytes = new byte[(int)privKeyFile.length()];
          dis.read(privKeyBytes);
          dis.close();
  
          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  
          // decode public key
          X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubKeyBytes);
          RSAPublicKey pubKey = (RSAPublicKey) 
          keyFactory.generatePublic(pubSpec);
  
          // decode private key
          PKCS8EncodedKeySpec privSpec = new 
          PKCS8EncodedKeySpec(privKeyBytes);
          RSAPrivateKey privKey = (RSAPrivateKey) 
          keyFactory.generatePrivate(privSpec);
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>Exception in thread "main" java.security.spec.InvalidKeySpecException: 
  java.security.InvalidKeyException: invalid key format
  at
  sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205)
  at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
  at main.java.me.txedo.security.Main2.f1(Main2.java:47)
  at main.java.me.txedo.security.Main2.main(Main2.java:20)
  Caused by: java.security.InvalidKeyException: invalid key format
  at sun.security.x509.X509Key.decode(X509Key.java:387)
  </code></pre>
  
  <p>PEM File Content:</p>
  
  <blockquote>
    <p>-----BEGIN RSA PRIVATE KEY-----
        MIIEowIBAAKCAQEAwnEEodFEf86+Ae+wYyI//u1kekIWnA3RfzbAwWD77uG7D9Ci
        9vVNbPO4XT2hKL03/q7d7KTgrA1sjBltfaOzVfA56x1S/0cYVk4xI440dpLo0F+m
        RIqRw5fh8IuUlUIr3I4A7ESkDQQsZbDpdgCiNbrlADqLotcZyB4rU4uURW8QUI/W
        eqsD6TOQs4bI+3o3xAKkky2kXujSaaa3tDxgUPTmSQ0Buk7Hx/IVzwyV7qjWiR4U
        C46rHpnWxfF0DWuJUOYgJmBQ8xFOQwt4Ec/u+0m8top8cqQF+gpBn9iLXpbtahA3
        pqyvLuNXRH9yn8mlEneBrjjl6U0H3W/AV7/dGwIBAwKCAQEAgaCtwTYtqonUAUp1
        l2wqqfOYUYFkaAk2VM8rK5X9SevSCosXT04znffQPikWGyjP/x8+ncNAcrOdsrue
        U8J3jqAmnL43VNoQOYl2F7Qi+bdF4D/ELbG2gmVBSwe4Y4FykwlV8thtXgLIQ8tG
        TqsWznyYqtGybI9mhWlyN7Ji2POMDZP5Lwx7M01pMezwpnsZSmPVL9TgVrtWv4xt
        C0vPyuy9THlFWtkOdHItNK+vOTcpuHn29rFUJI/D3R+SQjcdqj3aaqljOtdeBxgd
        yDl2/Z4rUyetgzcZMfNTt/NRT0hOJ6R6/2S7gFCTtxMHBh3vVCH+pLLnQyJvcPQu
        AsORSwKBgQDhOPr1x/8BioqaasoXvO9NsGktCgPDjbC4d3jR8n6lCa42X/eIahaD
        xi1VGWyQhdO7aMXiDmzOtox7xHcMRh+a5ySIs9gTsHkMB2hqwIUNg25INRkQ3Vr3
        eWnoTBGsfJqC1TEME3ocKwmyz57ZAe4yyR/ZRdDX5DUt9qCCFeA8uQKBgQDdAzbq
        7BlJkbTYfdlIRNJEJAO3wWqQTx8X0ttCMMwDluOT9l+RR/KuUxl85ph+kwJci6E/
        ixfeMTW1NcsMY/lB6mTP0oooalU1MP7gpPSu+24zhLXnUHZotbNbv9nk6w/1WWhz
        FBt5w2DG4kQPFK6LSySqcVuzIGQyvWD5PbpGcwKBgQCWJfyj2qoBBwcRnIa6ffTe
        dZtzXAKCXnXQT6XhTFRuBnQkP/pa8WRX2XOOEPMLA+J88IPsCZ3fJF2n2E9dhBUR
        722wd+VidaYIBPBHKwNeV57azhC16OdPpkaa3WEdqGcB43YIDPwSx1vMimnmAUl3
        ML/mLos6mCNz+cBWuUAoewKBgQCTV3nx8ruGYSM6/pDa2IwtbVfP1kcK32oP4eeB
        dd1Xue0NTupg2qHJjLuombr/DKw9smt/sg/pdiPOI9yy7VDWnEM1NwbFnDjOIKnr
        GKMfUkl3rc6aNaRFzneSf+aYnLVOO5r3Yrz715XZ7C1fYx8Hh23G9j0iFZgh05X7
        fnwu9wKBgHyC0X26KZQ0ukan5jDSiz4dapUp2d3F+vnRzZa2AOsmo995gsXLdfsJ
        n0o4Z3LsQJUDRI3tQ4dXe/5jS4oFrOdxALOAw6YmvEv/3oHwsCYPDhqLNfIJ9I6m
        Dt3yG61pUJiCArhPaYG17NQoCxF6Xi6GUajRsECbr8DdyGMAu5eE
        -----END RSA PRIVATE KEY-----</p>
  </blockquote>
  
  <p>I have tried removing file header and footer manually. I tried code from bouncycastle, no luck, same error.</p>
  
  <p>Python code which works with this file:</p>
  
  <pre><code>def t2e_enc(plaintext, pk_pem_file = './2017-12-04T062008Z.pem'):
  ''' 
  Function for encryption of Track2 credit card data.
  This function uses private key to derivate public part used for encryption
  
  '''
  with open(pk_pem_file, 'rb') as pk:
      private_key = serialization.load_pem_private_key(pk.read(), 
      password=None, backend=default_backend())
  
  public_key = serialization.load_pem_public_key(
      private_key.public_key().public_bytes(
          serialization.Encoding.PEM, 
          serialization.PublicFormat.SubjectPublicKeyInfo),
          backend=default_backend()
      )
  
  ciphertext = public_key.encrypt(
      plaintext,
      padding.OAEP(
          mgf=padding.MGF1(algorithm=hashes.SHA1()),
          algorithm=hashes.SHA1(),
          label=None
      )
  )
  b64ciphertext=base64.b64encode(ciphertext)
  return b64ciphertext
  </code></pre>
  
  <p>I am doing this for the first time, so bear with me if there is any silly mistake.</p>