简体   繁体   中英

nodejs jsonwebtoken with PEM file: error “PEM_read_bio:no start line”

I know there are a bunch of similar questions here but after hours of reading I cannot find a solution to mine. So:

I am trying to use jwk-to-pem for signing a JSON web token using jsonwebtoken . I have previously achieved this but now the same code does not work. After calling jwt.sign with the generated pem I get error:0906D06C:PEM routines:PEM_read_bio:no start line . I know that common reasons would include wrong linebreaks like \\r or generally wrong format. I actually even dug through the core nodejs code and found the line throwing this error.

Countless adjustments and attempts brought no solution. Specifically:

  • I made sure that all linebreaks are \\n
  • I tried different strings for the first and last line, eg -----BEGIN PUBLIC KEY----- or -----BEGIN RSA PUBLIC KEY-----
  • I made sure that the last line contains a \\n as last character
  • I tried to write the pem string to disk and re-reading it, using node's fs .
  • I compared the pem file to several other files with several editors eg using vi -b or regex.
  • I tried handing the raw file buffer with the pem content to jwt.sign as well as pem.toString('utf8') as well as every other string encouding supported by node
  • I tried to generate a random pem file using linux tools and give that to jwt.sign . Specifically: ssh-keygen -t rsa -b 2048 -f jwtRS256.key
    openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.pem

What am I missing here? What possible causes I did not think of can this have?

I found a very simple explanation: I was by mistake using the public instead of the private key. The error thrown by openssl/crypto is very misleading.

Using a private pem/key the signing works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM