113 lines
3.5 KiB
Dart
113 lines
3.5 KiB
Dart
// export_privateKey.dart
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'dart:typed_data';
|
|
import 'dart:convert';
|
|
import 'package:pointycastle/export.dart' as crypto;
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
class ExportationClePriveePage extends StatefulWidget {
|
|
const ExportationClePriveePage({super.key});
|
|
|
|
@override
|
|
_ExportationClePriveePageState createState() => _ExportationClePriveePageState();
|
|
}
|
|
|
|
class _ExportationClePriveePageState extends State<ExportationClePriveePage> {
|
|
final TextEditingController _passwordController = TextEditingController();
|
|
|
|
Future<void> _exportPrivateKey() async {
|
|
// Replace with your actual private key retrieval logic
|
|
final String privateKeyPem = 'Votre clé privée ici';
|
|
|
|
// Get the password from the user input
|
|
final password = _passwordController.text;
|
|
if (password.isEmpty) {
|
|
// Show error message
|
|
return;
|
|
}
|
|
|
|
// Encrypt the private key using AES-256
|
|
final encryptedData = _encryptPrivateKey(privateKeyPem, password);
|
|
|
|
// Let the user pick a file location
|
|
final outputFile = await FilePicker.platform.saveFile(
|
|
dialogTitle: 'Enregistrer la clé privée chiffrée',
|
|
fileName: 'private_key_encrypted.aes',
|
|
);
|
|
|
|
if (outputFile != null) {
|
|
// Write the encrypted data to the file
|
|
// Use appropriate file I/O methods (not shown here)
|
|
// ...
|
|
// Show a confirmation dialog or message
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Clé Exportée'),
|
|
content: const Text('La clé privée chiffrée a été exportée avec succès.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
Uint8List _encryptPrivateKey(String privateKey, String password) {
|
|
// Encryption logic using AES-256
|
|
final key = crypto.PBKDF2KeyDerivator(crypto.HMac(crypto.SHA256Digest(), 64))
|
|
.process(Uint8List.fromList(utf8.encode(password)));
|
|
|
|
final params = crypto.PaddedBlockCipherParameters(
|
|
crypto.ParametersWithIV(crypto.KeyParameter(key), Uint8List(16)), // Initialization Vector
|
|
null,
|
|
);
|
|
final cipher = crypto.PaddedBlockCipher('AES/CBC/PKCS7');
|
|
cipher.init(true, params);
|
|
|
|
final input = Uint8List.fromList(utf8.encode(privateKey));
|
|
final output = cipher.process(input);
|
|
|
|
return output;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
appBar: AppBar(
|
|
title: const Text('Exportation de la Clé Privée'),
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
const Text(
|
|
'Entrez un mot de passe pour chiffrer la clé privée:',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
TextField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
style: const TextStyle(color: Colors.white),
|
|
decoration: const InputDecoration(
|
|
hintText: 'Mot de passe',
|
|
hintStyle: TextStyle(color: Colors.grey),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
ElevatedButton(
|
|
onPressed: _exportPrivateKey,
|
|
child: const Text('Exporter la Clé Privée Chiffrée'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|