1

我正在使用JOSESwift。当我使用 Encrypter 方法时,它返回时nil没有任何错误的详细信息。下面是我的代码示例。任何人都可以请帮助为什么 Encrypter 方法返回nil

意图:我正在尝试为我的 JWE 对象设置包装密钥,并且从 JOSESwift 我可以理解 Encrypter 接受了该 cek 密钥。

// Jose implementation.
        let joseHeader = JWEHeader(algorithm: .direct,
                                   encryptionAlgorithm: .A128CBCHS256)
        let joseEncrypter = Encrypter(keyEncryptionAlgorithm: .RSAOAEP,
                                      encryptionKey: cekKeyData,
                                      contentEncyptionAlgorithm: .A128CBCHS256)!
        let josePayload = Payload(Data(base64Encoded: jsonString)!)

        let joseJWE = try? JWE(header: joseHeader, payload: josePayload, encrypter: joseEncrypter)
4

1 回答 1

0

我怀疑您传递的密钥类型错误,或者错误地选择.RSAOAEP了您的算法并表示.direct.

///   - key: The key used to perform the encryption. If the `keyEncryptionAlgorithm` is `.direct`, the
///          `encryptionKey` is the shared symmetric content encryption key. Otherwise the `encryptionKey` is the
///           public key of the receiver. See [RFC-7516](https://tools.ietf.org/html/rfc7516#section-5.1) for
///           details.

nil仅在给定传递参数的情况下从该代码返回:

switch (keyEncryptionAlgorithm, contentEncyptionAlgorithm) {
case (.RSA1_5, .A256CBCHS512), (.RSAOAEP, .A256CBCHS512), (.RSAOAEP256, .A256CBCHS512), (.RSA1_5, .A128CBCHS256), (.RSAOAEP, .A128CBCHS256), (.RSAOAEP256, .A128CBCHS256):
    guard type(of: key) is RSAEncrypter.KeyType.Type else {
        return nil
    }
    // ...

这表明cekKeyData不是 RSAEncrypter.KeyType 类型。我怀疑它是生成的 AES 密钥。

于 2020-01-24T17:47:21.957 回答