Flutter Asset Encryption

Mustafa Ali Dikçinar
Team Kraken
Published in
3 min readNov 21, 2022

Keep your assets safe.

Hi, in this article I will explain how to keep your assets safe with encryption.

Why We Need Asset Encryption?

As you know, when we compile our app it is easy to get assets from inside the app package. For example, we can easily reach the assets and use them when we open an apk file with WinRAR(or you can change .apk to .zip).

Non encypted assets

But what if we have own created assets that we want to keep them private?

  • Well, to do that we can use Encrypt package!

Let’s Encrypt Our Assets

First of all, we need a secure-key to encrypt our files. To generate a secure key, we need to activate encrypt as globally. Open a terminal window and follow these steps.

dart pub global activate encrypt

then generate a secure-key

secure-random

you should get a key someting like this

yA7exe4ZFq5g2St1VDDexMJ5yr+8w+0RWivkfIrUVmc=

keep it somewhere we will need that.

Now we need to create a Dart project to encrypt our assets. Here is my example Dart project. You can directly use it to encrypt your files or create your encrypter by look into my code.

  • Move your assets to the Input folder.
  • Set your secure-key that we generated before
  • Run the project
  • Move encrypted Assets from Output folder to your Flutter project
Encrypter main code
Encrypt method

Now we encrypted our assets, we will not be able to use them directly because these are encrypted now. We need to decrypt them on runtime while using on Flutter project.

Decrypt Assets on Flutter Side

Open your Flutter project and add Encrypt package to pubspec.yaml.

encrypt: ^5.0.1

Create a singleton DecryptionService

DecryptionService

Note: I strongly suggest you don’t keep your secure key hard coded. You can read more How to Store API Keys in Flutter.

I used FutureBuilder for this example and of course many things can be refactored, but these refactors are not our focus right now :)

Well, finally it worked like a charm 💙

Conclusion

I have tried to give an idea of how encryption can be applied to our assets. I hope I achieved that.

🎉 Thanks for reading my article.

⭐️ I also would like to thank @BGoktugOzdemir and @UmithanGuldemir review this article.

💙 You can find me on Twitter and Linkedin.

👉🏻 You can find all the source code on Github.

--

--