"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"listenfd 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
ron = "0.5.1" # Rust object notation, to load configuration files.
itertools = "0.8.0"
url = "1.7.2"
-base64 = "0.10.1"
\ No newline at end of file
+base64 = "0.10.1"
+rand = "0.7"
\ No newline at end of file
-cargo run --release -- --encrypt $args[0]
\ No newline at end of file
+cargo run -- --encrypt $args[0]
\ No newline at end of file
+use openssl::{symm, sha::sha256};\r
+use rand::prelude::*;\r
\r
+/// Encrypt the given text with the given key. The key length must be 128 bits encoded in base64.\r
+/// Ouput format:\r
+/// Format "1" + base_64(<IV> + <hash(message)> + <aes(message)>)\r
+/// IV: 16 bytes randomized.\r
+/// Mode : CBC.\r
pub fn encrypt(key: &str, plain_text: &str) -> String {\r
- let key_as_bytes = base64::decode(key);\r
+ let key_as_bytes = base64::decode(key).expect("Unable to decode base64 encoded key");\r
+ assert!(key_as_bytes.len() == 16);\r
\r
+ let text_as_bytes = plain_text.as_bytes();\r
\r
+ let iv = rand::thread_rng().gen::<[u8; 16]>();\r
\r
- String::new()\r
+ let cipher_text =\r
+ symm::encrypt(symm::Cipher::aes_128_cbc(), &key_as_bytes, Some(&iv), text_as_bytes)\r
+ .expect("Unable to encrypt message");\r
+\r
+ let hash_text = sha256(&text_as_bytes);\r
+\r
+ let mut result: Vec<u8> = Vec::new();\r
+ result.extend(&iv);\r
+ result.extend(&hash_text);\r
+ result.extend(&cipher_text);\r
+\r
+ String::from("1") + &base64::encode(&result)\r
}\r
\r
-pub fn decrypt(key: &str, cypher_text: &str) -> Option<String> {\r
+pub fn decrypt(key: &str, cipher_text: &str) -> Option<String> {\r
+ if cipher_text.chars() != '1' {\r
+ return None;\r
+ }\r
\r
- println!("cypher: {}", cypher_text);\r
+ println!("cypher: {}", cipher_text);\r
Some(String::new())\r
}\r
fn print_usage() {
println!("Usage:");
- println!(" {} [--help] [--encrypt <message>]", get_exe_name());
+ println!(" {} [--help] [--encrypt <plain-text>|--decrypt <cipher-text>]", get_exe_name());
}
fn read_key() -> String {
- use url::percent_encoding::percent_decode;
let mut key = String::new();
File::open(consts::FILE_KEY)
.expect(&format!("Failed to open key file: {}", consts::FILE_KEY))
.expect(&format!("Failed to read key file: {}", consts::FILE_KEY));
String::from(
- percent_decode(key.as_bytes())
+ url::percent_encoding::percent_decode(key.as_bytes())
.decode_utf8()
.expect(&format!("Failed to decode key file: {}", consts::FILE_KEY))
)
} else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") {
match args.iter().nth(position_arg_encrypt + 1) {
Some(mess_to_encrypt) => {
+
let encrypted_mess = crypto::encrypt(&key, mess_to_encrypt);
- println!("Encrypted message: {}", encrypted_mess);
+ let encrypted_mess_encoded = url::percent_encoding::utf8_percent_encode(&encrypted_mess, url::percent_encoding::DEFAULT_ENCODE_SET).to_string();
+ println!("Encrypted message percent-encoded: {}", encrypted_mess_encoded);
+ }
+ None => print_usage()
+ }
+
+ return Ok(());
+ } else if let Some((position_arg_decrypt, _)) = args.iter().find_position(|arg| arg == &"--decrypt") {
+ match args.iter().nth(position_arg_decrypt + 1) {
+ Some(cipher_text) => {
+ let cipher_text_decoded = url::percent_encoding::percent_decode(cipher_text.as_bytes()).decode_utf8().expect("Unable to decode encoded cipher text");
+ let plain_text = crypto::decrypt(&key, &cipher_text_decoded).unwrap();
+ println!("Decrypted message: {}", plain_text);
}
None => print_usage()
}