Re: DES implementation questions (FinalPermutation code)

From: Brian Ross (
Date: 06/15/05

Date: Wed, 15 Jun 2005 10:14:12 -0400

"tum_" <> wrote in message
> >ie.. if I do:
>> InitialPermutation(Left, Right)
>> FinalPermutation(Left, Right)
> Have you tried:
> InitialPermutation(Left, Right)
> FinalPermutation(Right, Left),

Thanks, this seems to be a large factor in what is causing me confusion.
When I swap the two sides as you suggest I am able to reproduce the original
values before the InitialPermutation.

Now I am a bit confused where the LR swaps are and why they are there.

Here are some followup questions:

Is there an extra LR swap at the end of the 16th round in the code
implementation (compared with the standard which mentions this LR swap is
skipped for symmetry between encryption and decryption).

Is this just due to how the inner loop is implemented with 2 rounds per loop
each with a swap?

If the answers to the above two questions yes - is this the reason why the
FP code has the swap built in? to undo this extra swap?

If I am wrong above, then why is this extra swap being done in the FP code?

Lastly, I don't really understand why the main DES encrypt/decrypt functions
swap the left and right sides again after the FinalPermutation is performed
while the users buffer is being repopulated (see code below). To me this
seems like an extra swap compared to the one that was performed in the FP


void CBlockCipherDES::EncryptBlock(const UnsignedInteger8* const 
pInputBlock, UnsignedInteger8* const pOutputBlock) const throw()
    UnsignedInteger32 Left = Encode8To32BE(pInputBlock);
    UnsignedInteger32 Right = Encode8To32BE( pInputBlock + 4 );
    DoInitialPermutation(Left, Right);
    DoRounds(Left, Right, m_EncryptKeySchedule);
    DoFinalPermutation(Left, Right);
    Encode32To8BE(Right, pOutputBlock);
    Encode32To8BE( Left, pOutputBlock + 4 );