Search code examples
iosswiftsqliteencryptionaes

How to encrypt whole file using AES-256 encryption?


I have one SQL database file in my bundle. I need to encrypt that file using AES-256 and need to write this data to another file.

I have already tried to use RNCryptor and CryptoSwift as well but, I am to getting exact solutions for my problem.


Solution

  • Just read any file as Data or NSData instance and apply encryption to that and write back with a custom extensions may be.. You can use the lib suggested by @Ashish Rana or https://gist.github.com/hfossli/7165dc023a10046e2322b0ce74c596f8

    1. Load data from file guard let fileURL = Bundle.main.url(forResource: "file-to-be-encrypted", withExtension: "txt") else { return } guard let fileData = try? Data(contentsOf: fileURL) else { return }

    2. Apply encryption let encrypted = try aes.encrypt(digest)

    3. Save encrypted file back try encrypted.write(to: encURL)

    If you do not want to share the encrypted file and just want to secure it then you can use OS base encryption like mentioned here: https://www.tomsguide.com/us/how-to-encrypt-ios,news-18338.html