2 回答

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>

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))
- 2 回答
- 0 關注
- 425 瀏覽
添加回答
舉報