From 173e7486900b4fa0daf9653d55ad8ad2d461b8b8 Mon Sep 17 00:00:00 2001 From: zakihussain <85828740+zakihussain@users.noreply.github.com> Date: Tue, 18 Nov 2025 10:13:22 +0500 Subject: [PATCH] Clear round keys before generating subkeys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, each round's subkey could have leftover bits from the previous round because we didn’t clear key_sets[i].k. This could cause wrong keys during encryption. Now, we set all 8 bytes of the round key to 0 before generating it, which fixes a potential problem and makes subkey generation correct. --- des.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/des.c b/des.c index ada929c..04285c8 100644 --- a/des.c +++ b/des.c @@ -150,8 +150,11 @@ void generate_sub_keys(unsigned char* main_key, key_set* key_sets) { int shift_size; unsigned char shift_byte, first_shift_bits, second_shift_bits, third_shift_bits, fourth_shift_bits; - for (i=0; i<8; i++) { - key_sets[0].k[i] = 0; + for (i = 1; i < 17; i++) { + // Initialize the round subkey to 0 before filling it + for (int x = 0; x < 8; x++) { + key_sets[i].k[x] = 0; + } } for (i=0; i<56; i++) {