mtk_pkg: fix decryption of unaligned data

This commit is contained in:
theubusu
2026-01-06 18:08:17 +01:00
parent 37eef68793
commit 24839ea9ab
2 changed files with 34 additions and 19 deletions
+9 -2
View File
@@ -117,10 +117,17 @@ pub fn extract_mtk_pkg_new(mut file: &File, output_folder: &str) -> Result<(), B
continue
}
let out_data;
let mut out_data;
if part_entry.is_encrypted() {
println!("- Decrypting...");
out_data = decrypt_aes128_cbc_nopad(&data[..data.len() & !15], &matching_key.unwrap(), &matching_iv.unwrap())?;
let (key_array, iv_array) = (matching_key.unwrap(), matching_iv.unwrap());
//data aligned to 16 bytes is AES encrypted. the remaining unaligned data is XORed with the key
let align_len = data.len() & !15;
let (aes_enc, xor_tail) = data.split_at(align_len);
out_data = decrypt_aes128_cbc_nopad(aes_enc, &key_array, &iv_array)?;
for (i, &b) in xor_tail.iter().enumerate() {
out_data.push(b ^ key_array[i % key_array.len()]);
}
} else {
out_data = data;
}