为了账号安全,请及时绑定邮箱和手机立即绑定

在 Go 中使用 Jose 加密/解密 JWE

在 Go 中使用 Jose 加密/解密 JWE

Go
一只名叫tom的猫 2023-08-07 19:07:40
我正在尝试创建 JWE 解密函数,但无法确定如何使用 Go Jose 接口来执行此操作。我已经使用密码进行了加密(对于此用例,我更喜欢使用密码):    token := jwt.NewWithClaims(        jwt.SigningMethodHS256,        claims,    )    ss, err := token.SignedString("thisisatestpassphraserighthere")    if err != nil {        panic("COULD_NOT_GENERATE")        return    }    privateKey, err := rsa.GenerateKey(rand.Reader, 2048)    if err != nil {        panic(err)    }    publicKey := &privateKey.PublicKey    encrypter, err := jose.NewEncrypter(        jose.A128CBC_HS256,        jose.Recipient{Algorithm: jose.RSA_OAEP, Key: publicKey},        nil,    )    if err != nil {        return    }    object, err := encrypter.Encrypt([]byte(ss))    if err != nil {        errRes = s.Error(codes.Internal, err, nil)        return    }    key, err := object.CompactSerialize()    if err != nil {        errRes = s.Error(codes.Internal, err, nil)        return    }    fmt.Println(key)上面的代码创建一个 JWT,对其进行编码,压缩并返回密钥。然而,现在还不完全清楚如何使用密码对其进行解密。Jose 文档上有一个 JWE 示例:https://godoc.org/gopkg.in/square/go-jose.v2#example-Encrypter--Encrypt所以我考虑了这一点:    object, err = jose.ParseEncrypted(Key)    if err != nil {        panic(err)    }    decrypted, err := object.Decrypt(...)但我不知道在省略号内该放什么。我似乎无法确定如何根据密码传递密钥。
查看完整描述

2 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我似乎做错了一些事情。首先 rsa.GenerateKey 使用随机值。这是完全错误的:-p 以下是如何使用令牌将 JWT 加密为 JWE:


rcpt := jose.Recipient{

    Algorithm:  jose.PBES2_HS256_A128KW,

    Key:        "mypassphrase",

    PBES2Count: 4096,

    PBES2Salt: []byte{ your salt... },

}

enc, err := jose.NewEncrypter(jose.A128CBC_HS256, rcpt, nil)

if err != nil {

   panic("oops")

}

jewPlaintextToken, err := enc.Encrypt(jwtToken)

if err != nil {

    panic("oops")

}

key, err := object.CompactSerialize()

if err != nil {

    panic("oops")

}

这是解密的方式:


// Decrypt the receive key

jwe, err := jose.ParseEncrypted(jewPlaintextToken)

if err != nil {

    panic("oops")

}

decryptedKey, err := jwe.Decrypt("mypassphrase")

if err != nil {

    panic("oops")

}

如果有人发现此方法有任何重大问题/安全问题,请提及。


查看完整回答
反对 回复 2023-08-07
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

但我不知道在省略号内该放什么。我似乎无法确定如何根据密码传递密钥。

JWE文档中的示例来看,您必须传递私钥。解密见下面部分

https://godoc.org/gopkg.in/square/go-jose.v2#JSONWebEncryption.Decrypt

// Generate a public/private key pair to use for this example.

privateKey, err := rsa.GenerateKey(rand.Reader, 2048)

if err != nil {

    panic(err)

}


// Parse the serialized, encrypted JWE object. An error would indicate that

// the given input did not represent a valid message.

object, err = ParseEncrypted(serialized)

if err != nil {

    panic(err)

}


// Now we can decrypt and get back our original plaintext. An error here

// would indicate the the message failed to decrypt, e.g. because the auth

// tag was broken or the message was tampered with.

decrypted, err := object.Decrypt(privateKey)

if err != nil {

    panic(err)

}


fmt.Printf(string(decrypted))


查看完整回答
反对 回复 2023-08-07
  • 2 回答
  • 0 关注
  • 156 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信