亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 332 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號