mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-16 05:04:22 +02:00
add epk3 initial support , move epk common functions
This commit is contained in:
+49
-1
@@ -51,8 +51,56 @@ pub fn extract_epk(file: &File, output_folder: &str) -> Result<(), Box<dyn std::
|
||||
println!("EPK2 detected!\n");
|
||||
formats::epk2::extract_epk2(file, output_folder)?;
|
||||
} else if epk_version == Some("epk3".to_string()) {
|
||||
println!("EPK3 detected! Not supported yet.");
|
||||
println!("EPK3 detected!\n");
|
||||
formats::epk3::extract_epk3(file, output_folder)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//COMMON EPK FUNCTIONS
|
||||
pub fn find_key<'a>(key_array: &'a [(&'a str, &'a str)], data: &[u8], expected_magic: &[u8]) -> Result<Option<(&'a str, Vec<u8>)>, Box<dyn std::error::Error>> {
|
||||
for (key_hex, name) in key_array {
|
||||
let key_bytes = hex::decode(key_hex)?;
|
||||
let decrypted = match decrypt_aes_ecb_auto(&key_bytes, data) {
|
||||
Ok(d) => d,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
if decrypted.starts_with(expected_magic) {
|
||||
return Ok(Some((name, key_bytes)));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
use aes::Aes128;
|
||||
use aes::Aes256;
|
||||
use ecb::{Decryptor, cipher::{BlockDecryptMut, KeyInit, generic_array::GenericArray}};
|
||||
|
||||
type Aes128EcbDec = Decryptor<Aes128>;
|
||||
type Aes256EcbDec = Decryptor<Aes256>;
|
||||
|
||||
pub fn decrypt_aes_ecb_auto(key: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
let mut buffer = ciphertext.to_vec();
|
||||
|
||||
if key.len() == 32 {
|
||||
// aes256
|
||||
let key_array: [u8; 32] = key.try_into()?;
|
||||
let mut decryptor = Aes256EcbDec::new(&key_array.into());
|
||||
for chunk in buffer.chunks_exact_mut(16) {
|
||||
let block: &mut [u8; 16] = chunk.try_into()?;
|
||||
decryptor.decrypt_block_mut(GenericArray::from_mut_slice(block));
|
||||
}
|
||||
} else {
|
||||
// aes128
|
||||
let key_array: [u8; 16] = key.try_into()?;
|
||||
let mut decryptor = Aes128EcbDec::new(&key_array.into());
|
||||
for chunk in buffer.chunks_exact_mut(16) {
|
||||
let block: &mut [u8; 16] = chunk.try_into()?;
|
||||
decryptor.decrypt_block_mut(GenericArray::from_mut_slice(block));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(buffer)
|
||||
}
|
||||
Reference in New Issue
Block a user