I have a blackbox system whose code is hidden from the end user. That's the intent anyway, I know some may break into it, but generally it's closed.

It's running on PHP, and I need to export a database for backup purposes. This database contains some data I don't want the user to tamper with, so the idea is obvious - encrypt the data file.

I can use RSA, but I'm not sure if it's the best, considering both private and public keys would be embedded in the code. I can also password-protect a zip containing the data file. There's maybe other methods I'm unaware of.

Here's a silly ASCII art diagram of what I want


+----------black-box system-----------+
|                                     |
|   +----------+      ??????????????  |  +-------------+
|   | SQL DUMP | ===> ? ENCRYPTION ? ==> # BACKUP FILE #
|   +----------+      ??????????????  |  +-------------+
|                                     |         v
|   +----------+      ??????????????  |  +-------------+
|   | SQL DUMP | <=== ? DECRYPTION ? <== # BACKUP FILE #
|   +----------+      ??????????????  |  +-------------+
|                                     |


The file should be readable by the app again, for "restore from backup" functionality.

Please, suggest how to protect the file. Thanks.

  • Who must have the ability to decrypt the data so that it is usable? – Robert Harvey Oct 31 '14 at 18:40
  • See the updated diagram ;) Basically the system itself should be able to decrypt the file, protecting it from the user's attempt at messing it up. Also, the support service may have the ability to decrypt the file, but that's not essential. – MightyPork Oct 31 '14 at 18:41
  • openssl can be used to encrypt files. Just put it into your backup script. stackoverflow.com/a/16056298/156671 – GrandmasterB Oct 31 '14 at 18:41
  • Alright, well, you haven't really given us any criteria for selecting an encryption algorithm. Any decent one should satisfy your requirement, however. You don't need public keys for this; just use symmetric encryption. – Robert Harvey Oct 31 '14 at 18:45
  • Well that's true, I just need some idea what to try. It must be available in PHP. What'd you suggest? – MightyPork Oct 31 '14 at 18:46

Your model is casual security, so you might as well just flip a coin. Not to be sarcastic, but if you store your keys in code and are sharing the key across all users of your app, and your goal is just to obscure the data from casual tampering, then it really doesn't matter. Pick a symmetric cipher (AES-128 block cipher for example) and be done. It is commonly supported and also fast on modern hardware.

Just make sure not to market the feature as "secure". It is casual anti-tampering, but not secure.

| improve this answer | |
  • Three nitpicks: (1) AES is not a stream cipher (2) AES-256 has no practical advantage over AES-128. 128 bit is safe against brute force as long as computers are made out of matter and occupy spacetime, CTCs excluded. (3) AES-256's key schedule may be weaker, it's been exploited for related-key attacks and attacks only ever get better. – user7043 Oct 31 '14 at 19:15
  • Thanks, yes I wrote RC4 orginally, then edited before submitting and failed to revise the sentence. – codenheim Oct 31 '14 at 19:18
  • WRT the rest, I'm aware of those things. As I said in the answer, it seems the OP is after casual anti-tampering. Though you have a point, AES-128 is faster by a good margin, so maybe a better "choice" – codenheim Oct 31 '14 at 19:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.