【问题标题】:Get IV and secret key from base64 string从 base64 字符串中获取 IV 和密钥
【发布时间】: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


【解决方案1】:

花了 2 天后,我终于能够在 Swift 中转换此代码希望这对其他人有所帮助

func decryptCode(_ cipher:String, _ key:String)-> String{
    var keyBytes: [UInt8] = []
    var codeBytes: [UInt8] = []
    var code = ""

    if let keyData = NSData(base64Encoded:key, options: .ignoreUnknownCharacters) {
        keyBytes = [UInt8](keyData as Data)
    }
    if let codeData = NSData(base64Encoded: cipher, options: .ignoreUnknownCharacters) {
        codeBytes = [UInt8](codeData as Data)
    }
    // First 4 bytes define the IV length
    // next 12 to 16 bytes are reserve for IV
    //and remaining bytes are actual cipher text
    debugPrint(codeBytes)

    let sizeOfIV = 4
    let ivUInt8Array = Array([UInt8](codeBytes)[0 ..< sizeOfIV])
    let ivLength:Int = Int(ivUInt8Array.reduce(0, +))

     if ivLength < 12 || ivLength >= 16{
        return code
    }

    let codeBytescount = [UInt8](codeBytes).count
    let remainingBytes = Array([UInt8](codeBytes)[sizeOfIV ..< codeBytescount])
    let remainingBytescount = [UInt8](remainingBytes).count

    let iv = Array([UInt8](remainingBytes)[0 ..< ivLength])
    let cipher = Array([UInt8](remainingBytes)[ivLength ..< remainingBytescount])
    do{
        let gcm = GCM(iv: iv, mode: .combined)
        let aes = try AES(key: keyBytes, blockMode: gcm, padding: .pkcs5)
        IFLOG("aes created")
        let decrypted = try aes.decrypt(cipher)
        IFLOG("decrypted completed")
        if let decryptedString = String(bytes: decrypted, encoding: .utf8) {
            code = decryptedString
        }
        debugPrint(code)

    }catch let error as AES.Error {
        debugPrint(error.localizedDescription)
        return code
    } catch {
        return code
    }
    return code
}

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多