|
| 1 | +use std::env; |
| 2 | +use std::fs::{self, File}; |
| 3 | +use std::io::{Write}; |
| 4 | +use aes::{Aes128, NewBlockCipher}; // Importer NewBlockCipher |
| 5 | +use block_modes::{BlockMode, Cbc}; |
| 6 | +use block_modes::block_padding::Pkcs7; |
| 7 | +use generic_array::{GenericArray, typenum::U16}; // Importer GenericArray et typenum |
| 8 | + |
| 9 | +type Aes128Cbc = Cbc<Aes128, Pkcs7>; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let args: Vec<String> = env::args().collect(); |
| 13 | + if args.len() < 5 { |
| 14 | + eprintln!("Usage: cargo run -- --crypt --file [file] --password [password]"); |
| 15 | + eprintln!("Or: cargo run -- --decrypt --file [file] --redrose [key_file]"); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + let mode = &args[1]; |
| 20 | + let file_path = &args[3]; |
| 21 | + let password = &args[4]; |
| 22 | + |
| 23 | + match mode.as_str() { |
| 24 | + "--crypt" => encrypt_file(file_path, password), |
| 25 | + "--decrypt" => decrypt_file(file_path, &args[5]), |
| 26 | + _ => eprintln!("Invalid mode. Use --crypt or --decrypt."), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +fn encrypt_file(file_path: &str, password: &str) { |
| 31 | + let file_content = fs::read(file_path).expect("Unable to read file"); |
| 32 | + let key = generate_key(password); |
| 33 | + |
| 34 | + // Chiffrement du contenu avec AES |
| 35 | + let iv = GenericArray::<u8, U16>::from_slice(&key[0..16]); // Vecteur d'initialisation (IV) |
| 36 | + |
| 37 | + // Créer un GenericArray pour la clé |
| 38 | + let key_array = GenericArray::from_slice(&key); // Convertir la clé en GenericArray |
| 39 | + |
| 40 | + let cipher = Aes128Cbc::new(Aes128::new(&key_array), iv); |
| 41 | + |
| 42 | + let encrypted_data = cipher.encrypt_vec(&file_content); |
| 43 | + |
| 44 | + // Écrire le fichier chiffré |
| 45 | + let encrypted_file_path = format!("{}.encrypted", file_path); |
| 46 | + let mut output_file = File::create(&encrypted_file_path).unwrap(); |
| 47 | + output_file.write_all(&encrypted_data).unwrap(); |
| 48 | + |
| 49 | + // Écrire la clé dans un fichier .redrose |
| 50 | + let key_file_path = format!("{}.redrose", file_path); |
| 51 | + let mut key_file = File::create(&key_file_path).unwrap(); |
| 52 | + key_file.write_all(&key).unwrap(); |
| 53 | + |
| 54 | + println!("File encrypted as {} and key saved as {}", encrypted_file_path, key_file_path); |
| 55 | +} |
| 56 | + |
| 57 | +fn decrypt_file(encrypted_file_path: &str, key_file_path: &str) { |
| 58 | + let encrypted_data = fs::read(encrypted_file_path).expect("Unable to read encrypted file"); |
| 59 | + |
| 60 | + // Lire la clé depuis le fichier .redrose |
| 61 | + let key = fs::read(key_file_path).expect("Unable to read key file"); |
| 62 | + |
| 63 | + // Déchiffrement du contenu chiffré avec AES |
| 64 | + let iv = GenericArray::<u8, U16>::from_slice(&key[0..16]); // Vecteur d'initialisation (IV) |
| 65 | + |
| 66 | + // Créer un GenericArray pour la clé |
| 67 | + let key_array = GenericArray::from_slice(&key); // Convertir la clé en GenericArray |
| 68 | + |
| 69 | + let cipher = Aes128Cbc::new(Aes128::new(&key_array), iv); |
| 70 | + |
| 71 | + let decrypted_data = cipher.decrypt_vec(&encrypted_data).expect("Decryption failed"); |
| 72 | + |
| 73 | + // Écrire le fichier reconstruit |
| 74 | + let reconstructed_file_path = format!("{}.reconstructed", encrypted_file_path); |
| 75 | + let mut output_file = File::create(&reconstructed_file_path).unwrap(); |
| 76 | + output_file.write_all(&decrypted_data).unwrap(); |
| 77 | + |
| 78 | + println!("File decrypted and saved as {}", reconstructed_file_path); |
| 79 | +} |
| 80 | + |
| 81 | +fn generate_key(password: &str) -> Vec<u8> { |
| 82 | + // Générer une clé basée sur le mot de passe (simple hash pour cet exemple) |
| 83 | + let mut key = vec![0u8; 16]; |
| 84 | + for (i, byte) in password.bytes().enumerate() { |
| 85 | + key[i % 16] ^= byte; // Simple XOR pour la démonstration |
| 86 | + } |
| 87 | + key |
| 88 | +} |
0 commit comments