pub trait Decryptable:
ReadBytes
+ Read
+ Seek {
// Provided methods
fn decrypt(&mut self) -> Result<Vec<u8>> { ... }
fn decrypt_u32(&mut self, second_key: u32) -> Result<u32> { ... }
fn decrypt_string(&mut self, second_key: u8) -> Result<String> { ... }
}Expand description
Trait for decrypting encrypted PackFile data.
This trait provides methods to decrypt different parts of encrypted PackFiles using Total War’s custom encryption scheme. The decryption uses three different keys for different types of data (file data, file sizes, and file paths).
§Implementation
This trait is automatically implemented for any type that implements
ReadBytes + Read + Seek.
Provided Methods§
Sourcefn decrypt(&mut self) -> Result<Vec<u8>>
fn decrypt(&mut self) -> Result<Vec<u8>>
Decrypts the data of an encrypted PackedFile.
This function decrypts data in 8-byte chunks using the DATA_KEY. The file is first padded to a multiple of 8 bytes if needed, then decrypted chunk by chunk. Note that the last chunk is NOT encrypted and is copied as-is.
§Returns
A Vec<u8> containing the decrypted data, or an error if decryption fails.
§Examples
use std::io::Cursor;
use rpfm_lib::encryption::Decryptable;
let encrypted_data = vec![/* encrypted bytes */];
let mut cursor = Cursor::new(encrypted_data);
let decrypted = cursor.decrypt()?;Sourcefn decrypt_u32(&mut self, second_key: u32) -> Result<u32>
fn decrypt_u32(&mut self, second_key: u32) -> Result<u32>
Decrypts the size of a PackedFile from the encrypted index.
This function reads and decrypts a u32 value representing the size of a PackedFile.
The decryption uses both the INDEX_U32_KEY and a second key derived from the position
in the index.
§Arguments
second_key- The secondary key, typically the number of PackedFiles after this one in the index.
§Returns
The decrypted file size as a u32, or an error if reading fails.
§Examples
use std::io::Cursor;
use rpfm_lib::encryption::Decryptable;
let encrypted_index = vec![/* encrypted index bytes */];
let mut cursor = Cursor::new(encrypted_index);
let packed_files_remaining = 5;
let size = cursor.decrypt_u32(packed_files_remaining)?;Sourcefn decrypt_string(&mut self, second_key: u8) -> Result<String>
fn decrypt_string(&mut self, second_key: u8) -> Result<String>
Decrypts the path of a PackedFile from the encrypted index.
This function reads and decrypts a null-terminated string representing the path of a PackedFile. The decryption uses the INDEX_STRING_KEY (a 64-byte rotating key) and a second key derived from the file’s properties.
§Arguments
second_key- The secondary key, typically derived from the file’s decrypted size.
§Returns
The decrypted file path as a String, or an error if reading fails.
§Implementation Note
This function reads the encrypted string byte-by-byte until a null terminator (0x00) is found. Each byte is XORed with the corresponding byte from INDEX_STRING_KEY (rotating through the 64-byte key) and the inverted second_key.
§Examples
use std::io::Cursor;
use rpfm_lib::encryption::Decryptable;
let encrypted_index = vec![/* encrypted index bytes */];
let mut cursor = Cursor::new(encrypted_index);
let file_size = 1024u8;
let path = cursor.decrypt_string(file_size)?;