DES generated key incorrect parity
I need to create a random generated DES key. But when I use the crypto service provider to create one the partity is wrong. The Key I am creating needs to be used outside the windows environment and so the partity needs to be correct or the device I am using will reject the key.
DES des =DES.Create();des.GenerateKey();
byte
[] key = des.Key;The above code creates a 64 bit key that does not meet the DES standard ie the parity is wrong. .net appears to ignore parity all together which is fine within dotnet but not if you need to use the key elsewhere.
Does anyone know a way to tell .net to do parity correctly as per the DES standard?
The NIST FIPS PUB 46-3 Data Encryption Standard (DES) says that a DES key consists of 64 bits of which 56 bits are directly used by the algorithm and the other 8 bits which are not used are used for error detection. These 8 bits are parity bits to make the parity of each 8-bit byte of the key odd (that is there is an odd number of 1s in each byte).
The question is not which CSP is the one to use. The DES CSP is the one that the query is raised on.
When the System.Security.Cryptography.DES method GenerateKey is called, instead of returning a valid DES key with correct parity it appears to be providing 64 random bits, often with incorrect parity. When the generated key is entered into existing third party commercial devices that require DES keys and validate the key on entry, these keys generated by the DES CSP are rejected. What was expected of the DES CSP was that 56 bits of random data would be generated and the parity bits inserted. If simply 64 bits of random data were required we could call on the RNG CSP to provide that.
Our current course of action is to post-process the generated key so that the Parity is correct. The objective of the query was to see if anyone else had experienced this, see how they dealt with it, and also to perhaps warn users of the DES CSP that GenerateKey actually doesn't necessarily generate a valid DES key.
Hi Daniel,
Thanks for reporting this. I can confirm that the DESCryptoServiceProvider class does not generate parity bits on its keys, instead just generating 64 random bits as you've identified. The workaround that you propose of post-processing the key is a good one. As another option, you could subclass the DES base class overriding the GenerateKey method to include your post processing. Internally, you could then delegate hte other operations to a DESCryptoServiceProvider class, making sure it used the key that you generated with the parity bits.
-Shawn