# DES with actual 7 byte key

Do we have any library or any mechanism where we can use true 7 byte key for DES instead of 8 byte key. I need it for keys analysis in DES and 8 byte key requirement for associated library is creating problem in getting actual keys analysis.

• My suggestion would be to find an open source implementation and modify it to do what you want. Shouldn't be too hard as I think DES ignores the parity bits anyways. – mikeazo Apr 24 '14 at 18:45
• DES ignores the parity bits and results 256 equivalent 8 byte keys to each 7 byte key leaving extra computation in my approach of analysis. Any ways Thank you so much for suggestion. Will go with it :) – ceasif Apr 24 '14 at 18:56

You need to split up your key into eight 7 bit pieces, and put these 7 bits into a byte each. The parity is in the least significant bit on most platforms, so the 7 bits need to go into the most significant bits. Of course, as the key is probably in bytes, you need to shift and combine the values in the bytes to retrieve the 7 bits.

It's possible the library does not require the parity bits to be set. In that case you don't need to calculate the parity. Otherwise you simply create the parity once you've got the 8 bytes.

Following code is in Java (which makes it a bit awkward because Java bytes are signed and most calculations assume integers).

// --- create 64 bit key from 56 bit key
// least significant bit can have any value
key64 = (byte) (key56 & 0xFE); // << 0
key64 = (byte) ((key56 << 7) | ((key56 & 0xFF) >>> 1));
key64 = (byte) ((key56 << 6) | ((key56 & 0xFF) >>> 2));
key64 = (byte) ((key56 << 5) | ((key56 & 0xFF) >>> 3));
key64 = (byte) ((key56 << 4) | ((key56 & 0xFF) >>> 4));
key64 = (byte) ((key56 << 3) | ((key56 & 0xFF) >>> 5));
key64 = (byte) ((key56 << 2) | ((key56 & 0xFF) >>> 6));
key64 = (byte) (key56 << 1);

// --- set parity in time independent of the values within key64
for (int i = 0; i < key64.length; i++) {
// if even # bits, make uneven, take last bit of count so XOR with 1
// for uneven # bits, make even, take last bit of count so XOR with 0
key64[i] ^= Integer.bitCount(key64[i] ^ 1) & 1;
}

• I edited pyDes and its accepting 7 bytes key :) thank you for comment – ceasif Apr 26 '14 at 8:43
• Note that I did not write extensive tests for this code; so please don't forget to add some unit testing. – Maarten Bodewes Apr 26 '14 at 9:31
• @fgrieu added your side channel code and removed 0xFE in favor of a comment. – Maarten Bodewes Apr 27 '14 at 13:40
• @owlstead: I see, nice. But I can't upvote twice! – fgrieu Apr 27 '14 at 13:44

Here, I edited existing pyDes library just by adding a new function to convert a 7 byte key into 64 bits required by the code. But I am not sure if the code implements DES in true sense

code

def __String_to_BitList_Key(self, data):
"""Turn the string data, into a list of bits (1, 0)'s"""
if _pythonMajorVersion < 3:
# Turn the strings into integers. Python 3 uses a bytes
# class, which already has this behaviour.
data = [ord(c) for c in data]
l = len(data) * 8+8
result =  * l
pos = 0
k=-1
for ch in data:
k+=1;j=k
i = 7

while i >= 0:
j+=1
if j== 8: result[pos] = 0;pos += 1 #appending 0 to each 8th bit in transformed 8 byte
if ch & (1 << i) != 0:
result[pos] = 1
else:
result[pos] = 0
pos += 1
i -= 1

print True;return  result;

• Yes, but most crypto libraries won't accept bit lists, and that one again is not protected against side channel attacks (because of the if). why not try and port above Java code? Doesn't set parity either. – Maarten Bodewes Apr 28 '14 at 7:58