Created
August 7, 2019 17:36
-
-
Save lovasoa/1bd799ca292a8bfe8f0a4c2cdd5b37de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn decrypt(mut input_buf: Vec<u8>) -> Result<Vec<u8>, InvalidEncryptedImage> { | |
if get_int(&input_buf, 0) != 0x0A_0A_0A_0A { return Ok(input_buf); } | |
let header_size = get_int(&input_buf, input_buf.len() as u32 - 4); | |
let encrypted_size_pos = header_size + 4; | |
let encrypted_pos = encrypted_size_pos + 4; | |
let encrypted_size = get_int(&input_buf, encrypted_size_pos); | |
let encrypted_end_pos = encrypted_pos + encrypted_size; | |
let footer_size = input_buf.len() as u32 - 4 - encrypted_end_pos; | |
let encrypted = input_buf.get(encrypted_pos as usize..(encrypted_pos + encrypted_size) as usize).unwrap(); | |
let mut encrypted_copy = Vec::from(encrypted); | |
let decrypted = aes_decrypt_buffer(&mut encrypted_copy)?; | |
let output_size = header_size + decrypted.len() as u32; | |
let final_size = header_size + output_size + footer_size; | |
let mut output_buf = Vec::with_capacity(final_size as usize); | |
output_buf.write_all(&input_buf[4..(4 + header_size) as usize])?; | |
output_buf.write_all(decrypted)?; | |
output_buf.write_all(&input_buf[encrypted_end_pos as usize..(encrypted_end_pos + footer_size) as usize])?; | |
Ok(output_buf) | |
} | |
fn get_int(buf: &[u8], pos: u32) -> u32 { | |
let i = pos as usize; | |
let mut bytes = [0u8; 4]; | |
bytes.copy_from_slice(&buf[i..i + 4]); | |
u32::from_le_bytes(bytes) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment