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>

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))
- 2 回答
- 0 關(guān)注
- 321 瀏覽
添加回答
舉報(bào)