swift - 使用 Apples CryptoKit 在 iOS 和 Kotlin/Java 之间进行跨平台 AES 加密

标签 swift kotlin encryption aes apple-cryptokit

我想使用 Apples CryptoKit 将加密数据从运行 kotlin 应用程序的服务器发送到 iOS 应用程序。
我在初始化 AES.GCM.SealedBox 时遇到问题并解密数据。总的来说,我不明白什么是 Sealboxs tag是为了。

所以首先是 Kotlin 方面:

        fun ByteArray.aesEncrypt(key: ByteArray, iv: ByteArray? = null): ByteArray {
            return aes(this, Cipher.ENCRYPT_MODE, key, iv)
        }
        private fun aes(self: ByteArray, mode: Int, key: ByteArray, iv: ByteArray?): ByteArray{
            val skey = SecretKeySpec(key, "AES")
            val cipher = Cipher.getInstance("AES/GCM/PKCS5Padding")
            println("MODE: ${cipher.algorithm}")
            iv?.let {
                cipher.init(mode, skey, GCMParameterSpec(128, iv))
            }?: run{
                cipher.init(mode, skey)
            }
            val cipherText = ByteArray(cipher.getOutputSize(self.size))
            var ctLength = cipher.update(self, 0, self.size, cipherText, 0)
            ctLength += cipher.doFinal(cipherText, ctLength)
            return cipherText
        }

IOS:
    static private let privateKey = SymmetricKey(size: SymmetricKeySize.bits128)
    static private let nonce = AES.GCM.Nonce()

    static func decrypt(_ data: Data) -> Data {
        print("Encrypted data \(data.bytes)")
        print("Private key: \(privateKey.data.bytes)")
        print("Nonce: \(Array(nonce))")
        let boxToDecrypt = try! AES.GCM.SealedBox(combined: data)
        let plainData = try! AES.GCM.open(boxToDecrypt, using: privateKey)
        return plainData
    }

因为双方都有相同的 key 和 iv/nonce。我遇到的错误消息是:
CryptoKit.CryptoKitError.incorrectParameterSize

排队:
let boxToDecrypt = try! AES.GCM.SealedBox(combined: data)

编辑我:
其他有效载荷信息:

服务器( Kotlin ):
Not encrypted: 0,0,0,0,0,0,0,1
Key: 169,152,60,154,77,50,10,63,60,166,48,129,1,68,219,250
IV: 134,191,34,26,111,146,17,54,31,212,74,14
Encrypted: 158,154,213,95,227,42,155,199,169,183,166,67,139,154,198,172,229,82,34,30,40,188,41,73



客户端(iOS):
Encrypted data [158, 154, 213, 95, 227, 42, 155, 199, 169, 183, 166, 67, 139, 154, 198, 172, 229, 82, 34, 30, 40, 188, 41, 73]
Nonce: [134, 191, 34, 26, 111, 146, 17, 54, 31, 212, 74, 14]
Private key: [169, 152, 60, 154, 77, 50, 10, 63, 60, 166, 48, 129, 1, 68, 219, 250]

最佳答案

你能用你的设置试试这个(或类似的东西)吗?从我不明白的
您需要用随机数作为数据前缀,因为来自 kotlin/java 的数据包含密文和末尾的标签。 CryptoKit 需要 nonce ||密文 ||标签。

func decrypt(data: Data) -> String {
    // need to prefix data with nonce, because data from kotlin/java contains the cipher text plus the tag at the end.
    // we want nonce || ciphertext || tag for CryptoKit to be happy
    let combine = nonce + data
    if let myNewSealedBox = try? AES.GCM.SealedBox(combined: combine),
        let res = try? AES.GCM.open(myNewSealedBox, using: mykey),
        let myText = try? String(decoding: res, as: UTF8.self) {
        return myText
    }
    return ""
}

关于swift - 使用 Apples CryptoKit 在 iOS 和 Kotlin/Java 之间进行跨平台 AES 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61332076/

相关文章:

ios - 可通过继承进行编码

c# - AES GCM 手动 byte[] IV 和标记与加密信息的串联

java - 在 Java 中实现 SHA1withRSA

Swift NSManagedObjects 不支持 mutableSetValueForKey

ios - 我怎样才能用一个segue做到这一点?

ios - 使用 Alamofire 解码 json 时出错

android - 使用 gradle maven-publish 插件导出的 Kotlin Android 库模块不添加依赖项

intellij-idea - IntelliJ : No autocomplete for Kotlin?

android - Kotlin + 刀柄错误。 [Hilt] 和 java.lang.reflect.InvocationTargetException(无错误消息)

python - 尝试将字符串加密为 ASCII