Nico
Nico

Reputation: 1838

how to know programmatically any file is encrypted or not ?- iPhone

I have a sqlite file(it may be either encrypted or non-encrypted) in Document Directory in an already created app, now in updated version i have to check it for decryption, if file is found encrypted then we will use it after decryption but it found non-encrypted then we will use it simply.

Is there any way to do it?

Upvotes: 3

Views: 832

Answers (2)

Martin R
Martin R

Reputation: 539965

According to "The SQLite Database File Format", every SQLite file starts with the bytes "SQLite format 3", followed by a nul termination.

Assuming that the encryption scrambles all bytes of the file, you can read the first 16 bytes and check if they match the above string.

But a simpler method is to just open the file with sqlite3_open() or one of the related open calls. If that fails with the error code SQLITE_CORRUPT, you can assume that the file was encrypted, so you decrypt it and open it again.

Upvotes: 5

Akira
Akira

Reputation: 4071

If you know what the encryption method is beforehand, you can try to find fingerprints of the method in order to detect if the file is encrypted or not. But there are encryption methods that are designed specifically to hide encryption into files that do not look encrypted.

This is the case of watermarking encryption embedded into images. You can open the images and they will look like any ordinary image file, but in fact there is encrypted information there.

Upvotes: 0

Related Questions