GeezerVison is showing github.com — invalid key format error on generating private key · Issue #884 · GoogleCloudPlatform/java-docs-samples
invalid key format error on generating private key #884
Description
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
Activity
testover99 commented on 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
Reacted by wbmrcb and Paul Feuer
gguuss commented on Oct 11, 2017
Is it the way it should work?
You are correct. For Java, the certificate must be in .PEM format for the MQTT sample to be able to produce the JWT. I've encountered the same issue; I can update the README to clarify this and correct the examples to reference files with the extension.
Currently, the only indicator that you need the PEM file is that the certificate generation script has this additional step specific to the Java sample.
testover99 commented on Oct 12, 2017
Thanks, @gguuss. Good to know this.
gguuss commented on Oct 16, 2017
FYI @testover99 I'm looking at this again and the existing README references the correct keyfiles, which should in fact be in PKCS #8 format.
parameshwargouda commented on Jun 16, 2018
I followed the steps mentioned but i am getting : An error occurred: Not authorized to connect
mvn exec:java -Dexec.mainClass="com.example.cloud.iot.examples.MqttExample" -Dexec.args="-project_id=fueblabs-206511
-registry_id=my-registry2
-cloud_region=us-central1
-device_id=java-device-8
-private_key_file=./rsa_private_pkcs8
-algorithm=RS256"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] cloudiot-manager-demo
[INFO] doc-samples
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cloudiot-manager-demo 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ cloudiot-manager-demo ---
An error occurred: Not authorized to connect
[WARNING]
Not authorized to connect (5)
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)
at org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:145)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
``
gguuss commented on Jun 18, 2018
@parameshwargouda did you register the device's public certificate that pairs with the private key (in PKCS8 format) used to connect?
Metadata
Metadata
Development
No branches or pull requestsIssue actions