exiftool-rs 0.5.0

Read, write, and edit metadata in 93 file formats — a pure Rust reimplementation of ExifTool 13.53 with 100% tag name parity (194/194 test files)
Documentation
//! Nikon MakerNotes decryption.
//!
//! Implements the exact same cipher as ExifTool's Nikon.pm Decrypt().
//! Custom byte-stream cipher using two 256-byte lookup tables.

/// Decrypt Nikon encrypted MakerNotes data in-place.
///
/// `data` — mutable slice of the encrypted data (modified in place)
/// `serial` — camera serial number (lower 8 bits used)
/// `shutter_count` — shutter count value (all 4 bytes XORed together)
/// `start` — byte offset where encryption begins (typically 4)
pub fn nikon_decrypt(data: &mut [u8], serial: u32, shutter_count: u32, start: usize) {
    if start >= data.len() {
        return;
    }

    // Derive key bytes from serial and shutter count
    let key: u8 = (shutter_count as u8)
        ^ ((shutter_count >> 8) as u8)
        ^ ((shutter_count >> 16) as u8)
        ^ ((shutter_count >> 24) as u8);

    let ci: u8 = XLAT[0][(serial & 0xFF) as usize];
    let cj0: u8 = XLAT[1][key as usize];
    let ck0: u8 = 0x60;

    // Calculate initial state for decryption starting at `start`
    // (skip the first `start` bytes of keystream)
    let _n = 0u32; // We always start from the DecryptStart position
    let mut cj = cj0;
    let mut ck = ck0;

    for byte in &mut data[start..] {
        cj = cj.wrapping_add(ci.wrapping_mul(ck));
        ck = ck.wrapping_add(1);
        *byte ^= cj;
    }
}

