jjzjj

ios - swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?

coder 2024-07-08 原文

我正在使用 rsaEncryptionOAEPSHA256AESGCM 在 iOS 上使用 SecKeyCreateEncryptedData 加密一些数据,然后在 golang 后端解密相同的数据。我正在使用 3072 位 rsa 公钥来加密对称 key 。当我从 iOS 获取数据到后端时,我能够成功解密对称 key ,但 gcm 标签验证失败。我使用的是与 iOS 相同的 16 字节 IV,但不知道 iOS 在加密时是否使用任何 aad(附加身份验证数据)。有谁知道 rsaEncryptionOAEPSHA256AESGCM for iOS 是否使用了一些 aad?这适用于 iOS 10+。

我已经尝试过使用 nil、空的 16 字节数组、aes key 本身、nonce 作为 aad,但这些都不起作用。

在 Swift 中:

private let algorithm = SecKeyAlgorithm.rsaEncryptionOAEPSHA256AESGCM
// Key is 3072 bit RSA public key
// API doesnt take any aad
SecKeyCreateEncryptedData(key, algorithm, cfData, &error)

在 Go 语言中:

c, err := aes.NewCipher(aesKey)
gcm, err := cipher.NewGCMWithNonceSize(c, 16)
nonce := make([]byte, 16)
// Data has both the ciphertext and the gcm tag as the last 16 bytes
// Last field in the api is the aad
dec, err := gcm.Open(nil, nonce, data, nil)

这是我在golang中看到的错误 “密码:消息身份验证失败”并且从验证 gcm 标记的代码中抛出错误。

最佳答案

找到了。 AAD是ASN.1 DER格式的RSA公钥。

关于ios - swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57900273/

有关ios - swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?的更多相关文章

随机推荐