0

I'm on iOS and trying to match the Android AES-CTR decryption. The code on Android:

Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(
      Cipher.DECRYPT_MODE,
      key,
      new IvParameterSpec(ivStr));
  result =
      cipher.doFinal(dataToDecrypt);

I'm trying to use CommonCrypto in swift to implement the same logic above, there is what I have for now:

    func AESCRTDecrypt(with key: [UInt8], encryptedData: [UInt8], iv: [UInt8]) -> [UInt8]? {
    var resultBytes = [UInt8](repeating: 0, count: encryptedData.count)
    var resultLength = 0
    let status = CCCrypt(CCOperation(kCCDecrypt),
                         CCAlgorithm(kCCAlgorithmAES),
                         CCOptions(????), // THIS IS MY Question: how to set AES-CTR-DECRYPT option so it can be the same as Android above as "AES/CTR/NoPadding"? 
                         key,
                         kCCKeySizeAES256,
                         iv,
                         encryptedData,
                         encryptedData.count,
                         &resultBytes,
                         resultBytes.count,
                         &resultLength)
    guard status == kCCSuccess else {
        Log.e("Failed in AES-CRT-DECRYPTION. Status: \(status)")
        return nil
    }
    return resultBytes
}

My question was commented above: how to set the option so it can be same as Android above as "AES/CTR/NoPadding"? I checked the options and some example code for this function, there are only 2 options in the enum for options:

public var kCCOptionPKCS7Padding: Int { get }
public var kCCOptionECBMode: Int { get }

Neither of them is what I want..

Could any help here? Thanks!!

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
RobotX
  • 208
  • 2
  • 15

0 Answers0