question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

invalid key format error on generating private key

See original GitHub issue

In which file did you encounter the issue?

java-docs-samples/iot/api-client/mqtt_example/src/main/java/com/google/cloud/iot/examples/MqttExample.java

Describe the issue

The command I used to generate rsa private key and certificate is this:

openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out rsa_cert.pem -subj "/CN=unused"

In createJwtRsa method, the private key file which is working for the Python MQTT sample, giving the following error:

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
	at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)	
Caused by: java.security.InvalidKeyException: invalid key format
	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:330)
	at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
	at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)
	at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)
	at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)
	at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)
	... 3 more 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
testover99commented, Oct 11, 2017

When I use the following command to convert the pem into der format, it works. Is it the way it should work? As I have to use the same private key file on both the platforms, Java and Python.

openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key_file -nocrypt > pkcs8_key

0reactions
gguusscommented, Jun 18, 2018

@parameshwargouda did you register the device’s public certificate that pairs with the private key (in PKCS8 format) used to connect?

Read more comments on GitHub >

github_iconTop Results From Across the Web

invalid key format on generating RSA public key
1 Answer 1 ... You must have an error in the way you read the key back from the database. The following code...
Read more >
What can I do if I'm getting an RSA Private Key is invalid error ...
If you are getting an RSA Private Key is invalid error when you try adding your .key file under Server > HTTPS &...
Read more >
SSH Key Format Issues and Fixing the “invalid format” Error
In this tutorial, we talk about issues with the format of keys when establishing an SSH session. First, we generate a sample key...
Read more >
Invalid Private key file error - Power Automate - Microsoft Learn
Load your existing private key file in PuTTY Key Generator. Select Conversions > Export OpenSSH key (not the force new file format option)....
Read more >
"Invalid Private Key Pass Phrase and Private key combination ...
There could be multiple causes: 1) The wrong passphrase was used, double check. ... Then the key is in RFC-4716 format, and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found