Page cover

salt-shakerSalt

Password Security in Flutter Apps: AES Encryption with Salt and IV

Understanding AES, Salt, and IV

  • AES (Advanced Encryption Standard):

    • A powerful and widely used symmetric encryption standard.

    • Uses the same key for both encryption and decryption.

    • Secure and efficient for mobile applications.

  • What is Salt, and why is it needed?

    • Salt is a random string added before encryption.

    • Ensures that encrypting the same data with the same passphrase produces different results.

    • Prevents dictionary attacks and lookup table attacks.

  • IV (Initialization Vector):

    • IV is a randomly generated initialization value used in encryption modes like CBC.

    • Ensures that the same plaintext encrypts into different ciphertexts.

Applying Security to Password Encryption in the App

The encryptAESCryptoJS Function

encryptAESCryptoJS
  • Functionality:

    • Encrypts a plaintext string (plainText) using a passphrase.

  • How It Works:

    1. Generate a Random Salt:

      • Uses _genRandomWithNonZero(8) to create an 8-byte random salt.

      • The salt ensures randomness and security in the encryption process.

    2. Derive Key and IV:

      • _deriveKeyAndIV(passphrase, salt) generates a key and IV from the passphrase and salt.

      • Uses multiple MD5 hash iterations to generate enough data for both the key and IV.

    3. Encrypt the Data:

      • Uses the key and IV to encrypt plainText with the AES algorithm in CBC mode.

      • Produces an encrypted byte sequence.

    4. Prepare the Final Encrypted Data:

      • Concatenates "Salted__" + salt + encrypted bytes.

      • Encodes the entire sequence in base64 for easy storage or transmission.

  • Why Does the Encrypted Output Differ Each Time?

    • Since a new random salt is generated every time, even if the same plainText and passphrase are used, the encrypted result will always be different.

    • This enhances security and prevents attacks based on comparing encrypted outputs.

decryptAESCryptoJS Function

decryptAESCryptoJS
  • Functionality:

    • Decrypts a string that was encrypted using encryptAESCryptoJS.

  • How It Works:

    1. Decode Base64:

      • Converts the encrypted string from base64 format back to its original byte form.

    2. Extract Salt and Encrypted Data:

      • Discards the first 8 bytes ("Salted__").

      • Extracts the next 8 bytes as the salt.

      • Retrieves the remaining bytes as the encrypted data.

    3. Derive Key and IV Again:

      • Uses the _deriveKeyAndIV function with the extracted salt and passphrase to regenerate the encryption key and IV.

    4. Decrypt the Data:

      • Uses the key and IV to decrypt the encrypted data using AES in CBC mode.

      • The result is the original plainText.

verify and verifyEncrypted Functions

  • Purpose:

    • Ensures the integrity and correctness of the encryption and decryption process.

  • How It Works:

    • verify: Compares the original plainText with the decrypted result of an encrypted string.

    • verifyEncrypted: Decrypts two encrypted strings and compares their results.

How to Use the Code in a Flutter Project

Install Dependencies

  • Add the following to pubspec.yaml:

  • Run the command:

Using Encryption and Decryption Functions

  • Combine the previous code snippets into a MyEncrypt class.

  • Encrypt Data:

  • Decrypt Data:

  • Verify the Result:

Notes on Usage

  • Protecting the passphrase:

    • Do not store the passphrase as plain text in the source code or database.

    • Use secure methods such as environment variables or secret management services.

  • Managing Keys and Sensitive Data:

    • Restrict access to encryption-related code.

    • Follow data security regulations and best practices.

Why Are Salt and IV Important?

  • Salt:

    • Prevents dictionary and rainbow table attacks.

    • Ensures unique encryption results for the same plaintext.

    • Without Salt, attackers can compare ciphertexts to detect patterns.

    • This is particularly dangerous if multiple users share the same passphrase or sensitive data.

    • Using Salt is like adding a unique spice to a dish, making each encryption process different.

    • This prevents attackers from guessing your "recipe."

  • IV (Initialization Vector):

    • Ensures security in CBC mode encryption.

    • Prevents repeating patterns in encrypted data.

Conclusion

  • Security is not optional; it's a mandatory requirement in app development.

  • Using AES encryption with Salt and IV effectively protects user data.

  • With this guide, you and your team can implement a secure encryption solution for your Flutter app.

  • If you want to skip the complex parts and use it right away, check out my package: https://pub.dev/packages/my_saltarrow-up-right.

References:

Buy Me a Coffeearrow-up-right | Support Me on Ko-fiarrow-up-right

Last updated