简体   繁体   English

为什么我在解码 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

I have a key named social-public.key , which I am using to decode JWTs, but the problem is I am having an error as below我有一个名为social-public.key的密钥,我用它来解码 JWT,但问题是我遇到了如下错误

Error occurred while decoding access token Error: error:0909006C:PEM routines:get_name:no start line
    at Verify.verify (internal/crypto/sig.js:157:24)
    at Object.verify (D:\SocialAnalysisDashboard\social-dashboard-user-service\node_modules\jwa\index.js:164:21)
    at Object.jwsVerify [as verify] (D:\SocialAnalysisDashboard\social-dashboard-user-service\node_modules\jws\lib\verify-stream.js:54:15)
    at D:\SocialAnalysisDashboard\social-dashboard-user-service\node_modules\jsonwebtoken\verify.js:127:19
    at getSecret (D:\SocialAnalysisDashboard\social-dashboard-user-service\node_modules\jsonwebtoken\verify.js:90:14)
    at Object.module.exports [as verify] (D:\SocialAnalysisDashboard\social-dashboard-user-service\node_modules\jsonwebtoken\verify.js:94:10)
    at D:\SocialAnalysisDashboard\social-dashboard-user-service\express\Middlewares\auth.js:24:46
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  library: 'PEM routines',
  function: 'get_name',
  reason: 'no start line',
  code: 'ERR_OSSL_PEM_NO_START_LINE'
}

Here is code the code I am using to decode JWTs,这是我用来解码 JWT 的代码,

const decodedToken = jwt.verify(token, key, {algorithms: ['RS256']});

Mostly solutions I have found are related to .pem files.我发现的大多数解决方案都与.pem文件有关。

I had a similar problem and I fixed it with help of this issue post.我有一个类似的问题,我在这个问题帖子的帮助下修复了它。 You only need one key to sign and verify if you use HS256 .如果您使用HS256您只需要一把钥匙来签名和验证。 The RSA version requires a public and private key (Public for verification and private for signing) RSA版本需要公钥和私钥(公钥用于验证,私钥用于签名)

Follow the following steps:请按照以下步骤操作:

  1. Have a valid public and it's associated private key.有一个有效的公钥及其关联的私钥。 ( csfieldguide , travistidwell ) ( csfieldguide , travistidwell )
  2. Include the '---private/public key start---' and '---public/private key end---' part包括 '---private/public key start---' 和 '---public/private key end---' 部分
  3. I saved it in base64 and can be giving directly in the jsonwebtoken module from npm我将它保存在base64 ,可以直接在npmjsonwebtoken模块中提供
  4. Make sure the new line byte ( \\n ) is giving with the base64 string确保新行字节( \\n )与base64字符串一起给出

example:例子:

let secret = [
        '-----BEGIN PRIVATE KEY-----',
        'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAkcd7iupXSHhgIRat',
        'b2gnEiyC3AIf7GCrISTtgM5Lb8kccGjunU8sIqwwd3BV6qD+pExeyvMyU085RHRX',
        'ud1cyQIDAQABAkAzmni6GPAiwDHPJLbqK+VAwq7j8ICabTHGvsqwANalT/O4V75m',
        'e2ExeqV05+jlzVOGrQ953n8Mx1u0uRgPlfoBAiEAyO3qytGKRRzlqBuGwPFPde4a',
        '66ZW4AmRcBwwuKp1zgkCIQC5u/2j/JFzM4GTbpoC0a2u78+tqYQW7Y/Usu6AAubI',
        'wQIhAMKbhMQJ7UUBNwH6HyryzcZn5pUEl7IIMmAGPb4uA0mZAiAbJPhawQzY00w6',
        'qc1kYBSMHowxiza8yxdcNJJarxHfgQIgcw2oEtn8GbvNMOsFg0Q9TPMdQ+uhxhWK',
        'xhVgWkIkTVU=',
        '-----END PRIVATE KEY-----',
    ].join('\n');

Same goes for the public key when verifying.验证时公钥也是如此。

// Create a signed JWT token
const token = jwt.sign(payload, privKey, {algorithm: 'RS256', ...otherSignOptions}); 

// returns undefined if token could not be verified
jwt.verify(token, pubKey);

I'd recommend of course reading the key from a file instead of hardcoding it.我当然建议从文件中读取密钥,而不是对其进行硬编码。 (preferably in a safe location) (最好在安全的地方)

暂无
暂无

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

相关问题 Firebase/admin 使用函数错误:错误:0909006C:PEM 例程:get_name:没有起始行 - Firebase/admin using functions Error: error:0909006C:PEM routines:get_name:no start line 环境变量不会用 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 在 Blitz.js api 处理程序中运行时,Google Cloud Storage 调用失败并显示“error:0909006C:PEM routines:get_name:no start line” - Google Cloud Storage call fails with "error:0909006C:PEM routines:get_name:no start line" when run in Blitz.js api handler 使用 NodeJS 将数据编码为 JWT 令牌获取错误 PEM 例程:PEM_read_bio:没有起始行 - Encoding the data to JWT token with NodeJS getting error PEM routines:PEM_read_bio:no start line 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 为什么我在 JSON 中的位置 0 处收到 Unexpected token &lt; 的错误 - Why am I getting an error of Unexpected token < in JSON at position 0 为什么在脚本的第一行出现JS错误语法错误:无效或意外的令牌? - Why am I getting the JS error in line one of my script Syntax error: invalid or unexpected token? 为什么会出现错误:第26行出现意外的令牌“ else”? - Why do I get an Error: unexpected token 'else' on line 26? 在 msal-node js 中使用带有证书的 SPN 获取访问令牌的 azure 活动目录面临错误“ERR_OSSL_PEM_NO_START_LINE” - azure active directory get access token using SPN with certificate in msal-node js facing error "ERR_OSSL_PEM_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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM