【发布时间】:2019-01-10 14:01:40
【问题描述】:
我正在使用 crypto swift 库但是我必须在 swift 中编写以下逻辑,因为我是 kotlin 的新手才能理解语法。
任何线索将不胜感激
fun decryptAES(data: ByteArray, secretKey: ByteArray): ByteArray {
try {
val byteBuffer = ByteBuffer.wrap(data)
val ivLength = byteBuffer.int
if (ivLength < 12 || ivLength >= 16) {
throw IllegalArgumentException("invalid iv length")
}
val iv = ByteArray(ivLength)
byteBuffer.get(iv)
val cipherText = ByteArray(byteBuffer.remaining())
byteBuffer.get(cipherText)
val encryptCipher = Cipher.getInstance("AES/GCM/PKCS5Padding")
encryptCipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(secretKey, "AES"), GCMParameterSpec(128, iv))
return encryptCipher.doFinal(cipherText)
} finally {
Arrays.fill(secretKey, 0.toByte())
}
}
【问题讨论】:
-
是否可以为此函数提供一个可验证的示例(即“对于输入 (X, Y) 我应该得到输出 Z”)?
-
花了 2 天终于可以在 Swift 中转换此代码。
-
太棒了。然后,您可以提供一个答案来帮助面临同样问题的任何其他用户。
-
基本上,我们必须实现 ByteArray 在 Kotlin 中所做的事情,所以我在 Swift 中实现了相同的行为,是的,我将提供相同的答案。
标签: ios swift4.2 cryptoswift