As a bit of practice, I'm reinventing the wheel by implementing DES in python. Before I even got beyond the first function, I came across a problem... With key legorooj, which happens to be "64" bits, that should work as a DES key. However, when I use python's built in binary transformation function, I get 56 bits. What should I do? Should I pad the input bits?

  • 2
    $\begingroup$ This is rather a programming question. The 'b' removes the leading zeros. Use this $\endgroup$
    – kelalaka
    Dec 11 '19 at 8:12
  • 2
    $\begingroup$ The whole idea that "legorooj" is a legal key for DES is completely flawed; keys are not character strings (nothing to do with your name in particular, although names are likely to be even worse keys, of course). That you should pad (or prune) keys when they do not comply to a specific format is possibly even more flawed. Use a password based key derivation function if you want to use a password. Otherwise, if you must use text to represent a (symmetric) key, use hexadecimals of the actual key in bytes instead. $\endgroup$
    – Maarten Bodewes
    Dec 11 '19 at 13:54
  • $\begingroup$ @Maarten-reinstateMonica I know all about the insecurity of not using a KDF. You've told me that many times, and I'm paranoid too : ). When testing code, I don't bother with the KDF. I use something that works to check the code. The question was about what to do when the key's bits were only 7 per char; but that turned out to be my programming. $\endgroup$
    – Legorooj
    Dec 11 '19 at 22:24
  • $\begingroup$ what you're doing seems like a very inefficient way to implement DES (splitting chars up into bitstrings etc.) Just use a good C library with Python bindings if you think you must implement this ancient algorithm in the first place (why not AES?).. $\endgroup$ Dec 12 '19 at 5:36
  • $\begingroup$ @HennoBrandsma AES is actually my end goal; I'm learning DES first. Sometimes reinventing the wheel is good practice. $\endgroup$
    – Legorooj
    Dec 12 '19 at 5:43

The '0b' representation removes leading 0's. The actual value (as a byte) of l (lowercase letter L) as a binary 8bit byte is 01101100 and likewise for the others (they are lower ASCII, so the highest bit is 0).

Note that the effective DES-key is actually 56 bits effectively; the least significant bit is a "parity bit", in the official standard, it should be set or unset so that the total parity of the byte is odd: so 011011010 is made the same (as an effective key byte) as 'm' which is 01101101. These final bits are not used in the algorithm anyway.

  • $\begingroup$ The standard is odd parity not even -- although almost everything today ignores it anyway. $\endgroup$ Dec 12 '19 at 4:32
  • $\begingroup$ @dave_thompson_085 I corrected my post, thx. $\endgroup$ Dec 12 '19 at 16:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.