From 6a56cc8433e9b28fe7e6dc44a3e721d15a600d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Sun, 15 Sep 2024 15:41:19 +0100 Subject: [PATCH] [qmc2/rc4] fix: match QMC2 decoder logic --- um_crypto/qmc/src/v2_rc4/cipher.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/um_crypto/qmc/src/v2_rc4/cipher.rs b/um_crypto/qmc/src/v2_rc4/cipher.rs index ad63fba..602d410 100644 --- a/um_crypto/qmc/src/v2_rc4/cipher.rs +++ b/um_crypto/qmc/src/v2_rc4/cipher.rs @@ -31,8 +31,9 @@ impl QMC2RC4 { let n = self.key.len(); for (datum, offset) in data.iter_mut().zip(offset..) { - let idx = get_segment_key(offset as u64, self.key[offset % n], self.hash) as usize; - *datum ^= self.key[idx % n]; + let idx = get_segment_key(offset as u64, self.key[offset % n], self.hash); + let idx = idx % (n as u64); + *datum ^= self.key[idx as usize]; } } @@ -43,10 +44,11 @@ impl QMC2RC4 { let block_offset = offset % OTHER_SEGMENT_SIZE; let seed = self.key[id % n]; - let skip = get_segment_key(id as u64, seed, self.hash) % 512; + let skip = get_segment_key(id as u64, seed, self.hash); + let skip = (skip & 0x1FF) as usize; debug_assert!(data.len() <= OTHER_SEGMENT_SIZE - block_offset); - let key_stream = self.key_stream.iter().skip(skip as usize + block_offset); + let key_stream = self.key_stream.iter().skip(skip + block_offset); for (datum, &key) in data.iter_mut().zip(key_stream) { *datum ^= key; }