问题
I am working on the DES (Data Encryption Standard) algorithm in my Cryptography class, as a part of which I have to write a C code which includes a function to check the parity of a DES key.
How can I do this?
回答1:
I would just do a Google search, and pick one of the first results that comes up.
Taken from the above link:
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] & (0×01 << j))
cPar = !cPar;
}
if(!cPar)
pucKey[i] ^= 0×01;
}
return true;
}
This isn't pure C, but it should be easy enough to translate.
来源:https://stackoverflow.com/questions/7149944/how-can-i-check-the-parity-of-a-des-key