Encrypt/Decrypt data using Google Tink library swift ios

128 Views Asked by At

I would like to decrypt encrypted data from Google Drive using google/tink in iOS.

I am uploading the encrypted file to Google Drive from the Android app and the same file downloading in the iOS app. Tink encryption/decryption works fine in Android, but now I want to do the same thing in iOS too.

So I have followed iOS Tink library documentation and concluded code like below,

do {
                                            
     let config = try TINKAllConfig()

     try TINKConfig.register(config)
                                            
     let takt = try TINKAeadKeyTemplate(keyTemplate: .TINKAes128Gcm)
                                            
     let handle = try TINKKeysetHandle(keyTemplate: takt)
                                            
     let aead = try TINKAeadFactory.primitive(with: handle)
                                                                                        
     guard let url = Bundle.main.url(forResource: "key", withExtension: "json") else {
          return
     }
                                            
     let aditionalData = try Data(contentsOf: url)
                                            

     let decrypted = try aead.decrypt(data!, withAdditionalData: aditionalData) // data is from google drive responce
                                            
     try decrypted.write(to: filePath)
     } catch {
         print("===tink error===", error)
  }

Every time try aead.decrypt(data!, withAdditionalData: aditionalData) throws an error like the below, I do not know how to fix it.

Error Domain=TinkErrorDomain Code=3 "Tink Error" UserInfo={NSLocalizedDescription=Tink Error, NSLocalizedFailureReason=decryption failed

0

There are 0 best solutions below