swift - Jose Encrypter 返回零
问题描述
我正在使用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)
解决方案
我怀疑您传递的密钥类型错误,或者错误地选择.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 密钥。
推荐阅读
- arrays - 有没有办法在 Swift 中将数组存储在数组中
- excel - 导致有关 ActiveWorkbook.Save 的运行时错误的代码未在我的 Workbook_Open() 事件中触发
- reactjs - 使用来自 refs 的信息来设置状态时的无限重新渲染循环
- embedded - 为什么链接描述文件会有多个节命令?
- json - 将回调保存为流星会话变量的一部分
- jenkins - 如何防止两个不同的 Jenkins 管道同时运行?
- python - 在python中将比较打印为数字而不是真或假
- javascript - Excel不显示俄语单词。它只适用于英文单词
- c++ - 使用 Visual Studio Native cpp Unite 测试包含文件 I/O 的问题单元测试函数
- selenium - selenium 中的搜索上下文是什么意思以及 webdriver 实现搜索上下文接口的原因