You get this error because you are reading a PKCS#8 key file, but your file has a PKCS#1 format (PKCS#1 has the BEGIN RSA PRIVATE KEY
header while PKCS#8 has the BEGIN PRIVATE KEY
header).
In order to read both PKCS#1 and PKCS#8 PEM files, I use the source code of Apache JMeter's org.apache.jmeter.protocol.oauth.sampler.PrivateKeyReader
:
PrivateKey pk = (new PrivateKeyReader("/path/to/myfile.der")).getPrivateKey();
Alternately you can conver your mykey.pem to DER format and use your existing code. Following is the openssl command to do that
//generate PKCS#1 key
openssl genrsa -out mykey.pem 1024
//generate public key
openssl rsa -in mykey.pem -pubout > mykey.pub
//Convert to PKCS#8
openssl pkcs8 -topk8 -inform PEM -outform DER -in mykey.pem -out mykey.der -nocrypt