修改获取disk序列号的方法,改为使用逻辑卷的序列号。因为可能由于权限的原因获取不到磁盘的序列号

This commit is contained in:
Ekko.bao 2024-10-20 16:44:41 +08:00
parent db28e8525c
commit 9c2fbe847e

View File

@ -7,6 +7,8 @@ use block_modes::block_padding::Pkcs7;
use base64::{Engine as _, engine::general_purpose};
type Aes128Cbc = Cbc<Aes128, Pkcs7>;
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,