I am trying to decrypt a message on iOS that is encrypted by backend. I receive the key and encrypted string and I am using Common Crypto AES GCM as below but receiving Authentication Failure.
I tried to create "AES.GCM.Nonce()" but I don't have a Authentication Tag as AES.GCM requires it when Nonce is specified.
However, I am able to decrypt the message using third-party framework called "CryptoSwift".
I would like to know if I can achieve the same using CommonCrypto framework provided by apple.
Code to Generate Key:
Code Block let sealedBoxToOpen = try! AES.GCM.SealedBox(combined: decryptToData) if let decryptedData = try? AES.GCM.open(sealedBoxToOpen, using: key)
I tried to create "AES.GCM.Nonce()" but I don't have a Authentication Tag as AES.GCM requires it when Nonce is specified.
However, I am able to decrypt the message using third-party framework called "CryptoSwift".
Code Block "decryptToData" -> Base64EncodedString var codeBytes = [UInt8]() if let codeData = NSData(base64Encoded: cipher, options: .ignoreUnknownCharacters) { codeBytes = [UInt8](codeData as Data) } let iv = Array([UInt8](codeBytes)[0 ..< 32]) let cipher = Array([UInt8](codeBytes)[iv.count ..< codeBytescount]) let gcm = GCM(iv: iv, mode: .combined) guard let derKey = createKey(password:Data(key.utf8),salt: Data(iv), iteration: 62233) else { return } keyBytes = [UInt8](derKey) let aes = try AES(key: keyBytes, blockMode: gcm, padding: .pkcs5) let decrypted = try aes.decrypt(cipher) guard let decryptedString = String(bytes: decrypted, encoding: .utf8) else { return nil }
I would like to know if I can achieve the same using CommonCrypto framework provided by apple.
Code to Generate Key:
Code Block class func createKey(password: Data, salt: Data, iteration: Int) -> Data? { let length = kCCKeySizeAES256 var status = Int32(0) var derivedBytes = [UInt8](repeating: 0, count: length) password.withUnsafeBytes { (passwordBytes: UnsafePointer<Int8>!) in salt.withUnsafeBytes { (saltBytes: UnsafePointer<UInt8>!) in status = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2), passwordBytes, password.count, saltBytes, salt.count, UInt32(kCCPRFHmacAlgSHA256), UInt32(iteration), &derivedBytes, length) } } guard status == 0 else { return nil } return Data(bytes: UnsafePointer<UInt8>(derivedBytes), count: length) }