Salt
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 Function
Functionality:
Encrypts a plaintext string (
plainText) using a passphrase.
How It Works:
Generate a Random Salt:
Uses
_genRandomWithNonZero(8)to create an 8-byte randomsalt.The salt ensures randomness and security in the encryption process.
Derive Key and IV:
_deriveKeyAndIV(passphrase, salt)generates akeyandIVfrom the passphrase and salt.Uses multiple MD5 hash iterations to generate enough data for both the key and IV.
Encrypt the Data:
Uses the
keyandIVto encryptplainTextwith the AES algorithm in CBC mode.Produces an encrypted byte sequence.
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
saltis generated every time, even if the sameplainTextandpassphraseare used, the encrypted result will always be different.This enhances security and prevents attacks based on comparing encrypted outputs.
decryptAESCryptoJS Function
decryptAESCryptoJS Function
Functionality:
Decrypts a string that was encrypted using
encryptAESCryptoJS.
How It Works:
Decode Base64:
Converts the encrypted string from base64 format back to its original byte form.
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.
Derive Key and IV Again:
Uses the
_deriveKeyAndIVfunction with the extractedsaltandpassphraseto regenerate the encryption key and IV.
Decrypt the Data:
Uses the
keyandIVto decrypt the encrypted data using AES in CBC mode.The result is the original
plainText.
verify and verifyEncrypted Functions
verify and verifyEncrypted FunctionsPurpose:
Ensures the integrity and correctness of the encryption and decryption process.
How It Works:
verify: Compares the originalplainTextwith 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
MyEncryptclass.
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_salt.
References:
Last updated
