第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

Go
梵蒂岡之花 2022-04-20 20:57:05
我正在嘗試創(chuàng)建 JWE 解密函數(shù),但無法確定如何使用 Go Jose 接口來執(zhí)行此操作。我已經(jīng)使用密碼短語加密(我更喜歡這個用例的密碼短語):    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)上面的代碼創(chuàng)建了一個 JWT,對其進行編碼、壓縮并返回密鑰。然而,現(xiàn)在如何用密碼解密它并不完全清楚。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(...)在橢圓內(nèi),我不知道該放什么。我似乎無法確定如何根據(jù)密碼傳遞密鑰。
查看完整描述

2 回答

?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超4個贊

我似乎做錯了一些事情。首先 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")

}

如果有人發(fā)現(xiàn)此方法有任何重大問題/安全問題,請?zhí)峒啊?/p>


查看完整回答
反對 回復 2022-04-20
?
侃侃無極

TA貢獻2051條經(jīng)驗 獲得超10個贊

在橢圓內(nèi),我不知道該放什么。我似乎無法確定如何根據(jù)密碼傳遞密鑰。


從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))


查看完整回答
反對 回復 2022-04-20
  • 2 回答
  • 0 關注
  • 425 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號