Following D.W.'s answer, here's an actual proof that $\text{wt}(X \oplus K \oplus Y \oplus K') \geqslant \text{wt}(X \oplus Y)$.
We'll assume that the plaintext (resp. the key)'s characters have been generated with an alphabet $A$ (resp. $A'$) and a probability distribution over this alphabet $D_A$ (resp. $D_{A'}$).
(e.g. lower case letters and english letter distribution).
This allows us to write the expected normalized Hamming distance as:
$H_R = E[\text{wt}(X_1 \oplus X_2)]$ if the key length is correctly guessed.
$H_W = E[\text{wt}(X_1 \oplus X_2 \oplus X'_1 \oplus X'_2)]$ otherwise.
where $X_i$ (resp. $X'_i$) are independent random variables with distribution $D_A$ (resp. $D_{A'}$).
Now, let's zoom in on bits.
Fact 1:
The probability $p_k$ that the kth bit $b_{i,k}$ of random variable $X_i$ is set to 1 is the probability of drawing a character from $D_A$ whose kth bit is 1, so the sum of the probability of all such characters.
(e.g. letters [q-z] have the 5th bit set to 1, so $p_5$ is $10/26$ for a uniform distribution).
Fact 2:
The XOR of n bits will have value 1 if bit 1 appears an odd number of times, and value 0 otherwise.
From these 2 facts, we can compute the expected Hamming distance for the kth bit:
- when we XOR $X_1$ and $X_2$:
$$h_{R,k} = h_{2,k} = E[\text{wt}(b_{1,k} \oplus b_{2,k})] = E[b_{1,k} \oplus b_{2,k}] = p(\text{1 bit set}) = 2p_k(1-p_k)$$
- similarly, when we XOR $X'_1$ and $X'_2$:
$$h'_{2,k} = E[b'_{1,k} \oplus b'_{2,k}] = 2p'_k(1-p'_k)$$
- when we XOR $X_1$, $X_2$, $X'_1$ and $X'_2$, noticing that to have an odd number of bits set to 1, you must have (an odd number of 1 in the first 2 bits AND an even number in the last 2 bits) OR (an even number in the first 2 bits AND an odd number in the last 2 bits):
$$h_{W,k} = E[b_{1,k} \oplus b_{2,k} \oplus b'_{1,k} \oplus b'_{2,k}] = h_{2,k}(1-h'_{2,k}) + h'_{2,k}(1-h_{2,k}) = h_{2,k} + h'_{2,k}(1-2h_{2,k})$$
If you plot $h_{2,k}$, you can see that it does not exceed 0.5, so $(1-2h_{2,k})$ is positive, and thus $h_{W,k} \geqslant h_{R,k}$.
Since the expected normalized Hamming distance $H_R$ (resp. $H_W$) is just the sum of the expected distances $h_{R,k}$ (resp. $h_{W,k}$) for each bit, we have proven why it is lower when the key length is guessed correctly =)
Note 1. You can now compute the expected Hamming distance, when key length is correctly guessed or not, for any ($A$, $D_A$) and ($A'$, $D_{A'}$).
For example:
- if plaintext and key are random lower case letters, $H_R \approx 2.47 bits$ and $H_W \approx 2.50 bits$.
- if we use english letter frequency instead, $H_R \approx 2.36 bits$ and $H_W \approx 2.49 bits$.
- if we add in spaces (that could be useful) with ~19% frequency, $H_R \approx 2.54 bits$ and $H_W \approx 2.88 bits$.
Note 2. $h_{W,k}$ cannot exceed 0.5 either, so if $h_{R,k}$ (i.e. $p_k$) is close to 0.5 for all bits, key length detection won't work well. And the good thing is that "$p_k$ is close to 0.5 for all bits" does NOT mean there is no statistical information in the text. For a given ($A$, $D_A$), one can maybe devise a set of distinct bytes for each charcater such that $p_k$ is close to 0.5 for every k, in order to make key length guessing more difficult =)