RS256 消息对于 RSA 公钥大小来说太长 - 签名 JWT 时出错

问题描述 投票:0回答:3

我正在使用 https://github.com/dgrijalva/jwt-go 使用 256 位 PEM 私钥构建 JWT。我正在使用

SigningMethodRS256
签署 JWT:

signBytes, _ := ioutil.ReadFile(privKeyPath)
signKey, err := jwt.ParseRSAPrivateKeyFromPEM(signBytes)
token := jwt.NewWithClaims(jwt.SigningMethodRS256, middleware.CognitoAccessTokenClaim{
    CustomArray:  []string{"testString"},
    StandardClaims: jwt.StandardClaims{
    ExpiresAt: 1500,
    },
})
jwtString, err := token.SignedString(signKey)

在最后一行,我在签署 jwt 时收到错误:

crypto/rsa: message too long for RSA public key size
。有谁知道是什么原因造成的? pem 文件的大小似乎是正确的。

go jwt rsa pem jwt-go
3个回答
4
投票

您需要将消息拆分成块

func EncryptOAEP(hash hash.Hash, random io.Reader, public *rsa.PublicKey, msg []byte, label []byte) ([]byte, error) {
    msgLen := len(msg)
    step := public.Size() - 2*hash.Size() - 2
    var encryptedBytes []byte

    for start := 0; start < msgLen; start += step {
        finish := start + step
        if finish > msgLen {
            finish = msgLen
        }

        encryptedBlockBytes, err := rsa.EncryptOAEP(hash, random, public, msg[start:finish], label)
        if err != nil {
            return nil, err
        }

        encryptedBytes = append(encryptedBytes, encryptedBlockBytes...)
    }

    return encryptedBytes, nil
}

func DecryptOAEP(hash hash.Hash, random io.Reader, private *rsa.PrivateKey, msg []byte, label []byte) ([]byte, error) {
    msgLen := len(msg)
    step := private.PublicKey.Size()
    var decryptedBytes []byte

    for start := 0; start < msgLen; start += step {
        finish := start + step
        if finish > msgLen {
            finish = msgLen
        }

        decryptedBlockBytes, err := rsa.DecryptOAEP(hash, random, private, msg[start:finish], label)
        if err != nil {
            return nil, err
        }

        decryptedBytes = append(decryptedBytes, decryptedBlockBytes...)
    }

    return decryptedBytes, nil
}

0
投票

可能您生成私钥的方式不正确。我通过参考here

解决了同样的问题

生成密钥的步骤

ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub

使用 jwt-go 的步骤

package main

import (
    "fmt"
    "github.com/dgrijalva/jwt-go"
    "io/ioutil"
    "time"
)

func panicOnError(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    signBytes, err := ioutil.ReadFile("./jwtRS256.key")
    panicOnError(err)
    signKey, err := jwt.ParseRSAPrivateKeyFromPEM(signBytes)
    panicOnError(err)
    verifyBytes, err := ioutil.ReadFile("./jwtRS256.key.pub")
    panicOnError(err)
    verifyKey, err := jwt.ParseRSAPublicKeyFromPEM(verifyBytes)
    panicOnError(err)
    claims := jwt.MapClaims{
        "exp": time.Now().Add(time.Minute).Unix(),
    }
    fmt.Println(claims)
    t := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
    tokenString, err := t.SignedString(signKey)
    panicOnError(err)
    fmt.Println(tokenString)
    token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
        return verifyKey, nil
    })
    panicOnError(err)
    fmt.Println(token.Claims)
}

0
投票

作为补充答案,为了避免误解:

RS256
不要求私钥的大小仅为256位。
256
中的
RS256
描述了使用的哈希算法是
SHA-256
。与此相反,您在密钥生成中传递的位大小描述了模数的长度,并且是独立的(并且可能应该大于 256 位)。我也陷入了同样的困境。

© www.soinside.com 2019 - 2024. All rights reserved.