ios - swift api SecKeyCreateEncryptedData 使用的额外认证数据是什么?
问题描述
我正在使用 rsaEncryptionOAEPSHA256AESGCM 在 iOS 上使用 SecKeyCreateEncryptedData 加密一些数据,然后在 golang 的后端解密相同的数据。我正在使用 3072 位 rsa 公钥来加密对称密钥。当我从 iOS 获取数据到后端时,我能够成功解密对称密钥,但 gcm 标签验证失败。我正在使用与 iOS 相同的 16 字节 IV,但不知道 iOS 在加密时是否使用了任何 aad(附加身份验证数据)。有谁知道 iOS 的 rsaEncryptionOAEPSHA256AESGCM 是否使用了一些 aad?这适用于 iOS 10+。
我已经尝试过使用 nil、空的 16 字节数组、aes 键本身、nonce 作为 aad,但这些都不起作用。
在斯威夫特:
private let algorithm = SecKeyAlgorithm.rsaEncryptionOAEPSHA256AESGCM
// Key is 3072 bit RSA public key
// API doesnt take any aad
SecKeyCreateEncryptedData(key, algorithm, cfData, &error)
在 Golang 中:
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 "cipher: message authentication failed" 中看到的错误,该错误是从验证 gcm 标签的代码中抛出的。
解决方案
找到了。AAD 是 ASN.1 DER 格式的 RSA 公钥。
推荐阅读
- amazon-web-services - AWS Athena 对于 api 来说太慢了?
- r - 使用 .SD 和 by 的 data.table 性能
- python - 如何在 Python 中调整 OpenCV 图像的大小?
- python - 在分布式 Spark DataFrame 上使用 functools reduce
- java - Selenium 参数 [0].scroll.to 函数第二次不起作用
- sql - 将 csv 和 json 数据从 S3 复制到 Redshift
- neo4j - 在哪里可以找到 Neo4j Desktop 的 apoc.conf?
- c# - HttpWebResponse 返回错误的 html
- powershell - Powershell - 如何根据文件名创建文件名数组?
- python-3.x - 升级到 Anaconda 2020.02 后“import pandas as pd”执行失败