/// Nikon cipher lookup tables (from ExifTool Nikon.pm lines 13553-13584).
static XLAT: [[u8; 256]; 2] = [
    // xlat[0] — indexed by (serial & 0xFF)
    [
        0xc1, 0xbf, 0x6d, 0x0d, 0x59, 0xc5, 0x13, 0x9d, 0x83, 0x61, 0x6b, 0x4f, 0xc7, 0x7f, 0x3d,
        0x3d, 0x53, 0x59, 0xe3, 0xc7, 0xe9, 0x2f, 0x95, 0xa7, 0x95, 0x1f, 0xdf, 0x7f, 0x2b, 0x29,
        0xc7, 0x0d, 0xdf, 0x07, 0xef, 0x71, 0x89, 0x3d, 0x13, 0x3d, 0x3b, 0x13, 0xfb, 0x0d, 0x89,
        0xc1, 0x65, 0x1f, 0xb3, 0x0d, 0x6b, 0x29, 0xe3, 0xfb, 0xef, 0xa3, 0x6b, 0x47, 0x7f, 0x95,
        0x35, 0xa7, 0x47, 0x4f, 0xc7, 0xf1, 0x59, 0x95, 0x35, 0x11, 0x29, 0x61, 0xf1, 0x3d, 0xb3,
        0x2b, 0x0d, 0x43, 0x89, 0xc1, 0x9d, 0x9d, 0x89, 0x65, 0xf1, 0xe9, 0xdf, 0xbf, 0x3d, 0x7f,
        0x53, 0x97, 0xe5, 0xe9, 0x95, 0x17, 0x1d, 0x3d, 0x8b, 0xfb, 0xc7, 0xe3, 0x67, 0xa7, 0x07,
        0xf1, 0x71, 0xa7, 0x53, 0xb5, 0x29, 0x89, 0xe5, 0x2b, 0xa7, 0x17, 0x29, 0xe9, 0x4f, 0xc5,
        0x65, 0x6d, 0x6b, 0xef, 0x0d, 0x89, 0x49, 0x2f, 0xb3, 0x43, 0x53, 0x65, 0x1d, 0x49, 0xa3,
        0x13, 0x89, 0x59, 0xef, 0x6b, 0xef, 0x65, 0x1d, 0x0b, 0x59, 0x13, 0xe3, 0x4f, 0x9d, 0xb3,
        0x29, 0x43, 0x2b, 0x07, 0x1d, 0x95, 0x59, 0x59, 0x47, 0xfb, 0xe5, 0xe9, 0x61, 0x47, 0x2f,
        0x35, 0x7f, 0x17, 0x7f, 0xef, 0x7f, 0x95, 0x95, 0x71, 0xd3, 0xa3, 0x0b, 0x71, 0xa3, 0xad,
        0x0b, 0x3b, 0xb5, 0xfb, 0xa3, 0xbf, 0x4f, 0x83, 0x1d, 0xad, 0xe9, 0x2f, 0x71, 0x65, 0xa3,
        0xe5, 0x07, 0x35, 0x3d, 0x0d, 0xb5, 0xe9, 0xe5, 0x47, 0x3b, 0x9d, 0xef, 0x35, 0xa3, 0xbf,
        0xb3, 0xdf, 0x53, 0xd3, 0x97, 0x53, 0x49, 0x71, 0x07, 0x35, 0x61, 0x71, 0x2f, 0x43, 0x2f,
        0x11, 0xdf, 0x17, 0x97, 0xfb, 0x95, 0x3b, 0x7f, 0x6b, 0xd3, 0x25, 0xbf, 0xad, 0xc7, 0xc5,
        0xc5, 0xb5, 0x8b, 0xef, 0x2f, 0xd3, 0x07, 0x6b, 0x25, 0x49, 0x95, 0x25, 0x49, 0x6d, 0x71,
        0xc7,
    ],
    // xlat[1] — indexed by XOR of shutter count bytes
    [
        0xa7, 0xbc, 0xc9, 0xad, 0x91, 0xdf, 0x85, 0xe5, 0xd4, 0x78, 0xd5, 0x17, 0x46, 0x7c, 0x29,
        0x4c, 0x4d, 0x03, 0xe9, 0x25, 0x68, 0x11, 0x86, 0xb3, 0xbd, 0xf7, 0x6f, 0x61, 0x22, 0xa2,
        0x26, 0x34, 0x2a, 0xbe, 0x1e, 0x46, 0x14, 0x68, 0x9d, 0x44, 0x18, 0xc2, 0x40, 0xf4, 0x7e,
        0x5f, 0x1b, 0xad, 0x0b, 0x94, 0xb6, 0x67, 0xb4, 0x0b, 0xe1, 0xea, 0x95, 0x9c, 0x66, 0xdc,
        0xe7, 0x5d, 0x6c, 0x05, 0xda, 0xd5, 0xdf, 0x7a, 0xef, 0xf6, 0xdb, 0x1f, 0x82, 0x4c, 0xc0,
        0x68, 0x47, 0xa1, 0xbd, 0xee, 0x39, 0x50, 0x56, 0x4a, 0xdd, 0xdf, 0xa5, 0xf8, 0xc8, 0x58,
        0xc9, 0xb1, 0x2b, 0xe4, 0xce, 0xb0, 0xd5, 0xa0, 0x8d, 0x03, 0xa7, 0xd0, 0xc1, 0x02, 0xce,
        0xa8, 0xad, 0xba, 0xf5, 0x35, 0x3a, 0x18, 0xb5, 0x48, 0x23, 0x5a, 0xde, 0xd8, 0xef, 0xd6,
        0xad, 0xce, 0x08, 0x49, 0x2a, 0x36, 0x12, 0x67, 0x0a, 0xc4, 0x89, 0x63, 0xc4, 0x32, 0x3e,
        0xf4, 0x1e, 0x57, 0xb8, 0xbb, 0x60, 0xbd, 0x8d, 0x29, 0x05, 0x62, 0x4a, 0x71, 0xb5, 0xd3,
        0xb8, 0x4c, 0x5a, 0x3c, 0xe5, 0x9d, 0xb0, 0x57, 0x1e, 0x51, 0x28, 0xce, 0x26, 0x26, 0xda,
        0xeb, 0x61, 0x42, 0xb4, 0x47, 0x8a, 0x26, 0xaf, 0x6a, 0xa4, 0x25, 0xaf, 0xfe, 0x12, 0xb0,
        0x14, 0x25, 0x01, 0xb3, 0x5b, 0x7f, 0x16, 0x53, 0x10, 0xc9, 0xd0, 0x35, 0xe9, 0x27, 0x4c,
        0x08, 0x38, 0xc1, 0x87, 0xb4, 0xa6, 0x56, 0x75, 0x8a, 0x9e, 0x00, 0x44, 0xba, 0xda, 0xc5,
        0xa7, 0x1f, 0x7a, 0xf0, 0xdf, 0xba, 0x55, 0xae, 0xdd, 0x13, 0xf3, 0x2c, 0x6b, 0xce, 0xf6,
        0x18, 0x12, 0x23, 0x58, 0x1e, 0x02, 0x5a, 0xce, 0x97, 0x5d, 0x69, 0x68, 0xb0, 0xe9, 0x16,
        0xef, 0x60, 0x95, 0x87, 0x24, 0x45, 0x89, 0x19, 0xcf, 0xf0, 0xf4, 0xac, 0x36, 0x00, 0x24,
        0x2d,
    ],
];