简体   繁体   English

带有PEM文件的nodejs jsonwebtoken:错误“ PEM_read_bio:无起始行”

[英]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 . 我试图用JWK到PEM签署使用JSON网络令牌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 . 用生成的pem调用jwt.sign ,我得到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. 我知道常见的原因包括\\r等换行符或格式错误。 I actually even dug through the core nodejs code and found the line throwing this error. 实际上,我什至挖了核心的nodejs代码,发现抛出此错误的行

Countless adjustments and attempts brought no solution. 无数的调整和尝试都没有解决方案。 Specifically: 特别:

  • I made sure that all linebreaks are \\n 我确保所有换行符都是\\n
  • I tried different strings for the first and last line, eg -----BEGIN PUBLIC KEY----- or -----BEGIN RSA PUBLIC KEY----- 我为第一行和最后一行尝试了不同的字符串,例如-----BEGIN PUBLIC KEY----------BEGIN RSA PUBLIC KEY-----
  • I made sure that the last line contains a \\n as last character 我确保最后一行包含\\n作为最后一个字符
  • I tried to write the pem string to disk and re-reading it, using node's fs . 我尝试使用节点的fspem字符串写入磁盘并重新读取。
  • I compared the pem file to several other files with several editors eg using vi -b or regex. 我通过几个编辑器(例如,使用vi -b或regex)将pem文件与其他几个文件进行了比较。
  • 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 我尝试将带有pem内容的原始文件缓冲区传递给jwt.sign以及pem.toString('utf8')以及节点支持的所有其他字符串
  • I tried to generate a random pem file using linux tools and give that to jwt.sign . 我试图使用linux工具生成一个随机的pem文件,并将其提供给jwt.sign Specifically: ssh-keygen -t rsa -b 2048 -f jwtRS256.key 具体来说: 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. openssl / crypto引发的错误非常容易引起误解。

Using a private pem/key the signing works as expected. 使用私有的pem /密钥,签名可以按预期进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 NodeJS 将数据编码为 JWT 令牌获取错误 PEM 例程:PEM_read_bio:没有起始行 - Encoding the data to JWT token with NodeJS getting error PEM routines:PEM_read_bio:no start line 带有Google Vision API的AWS Lambda抛出PEM_read_bio:no起始行或Errno :: ENAMETOOLONG - AWS Lambda w/ Google Vision API throwing PEM_read_bio:no start line or Errno::ENAMETOOLONG npm 错误! 对 https://registry.npmjs.org/node-modules 的请求失败,原因:错误:0906D06C:PEM 例程:PEM_read_bio:没有起始行 - npm ERR! request to https://registry.npmjs.org/node-modules failed, reason: error:0906D06C:PEM routines:PEM_read_bio:no start line Node.js-“npm install express”错误:0906D06C:PEM 例程:PEM_read_bio npm - Node.js- "npm install express" error:0906D06C :PEM routines : PEM_read_bio npm 如何读取nodejs中的pem文件? - How to read the pem file in nodejs? Firebase/admin 使用函数错误:错误:0909006C:PEM 例程:get_name:没有起始行 - Firebase/admin using functions Error: error:0909006C:PEM routines:get_name:no start line 为什么我在解码 JSON Web 令牌错误时出现这个错误:错误:0909006C:PEM 例程:get_name:没有起始行 - Why I am this Error in decoding JSON Web Token Error: error:0909006C:PEM routines:get_name:no start line 在云功能中引用PEM文件 - Reference a pem file in a cloud function 环境变量不会用 javascript 中的新行替换 '\n' 并抛出错误:0909006C:PEMroutines:get_name:no start line - Environment variables doesn't replace '\n' with new line in javascript and throw error:0909006C:PEM routines:get_name:no start line 使用 nodeJS 解析椭圆曲线 PEM 证书 - Parse Elliptic Curve PEM certificate with nodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM