Secure data encryption and decryption using biometric authentication for Flutter (Android & iOS).
- π Biometric Encryption - Encrypt data using biometric authentication
- π Secure Decryption - Decrypt data only with valid biometric authentication
- π± Cross-Platform - Works seamlessly on both Android and iOS
- π‘οΈ Hardware Security - Leverages device secure hardware
- π― Simple API - Easy to use encryption/decryption interface
- β‘ Fast & Efficient - Native implementation for optimal performance
Add to your pubspec.yaml:
dependencies:
local_auth_crypto: ^1.1.1Then run:
flutter pub getimport 'package:local_auth_crypto/local_auth_crypto.dart';final localAuthCrypto = LocalAuthCrypto.instance;final message = 'SECRET_TOKEN';
final cipherText = await localAuthCrypto.encrypt(message);final promptInfo = BiometricPromptInfo(
title: 'BIOMETRIC',
subtitle: 'Please scan biometric to decrypt',
negativeButton: 'CANCEL',
);
final plainText = await localAuthCrypto.authenticate(promptInfo, cipherText);- Update
MainActivity.kt:
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity : FlutterFragmentActivity()- Add permissions to
AndroidManifest.xml:
<uses-permission android:name="android.permission.USE_BIOMETRIC" />Add to your Info.plist:
<dict>
<key>NSFaceIDUsageDescription</key>
<string>This application wants to access your TouchID or FaceID</string>
</dict>Future<CipherText> encrypt(String message)Encrypts a string message and returns encrypted cipher text.
Future<String> authenticate(BiometricPromptInfo promptInfo, CipherText cipherText)Authenticates user with biometrics and decrypts the cipher text.
Configure the biometric prompt:
BiometricPromptInfo({
required String title,
required String subtitle,
required String negativeButton,
String? description,
bool confirmationRequired = true,
})import 'package:local_auth_crypto/local_auth_crypto.dart';
class SecureDataManager {
final localAuthCrypto = LocalAuthCrypto.instance;
Future<void> saveSecureData(String sensitiveData) async {
try {
// Encrypt sensitive data
final cipherText = await localAuthCrypto.encrypt(sensitiveData);
// Save encrypted data to storage
await saveToSecureStorage(cipherText);
print('Data encrypted and saved successfully');
} catch (e) {
print('Encryption failed: $e');
}
}
Future<String?> retrieveSecureData() async {
try {
// Retrieve encrypted data from storage
final cipherText = await getFromSecureStorage();
// Create prompt for biometric authentication
final promptInfo = BiometricPromptInfo(
title: 'Authentication Required',
subtitle: 'Please authenticate to access secure data',
negativeButton: 'Cancel',
description: 'Your biometric data is required to decrypt sensitive information',
);
// Authenticate and decrypt
final decryptedData = await localAuthCrypto.authenticate(
promptInfo,
cipherText,
);
return decryptedData;
} catch (e) {
print('Decryption failed: $e');
return null;
}
}
}Handle common scenarios:
try {
final result = await localAuthCrypto.authenticate(promptInfo, cipherText);
// Handle successful authentication
} on PlatformException catch (e) {
switch (e.code) {
case 'auth_failed':
print('Authentication failed');
break;
case 'auth_canceled':
print('Authentication canceled by user');
break;
case 'not_available':
print('Biometric authentication not available');
break;
case 'not_enrolled':
print('No biometric data enrolled');
break;
default:
print('Unknown error: ${e.message}');
}
}- Sensitive Data Only: Use this package only for sensitive data that requires biometric protection
- Error Handling: Always implement proper error handling for authentication failures
- Fallback Options: Provide alternative authentication methods when biometrics are unavailable
- Data Validation: Validate decrypted data to ensure integrity
- Key Management: Never store encryption keys in plain text
| Platform | Supported Biometrics |
|---|---|
| Android | Fingerprint, Face recognition, Iris |
| iOS | Touch ID, Face ID |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this package helpful, please consider supporting it:

