1
$\begingroup$

I have recently programmed the DES algorithm. Right now it works just with integers (or better to say chars) in the range <0;255>. How can I use its whole potential - using just ASCII would be a waste (there are many characters I would never use).

$\endgroup$
  • 4
    $\begingroup$ DES, like all modern crypto algorithms, works on a sequence of bytes. How you encode the message as bytes is out of scope for crypto. For text UTF-8 is the standard choice, if your data is different, you need to choose a different encoding. $\endgroup$ – CodesInChaos Nov 27 '15 at 10:22
5
$\begingroup$

Programming your own DES is usually a bad idea (unless as an exercise to understand the algorithm better), but OK. By definition the algorithm works with blocks of data that consist of 8 bytes (i.e. integers in the range 0..255), and the key is 7 of those bytes (or 8 with unused parity bits).

What the data means (text or binary files like Office documents or zip files) etc. is irrelevant. I don't see what you mean by "full potential". You can just use it for ASCII text. It doesn't really matter what range the bytes are in. The encoding of the data is up to you (you can pack lower ASCII data into 7 bit characters, that overflow into the next byte, as SMS does, e.g.), and not a part of DES or any crypto-algorithm. Bytes are bytes. DES handles bytes.

$\endgroup$
1
$\begingroup$

DES is an old (by itself insecure) block cipher. Block ciphers work on blocks of bits. Due to the nature of modern computer the input and output of the cipher is however normally limited to 8-bit bytes or octets. The time that a byte could have 7 or 9 bits is long gone.

Again, due to the nature of computing, everything inside a computer is encoded in bits and, at a slightly higher level, bytes. This data - whatever it represents - can be encrypted using a block cipher mode of operation such as CBC. If you have a char* you have a direct representation of the bytes though; a char in the C-language is actually just a byte, not a character.

With higher level languages or constructs - e.g. Java - you may not be able to directly access the internal encoding. It may also be that the internal encoding is not specified or standardized. In that case you need to perform the encoding and decoding yourself. So you first encode to a byte array (char[]in C) and then encrypt the result. Encoding of entire objects to a byte stream is referred to as "serialization".

If you think you are wasting bits then you have two options: either you use a smaller encoding for the data to encrypt (e.g. UTF-8 instead of UTF-16 or 7 bit ASCII instead of 8 bit ASCII), or you can compress the result (e.g. zipping it) before encryption. Note that the length of the ciphertext is dependent on the length of the plaintext. That means an attacker may be able to extract information from the size of the ciphertext.

Sometimes you need the encryption result to be of the same format as the input. In that case you need to encrypt using Format Preserving Encryption. This is a special branch of cryptography that requires quite a bit of knowledge and is probably best used by experts and scholars on the subject.


As said, DES is insecure if just because of its small key size. If you want to use it to successfully protect data for confidentiality you should only use it as building block for (3-key) TDES (also known as triple DES, TDEA, 3DES, DES-EDE etc).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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