无法使用 RS256 验证 JWT - 算法无效
Cannot verify JWT with RS256 - invalid algorithm
我正在尝试将我的 JWT 身份验证从密码短语移动到 RS256
这里是示例代码:
import fs from 'fs'
import jwt from 'jsonwebtoken'
const private_key = fs.readFileSync('private.key')
const public_key = fs.readFileSync('public.pem')
const token = jwt.sign({
some: 'payload'
}, private_key, { algorithm: 'RS256' })
const verified = jwt.verify(token, public_key)
这是我的 public 密钥的格式:
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAyLVWMJHRJ0VRPiyhjvPJTwpgNkMd4jjvUxEfy54COanbrL+GeOVd
NB2YooopYzgJJZyVhHSD+GwyH7WTvsItwNkfMiZKmjUIEA9dIfcbmHNXtfXraiSh
aTSO7NIREGzGQjBfzRGNeWpR83P+ffE5H0gfi9FFJlvV8QSLSSXJNMXVFAga//fb
uT/CYLg29bzEBtNSdgTHNrbwSvYzGFDOHNChDelX6fp34JMhtb6UNh6cIqhURclK
reQHz2oDcBaXnCAHjvC8p/IoG8xV2Ws5gK6bVW7h0EP5XoaUVHVpjPv5TDFz+veR
/lkKdowQU74E5oyei1WzDXSTusCR9gKIcQIDAQAB
-----END RSA PUBLIC KEY-----
我收到以下错误:
JsonWebTokenError: invalid algorithm
当我尝试验证 jwt.io 令牌是否已成功签名时。
试试这个:
var fs = require('fs')
var jwt = require('jsonwebtoken')
const private_key = fs.readFileSync('private.key', 'utf8')
const public_key = fs.readFileSync('public.pem', 'utf8')
const token = jwt.sign({
some: 'payload'
}, private_key, { algorithm: 'RS256' })
const verified = jwt.verify(token, public_key, { algorithms: 'RS256'})
私钥:(private.key)
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDCDzF83j+imr1t2fNtfT4Fusm3LJAzFK49xK3bepARl2gcKKNm
YJI6svZ8mznkoDp1uCuwyBKVIkRfrGAd1FuuVrzMi0kairWGz71FHbcubKy/6AOG
caONmk0K1DjS4dIzJLmIeUpv2jrtIctGDt2tXg4OQi1aM59yS4efZlbNxQIDAQAB
AoGARlZPb5z2n69KfaiDzOmaM2Vye4wRcBJM3WKOSYDw+w9P0vT/1LmoBtKa+Ksi
nrVcuxJX86lw4tEayzqlU+qGl9Z5uOI+vMjh6e4hpEQN2KVrVnpEUrzVX8yZN22A
KWWZsWucU7djb8wLpezYNYd5bItUuOXFRnmrjuPQhqLXbPkCQQDmiR+zwmcOTir0
9auT88fTeLpr7XT+YyrBOHjgwUTd8HO3C2FuL/dDv3pP9LrsUgynMTMef3nF2qh0
40bep4qPAkEA136gUbisosNHa7GRKCSPUobmEBc4pRr2CtaUbAvGFJMbV+RCzu7a
n0hHbPZwlWBTN/WIfrj5dvNRa+lbk3PcawJAem2P/HLdL+erQHPHLsdj85ZFylNM
slwPtJU8/H8nB4ZOrYLJty6Z7cyeNCAPtLjOJ2wlbajdDonUtF6OoGfxWQJBALGp
TOVzGqkp1CUehO0SjzLb0qrraiD8xGKVHFKjtk/aJE3m+4l9dLKjNXfJCXKtso5N
GJZZTBpcagFMp9o+SDcCQQCKwJ9tVu/4X5SFgbVSr06u1tPkCccYFAiqbpEH4fPx
zt18ubZgiViKiYKT6gHZPKI8bhkmvG/Fg/zJ9FBYjl+9
-----END RSA PRIVATE KEY-----
public键:(public.pem)
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCDzF83j+imr1t2fNtfT4Fusm3
LJAzFK49xK3bepARl2gcKKNmYJI6svZ8mznkoDp1uCuwyBKVIkRfrGAd1FuuVrzM
i0kairWGz71FHbcubKy/6AOGcaONmk0K1DjS4dIzJLmIeUpv2jrtIctGDt2tXg4O
Qi1aM59yS4efZlbNxQIDAQAB
-----END PUBLIC KEY-----
我正在尝试将我的 JWT 身份验证从密码短语移动到 RS256
这里是示例代码:
import fs from 'fs'
import jwt from 'jsonwebtoken'
const private_key = fs.readFileSync('private.key')
const public_key = fs.readFileSync('public.pem')
const token = jwt.sign({
some: 'payload'
}, private_key, { algorithm: 'RS256' })
const verified = jwt.verify(token, public_key)
这是我的 public 密钥的格式:
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAyLVWMJHRJ0VRPiyhjvPJTwpgNkMd4jjvUxEfy54COanbrL+GeOVd
NB2YooopYzgJJZyVhHSD+GwyH7WTvsItwNkfMiZKmjUIEA9dIfcbmHNXtfXraiSh
aTSO7NIREGzGQjBfzRGNeWpR83P+ffE5H0gfi9FFJlvV8QSLSSXJNMXVFAga//fb
uT/CYLg29bzEBtNSdgTHNrbwSvYzGFDOHNChDelX6fp34JMhtb6UNh6cIqhURclK
reQHz2oDcBaXnCAHjvC8p/IoG8xV2Ws5gK6bVW7h0EP5XoaUVHVpjPv5TDFz+veR
/lkKdowQU74E5oyei1WzDXSTusCR9gKIcQIDAQAB
-----END RSA PUBLIC KEY-----
我收到以下错误:
JsonWebTokenError: invalid algorithm
当我尝试验证 jwt.io 令牌是否已成功签名时。
试试这个:
var fs = require('fs')
var jwt = require('jsonwebtoken')
const private_key = fs.readFileSync('private.key', 'utf8')
const public_key = fs.readFileSync('public.pem', 'utf8')
const token = jwt.sign({
some: 'payload'
}, private_key, { algorithm: 'RS256' })
const verified = jwt.verify(token, public_key, { algorithms: 'RS256'})
私钥:(private.key)
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDCDzF83j+imr1t2fNtfT4Fusm3LJAzFK49xK3bepARl2gcKKNm
YJI6svZ8mznkoDp1uCuwyBKVIkRfrGAd1FuuVrzMi0kairWGz71FHbcubKy/6AOG
caONmk0K1DjS4dIzJLmIeUpv2jrtIctGDt2tXg4OQi1aM59yS4efZlbNxQIDAQAB
AoGARlZPb5z2n69KfaiDzOmaM2Vye4wRcBJM3WKOSYDw+w9P0vT/1LmoBtKa+Ksi
nrVcuxJX86lw4tEayzqlU+qGl9Z5uOI+vMjh6e4hpEQN2KVrVnpEUrzVX8yZN22A
KWWZsWucU7djb8wLpezYNYd5bItUuOXFRnmrjuPQhqLXbPkCQQDmiR+zwmcOTir0
9auT88fTeL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