DES Key Parity


A single DES key is 64 bits (8 bytes) long, however the actual key material used by the DES algorithm amounts to only 56 bits in length. The least significant bit of each byte is a parity bit, and should be set such that there is always an odd number of bits set (1’s) in each key byte. Only the 7 most significant bits of each byte are effective for security purposes.

Hence, DES has an effective key strength of 56 bits and TripleDES variants EDE2 and EDE3 have effective key strengths of 112 bits and 168 bits respectively.

The parity of a single DES key can be checked and adjusted using the following function:-

bool AdjustDESKeyParity(UCHAR* pucKey, int nKeyLen)
{
   int cPar;
   for(int i = 0; i < nKeyLen; i++)
   {
      cPar = 0;
      for(int j = 0; j < DES::BLOCKSIZE; j++)
      {
         if(pucKey[i] & (0x01 << j))
            cPar = !cPar;
      }
      if(!cPar)
         pucKey[i] ^= 0x01;
   }

   return true;

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

4 thoughts on “DES Key Parity

  1. While it is true that the 8th bit determines the parity, your code loops 8*8 or 64 times for the deskey and 192 times for the 3des key.

    Here is some thing to think about. We will build a table of the bit counts per nibble.
    The table has 16 entriesl

    nibble values/counts
    0000 0 0100 1 1000 1 1100 3
    0001 1 0101 2 1001 2 1101 3
    0010 1 0110 2 1010 2 1110 3
    0011 2 0111 3 1011 3 1111 4

    static UCHAR partable[16]={0,1,1,2, 1,2,2,3, 1,2,2,3, 3,3,3,4};
    char * disparity(unsigned char string, int numberbytes)
    {
    unsigned char uch, ¨*cp;
    int count,i;
    for(cp=string,i=0;I>4)&15] ;
    if(count^1) uch^0x80;
    *cp=uch;
    }
    Return(string);
    }

    This does 1 loop per byte, with 1 shift of 4 bits. I have not timed it, but I think my code is much faster in execution. Am I wrong? (I have not tested it, but will do a comparison test. I wrote it while I was watching TV)

  2. Here is a second comment. Does the parity bit always have to be the 8th bit? Could it be the next left bit that adds a 1 to the left of significant bit in the bite. So 00000011 would become 00000111 instead of 10000011.

    I am studying the Algorithm by studying code.

  3. There was an upload error with the for statement above posting, here is the code again

    UCHAR partable[16]={0,1,1,2,1,2,2,3,1,2,2,3,3,3,3,4};
    char * disparity(unsigned char string, int numberbytes)
    {
    unsigned char uch, ¨*cp;
    int count,i;
    for(cp=string,i=0;I>4)&15] ;
    if(count^1) uch^0x80; *cp=uch;
    }
    Return(string);
    }

    I paste this as correct, hope that html does not change the code If there is a problem, I will use the word less than for the less than symbol. I do not really know html.

  4. UCHAR partable[16]={0,1,1,2,1,2,2,3,1,2,2,3,3,3,3,4};
    char * disparity(unsigned char string, int numberbytes)
    {
    unsigned char uch, ¨*cp;
    int count,i;
    for(cp=string,i=0;I\\>4)&15] ;
    if(count^1) uch^0x80; *cp=uch;
    }
    Return(string);
    }

    (trying to escape the less than symbol

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s