■ AES 암호화 도구를 만드는 방법을 보여준다.
▶ Cargo.toml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[package] name = "test_project" version = "0.1.0" edition = "2021" [dependencies] aes = "0.7.5" block-modes = "0.8.1" base64 = "0.13.0" sha2 = "0.9.8" getrandom = "0.2.3" |
▶ src/cipher.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
use aes; use block_modes; use block_modes::block_padding; use block_modes::BlockMode; use sha2; use sha2::Digest; type AesCbc = block_modes::Cbc<aes::Aes256, block_padding::Pkcs7>; const SALT : &str = "LFsMH#kL!IfY:dcEz9F/dvj17nUN"; pub fn encrypt(plain_text : &str, password : &str, initial_value_byte_vector : &Vec<u8>) -> String { let key_byte_vector : Vec<u8> = get_key_byte_vector(password); let cbc : block_modes::Cbc<aes::Aes256, block_padding::Pkcs7> = AesCbc::new_from_slices(&key_byte_vector, &initial_value_byte_vector).unwrap(); let encrypted_byte_vector : Vec<u8> = cbc.encrypt_vec(plain_text.as_bytes()); let encrypted_text : String = base64::encode(encrypted_byte_vector); return encrypted_text; } pub fn decrypt(encrypted_text : &str, password : &str, initial_value_byte_vector : &Vec<u8>) -> String { let key_byte_vector : Vec<u8> = get_key_byte_vector(password); let encrypted_byte_vector : Vec<u8> = base64::decode(encrypted_text).unwrap(); let cbc : block_modes::Cbc<aes::Aes256, block_padding::Pkcs7> = AesCbc::new_from_slices(&key_byte_vector, initial_value_byte_vector).unwrap(); let plain_byte_vector : Vec<u8> = cbc.decrypt_vec(&encrypted_byte_vector).unwrap(); return String::from_utf8(plain_byte_vector).unwrap(); } fn get_key_byte_vector(password : &str) -> Vec<u8> { let password_salt : String = format!("{}::{}", password, SALT); let mut sha256 : sha2::Sha256 = sha2::Sha256::new(); sha256.update(password_salt.as_bytes()); return sha256.finalize().to_vec(); } pub fn get_initial_value_byte_vector() -> Vec<u8> { let mut initial_value_byte_vector : Vec<u8> = vec! [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]; getrandom::getrandom(&mut initial_value_byte_vector).unwrap(); return initial_value_byte_vector; } |
▶ src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
use std::env; mod cipher; fn main() { let argument_vector : Vec<String> = env::args().collect(); if argument_vector.len() < 4 { print_usage_message(); return; } let method : String = String::from(argument_vector[1].trim()); let password : String = String::from(argument_vector[2].trim()); let source : String = String::from(argument_vector[3].trim()); let initial_value_byte_vector : Vec<u8> = cipher::get_initial_value_byte_vector(); let target : String = match &method[..] { "enc" => cipher::encrypt(&source, &password, &initial_value_byte_vector), "dec" => cipher::decrypt(&source, &password, &initial_value_byte_vector), _ => { print_usage_message(); return; } }; println!("{}", target); } fn print_usage_message() { println!("[USAGE] test_project (enc|dec) password source"); } |