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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

Go
一只名叫tom的貓 2023-08-07 19:07:40
我正在嘗試創(chuàng)建 JWE 解密函數(shù),但無法確定如何使用 Go Jose 接口來執(zhí)行此操作。我已經(jīng)使用密碼進(jìn)行了加密(對(duì)于此用例,我更喜歡使用密碼):    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)建一個(gè) JWT,對(duì)其進(jìn)行編碼,壓縮并返回密鑰。然而,現(xiàn)在還不完全清楚如何使用密碼對(duì)其進(jìn)行解密。Jose 文檔上有一個(gè) JWE 示例:https://godoc.org/gopkg.in/square/go-jose.v2#example-Encrypter--Encrypt所以我考慮了這一點(diǎn):    object, err = jose.ParseEncrypted(Key)    if err != nil {        panic(err)    }    decrypted, err := object.Decrypt(...)但我不知道在省略號(hào)內(nèi)該放什么。我似乎無法確定如何根據(jù)密碼傳遞密鑰。
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

我似乎做錯(cuò)了一些事情。首先 rsa.GenerateKey 使用隨機(jī)值。這是完全錯(cuò)誤的:-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)此方法有任何重大問題/安全問題,請(qǐng)?zhí)峒啊?/p>


查看完整回答
反對(duì) 回復(fù) 2023-08-07
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

但我不知道在省略號(hào)內(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))


查看完整回答
反對(duì) 回復(fù) 2023-08-07
  • 2 回答
  • 0 關(guān)注
  • 321 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)