0

I haven't been able to find a clear answer to this question, so here it is;

I want to use Realm for Android to store files in an encrypted way, then be able to open/view those files without needing to actually save them in the phone. Those would be sensitive data file so It would be perfect if it stay in the local database of my app.

Is Realm well suited for my case or if not, what else would be for Android?

Jaythaking
  • 2,200
  • 4
  • 25
  • 67

2 Answers2

2

You can check out this previous answer for a more deep explanation about saving images on Realm (it's on iOS, but the basics are the same: Don't do it if there's a lot of images.)

In your specific use case, did you consider saving encrypted images on the filesystem and the references for the image in Realm? You just need the file path and the encryption key in the database to work.

Orlando
  • 1,509
  • 2
  • 19
  • 27
  • The document/image wouldn't be natively encrypted, that's why It would be perfect to keep it only in Realm, since it has it's own encryption system – Jaythaking Mar 15 '17 at 20:47
  • Sure, Realm uses a 256 bit AES encryption algorithm, a well known encryption algorithm, surely there are some libraries on Android to do the heavylifting for you. You can encrypt the images/documents before saving them in the filesystem and then in Realm just keep the Path and the encryption key for each file (because it would be better if every file had a different encryption key.) – Orlando Mar 15 '17 at 20:51
  • I though about that, but couldn't find any easy solution. Encrypt/decrypt a file evrerytimes by hand seems a bit of a hassle... Unless there is some magic library out there I didn't know... – Jaythaking Mar 15 '17 at 21:19
  • Yes, it may be some extra programming work. Anyway, assuming you save the images in Realm then the database must perform those operations, so from the processor's point of view there's no extra work to do (the encrypt/decrypt process is performed either by Realm or by your code.) I'm not an Android expert so I can't recommend you a specific library for this job. – Orlando Mar 15 '17 at 22:15
2

Realm suits or not. You may need to reconsider.

If you just want to encrypt a database, it is easy. Just give it an encryption key when you init a Realm instance.

However, there are some side effects.

  1. Save big files to Realm will slow it down.
  2. The danger of data corruption.
    If you database file is corrupted, all data is lost. So you have to backup. That means saving files into Realm will cause your app takes at least twice the size of saving the files in filesystem directly.
  3. The encryption key.
    You must design an encryption key that basing on each device other than just using one same key in your app. Or someone just needs to copy the database file from another's phone to his own and your app will decrypt the database for him.
Owen Zhao
  • 3,205
  • 1
  • 26
  • 43
  • Realm file have a limit of 16mb though, which isn't that big... Will it slow it down considerably? Also, is it possible to read those file without needing to save them in the file system storage first? I like the idea of storing everything in the same place, I would prefer avoiding handling myself the storage/encrypting in the external drive – Jaythaking Mar 16 '17 at 13:10
  • @Jaythaking No, the 16 MB is the limitation on NSString and NSData on iOS or macOS only. You said you were on Android, right? On the speed part, I suggest you to create some data and test it yourself. You don't need to save it to file system to read it. It works in RAM. – Owen Zhao Mar 16 '17 at 13:23
  • Oh so you're saying there is no limitation on Android? Yeah, I'll do some test with this method then and if it's too slow I might try that one [Android-Simple-Storage] https://android-arsenal.com/details/1/951#get-files – Jaythaking Mar 16 '17 at 13:26
  • 1
    At first, I added 16 MB on my answer before I published it. I mainly work on Apple platforms These days. However, when I checked Realm docs on Java, there was no such limitation on the limitation section. – Owen Zhao Mar 16 '17 at 13:30