GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

New issue
Copy link
New issue
Copy link
Closed
Closed
invalid key format error on generating private key#884
Copy link
Assignees
gguuss

Description

@testover99
testover99
opened on Oct 11, 2017
Issue body actions

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

  1. testover99 commented on Oct 11, 2017

    @testover99
    testover99
    on Oct 11, 2017
    Author
    More actions

    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
  2. lesv
    assigned
    gguuss
    on Oct 11, 2017
  3. lesv
    added
    TRIAGED
    on Oct 11, 2017
  4. gguuss commented on Oct 11, 2017

    @gguuss
    gguuss
    on Oct 11, 2017
    Contributor
    More actions

    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.

  5. testover99 commented on Oct 12, 2017

    @testover99
    testover99
    on Oct 12, 2017
    Author
    More actions

    Thanks, @gguuss. Good to know this.

  6. testover99
    closed this as completedon Oct 12, 2017
  7. gguuss commented on Oct 16, 2017

    @gguuss
    gguuss
    on Oct 16, 2017
    Contributor
    More actions

    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.

  8. parameshwargouda commented on Jun 16, 2018

    @parameshwargouda
    parameshwargouda
    on Jun 16, 2018
    More actions

    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)
    ``

  9. gguuss commented on Jun 18, 2018

    @gguuss
    gguuss
    on Jun 18, 2018
    Contributor
    More actions

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

  10. minherz
    added 3 commits that reference this issue on Nov 9, 2022

    chore(main): release 0.123.3 (#884)

    801c1a1

    chore(main): release 0.123.3 (#884)

    c366c09

    chore(main): release 0.123.3 (#884)

    ea68c88
  11. Sita04
    added 2 commits that reference this issue on Feb 7, 2023

    deps: update dependency com.google.cloud:google-cloud-pubsub to v1.11…

    021810b

    chore(main): release 3.4.0 (#886)

    8212a40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @gguuss
    gguuss

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.