From 9c2fbe847e07ecb53b931fe3fa5daf4c135c4e0e Mon Sep 17 00:00:00 2001 From: Begild Date: Sun, 20 Oct 2024 16:44:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96disk?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8F=B7=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8=E9=80=BB=E8=BE=91=E5=8D=B7?= =?UTF-8?q?=E7=9A=84=E5=BA=8F=E5=88=97=E5=8F=B7=E3=80=82=E5=9B=A0=E4=B8=BA?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=94=B1=E4=BA=8E=E6=9D=83=E9=99=90=E7=9A=84?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0=E7=A3=81?= =?UTF-8?q?=E7=9B=98=E7=9A=84=E5=BA=8F=E5=88=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/encrypt/mod.rs | 55 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/encrypt/mod.rs b/src/encrypt/mod.rs index 39ff40b..672aab4 100755 --- a/src/encrypt/mod.rs +++ b/src/encrypt/mod.rs @@ -7,6 +7,8 @@ use block_modes::block_padding::Pkcs7; use base64::{Engine as _, engine::general_purpose}; type Aes128Cbc = Cbc; +use std::ffi::CString; +use winapi::um::fileapi::GetVolumeInformationA; use std::fmt; #[derive(Debug)] @@ -24,22 +26,49 @@ impl fmt::Display for EncryptErr { impl std::error::Error for EncryptErr {} +// pub fn get_key() -> String { +// let output = Command::new("wmic") +// .args(&["DISKDRIVE", "get", "serialnumber"]) +// .output() +// .expect("Failed to execute command"); + +// let serial = String::from_utf8_lossy(&output.stdout) +// .lines() +// .nth(1) // 获取第二行,通常是序列号 +// .unwrap() +// .trim() +// .to_string(); +// log::trace!("hdd serial is {}", serial); +// serial +// } pub fn get_key() -> String { - let output = Command::new("wmic") - .args(&["DISKDRIVE", "get", "serialnumber"]) - .output() - .expect("Failed to execute command"); + let drive: &str = "C:\\"; + let mut volume_name: [i8; 100] = [0; 100]; + let mut file_system_name: [i8; 100] = [0; 100]; + let mut volume_serial_number: u32 = 0; + let mut max_component_length: u32 = 0; + let mut file_system_flags: u32 = 0; + let tstr = CString::new(drive).unwrap(); + let success = unsafe { + GetVolumeInformationA( + tstr.as_ptr(), + volume_name.as_mut_ptr(), + volume_name.len() as u32, + &mut volume_serial_number, + &mut max_component_length, + &mut file_system_flags, + file_system_name.as_mut_ptr(), + file_system_name.len() as u32, + ) + }; - let serial = String::from_utf8_lossy(&output.stdout) - .lines() - .nth(1) // 获取第二行,通常是序列号 - .unwrap() - .trim() - .to_string(); - log::trace!("hdd serial is {}", serial); - serial + if success != 0 { + return volume_serial_number.to_string() + } else { + log::warn!("Failed to get volume information."); + return "".to_string(); + } } - pub fn is_base64(encrypted_data: &str) -> bool { match general_purpose::STANDARD.decode(encrypted_data) { Ok(_) => true,