diff --git a/Cargo.toml b/Cargo.toml index 04177eb..c9f6731 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ description = "A simple and tiny file encrypt tool" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +aes-gcm = { version = "0.10.1", features = ["zeroize"] } clap = { version = "4.1.4", features = ["derive"] } hex = "0.4.3" rust_util = "0.6.41" diff --git a/src/crypto.rs b/src/crypto.rs new file mode 100644 index 0000000..7de2950 --- /dev/null +++ b/src/crypto.rs @@ -0,0 +1,46 @@ +use aes_gcm::{Aes256Gcm, AesGcm, Key, KeyInit, Nonce}; +use aes_gcm::aead::{AeadMut, OsRng}; +use aes_gcm::aead::generic_array::GenericArray; +use aes_gcm::aes::{Aes256, Aes256Enc}; +use aes_gcm::aes::cipher::{Block, BlockEncrypt, BlockEncryptMut, Iv}; +use aes_gcm::aes::cipher::inout::{InOut, InOutBuf}; + +#[test] +fn test_aes_gcm_01() { + let data_key = hex::decode("0001020304050607080910111213141516171819202122232425262728293031").unwrap(); + let nonce = hex::decode("000102030405060708091011").unwrap(); + + let plain_text1 = "Hello world!".as_bytes(); + let encrypted_text1 = "dce9511866417cff5123fa08c9e92cf156c5fc8bf6108ff28816fb58"; + + let plain_text2 = "This is a test message.".as_bytes(); + let encrypted_text2 = hex::decode("c0e45407290878b0426fea4c09597ce323b056f975c63cce6c8da516c2a78c7d71b590c869cf92").unwrap(); + + let key = Key::::from_slice(&data_key); + let nonce = Nonce::from_slice(&nonce); + { + let mut aes256_gcm = Aes256Gcm::new(&key); + let encrypted = aes256_gcm.encrypt(&nonce, plain_text1).unwrap(); + assert_eq!(encrypted_text1, hex::encode(&encrypted)); + } + // { + // let mut aes256_gcm = Aes256Gcm::new(&key); + // let mut arr = [0_u8; 16]; + // // let mut block = Block::::from(arr); + // // let mut x: [u8; 16] = [205, 129, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + // let block = GenericArray::from_mut_slice(&mut arr); + // aes256_gcm.encrypt_block_mut(&mut block); + // } +} + +#[test] +fn test_aes_gcm_02() { + let data_key = hex::decode("aa01020304050607080910111213141516171819202122232425262728293031").unwrap(); + let nonce = hex::decode("aa0102030405060708091011").unwrap(); + + let plain_text1 = "Hello world!".as_bytes(); + let encrypted_text1 = hex::decode("42b625d2bacb8a514076f14002f02770e9ccd98c90e556dc267aca30").unwrap(); + + let plain_text2 = "This is a test message.".as_bytes(); + let encrypted_text2 = hex::decode("5ebb20cdf5828e1e533ae1043ce6703cfa51574a83a069700aedefdbe2c735b01b74da214cba4a").unwrap(); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 693f318..6f8c524 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use clap::{Parser, Subcommand}; use rust_util::information; mod spec; +mod crypto; #[derive(Debug, Parser)] #[command(name = "tiny-encrypt-rs")] diff --git a/src/spec.rs b/src/spec.rs index 0c9ef14..41ea6db 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -29,6 +29,7 @@ pub struct DescBlock { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct KeyBlock { + pub key_type: String, pub key_id: Option, pub encrypted_key_hex: String, pub key_verification_code_hex: String,