Salt
Password Security in Flutter Apps: AES Encryption with Salt and IV
Last updated
Password Security in Flutter Apps: AES Encryption with Salt and IV
Last updated
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.
encryptAESCryptoJS
FunctionFunctionality:
Encrypts a plaintext string (plainText
) using a passphrase.
How It Works:
Generate a Random Salt:
Uses _genRandomWithNonZero(8)
to create an 8-byte random salt
.
The salt ensures randomness and security in the encryption process.
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.
Encrypt the Data:
Uses the key
and IV
to encrypt plainText
with 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 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
FunctionFunctionality:
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 _deriveKeyAndIV
function with the extracted salt
and passphrase
to regenerate the encryption key and IV.
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
FunctionsPurpose:
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.
Add the following to pubspec.yaml
:
Run the command:
Combine the previous code snippets into a MyEncrypt
class.
Encrypt Data:
Decrypt Data:
Verify the Result:
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.
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.
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.
References:
If you want to skip the complex parts and use it right away, check out my package: .
|