Des

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DES, an example

Mike May, S. J., 2002 Time to walk through a DES conversion


>

restart:

This worksheet walks through the example of using DES that is on the handouts we have been looking at. This worksheet assumes that you understand key expansion. In this worksheet it is done with a single command that suppresses all the intermediate steps. It is done in full detail in the DES key expansion worksheet. This worksheet also assume that you have been through the worksheets on DES constants and DES functions and have created the DES.m file that contains those functions and constants. (If this is not true you should run those worksheets now.)
>

read `DES.m`:

Initial setup
We start with a key in hex which we expand into a list of round keys.
>

keytest := "133457799BBCDFF1"; key := keyexpander(keytest):

Then we establish a message in hex, which we convert to binary.


>

mess1 := "0123456789ABCDEF"; bin1 := bin64hex(mess1);

Next we apply permutation IP


>

bin2 := InitPerm(bin1);

Now we can define R0 and L0.


>

R0 := substring(bin2,33..64); L0 := substring(bin2,1..32);

Encrypting one round


We need to expand R0 and xor, the result with key[1].
>

expander(R0); key[1]; bin3 := xor48(key[1],expander(R0));

Next we want to take the 48 bit result and break it into a vector of 8 6 bit words. Each word is then used to get an s-box value.
>

vec1 := prodbs(bin3); vec2 := linalg[vector](8,[seq(SBox[i][vec1[i]],i=1..8)]);

Next we want to convert the values in vec2 into 4 bit binary words and concatenate them together.
>

bin4 := cat(seq(vec2[i],i=1..8));

The result of the concatenation is then acted on by the permutation P to give the f-value.
>

f1 := PPerm(bin4);

The result is XORed with L0 to give R1. L1 is simply R0.


>

R1 := xor32(f1,L0); L1 := R0;

Encrypting 15 more rounds


Now we simply repeat this simple process 15 more times. (Oh no!!!! Time to write a procedure and do a loop.)
>

L := linalg[vector](16): R := linalg[vector](16): L[1] := L1; R[1] := R1;

>

fcomb := proc(ri, ki) local t1,t2, t3, vec1, vec2, i: t1 := xor48(ki,expander(ri)): vec1 := prodbs(t1): vec2 := linalg[vector](8,[seq(SBox[i][vec1[i]],i=1..8)]): t2 := cat(seq(vec2[i],i=1..8)): t3 := PPerm(t2): end: > for i from 2 to 16 do L[i] := R[i-1]; R[i] := xor32(L[i-1],fcomb(R[i-1],key[i])); od;

Now we put the left and right halves together in reverse order, invert the initial permutation, and convert to hex.
>

puttogether := cat(R[16],L[16]); bincodetext := FinalPerm(puttogether); ciphertext := convert(convert(parse(bincodetext),decimal,binary),hex);

Decrypting
Now we try decrypting. We will essentially go backward through the procedure.
>

R2 := linalg[vector](17): L2 := linalg[vector](17): R2[17] := R[16]; L2[17] := L[16];

>

for i from 1 to 16 do j := 17-i: R2[j] := L2[j+1]; L2[j] := xor32(R2[j+1],fcomb(L2[j+1],key[j])); od;

Now that we have done the 16 rounds, we need to put the two halves of the string together, unscramble it, make it a number and convert it back to ASCII
>

almost := cat(L2[1],R2[1]); unscrambled := FinalPerm(almost); code3 := convert(convert(parse(unscrambled),decimal,binary),hex);

Thus we have recovered the original message.


>

Quick encryption and decryption


We would also like to do the same conversion with single commands. Recall our stating values:
>

keytest := "133457799BBCDFF1"; mess1 := "0123456789ABCDEF";

We want to expand the key and encrypt the message.


>

key := keyexpander(keytest): ciphertext := qdDEShex(mess1,key);

Now we decrypt:
>

recoveredtext := unDEShex(ciphertext, key);

E xercises - Key expansion 1) Start with the key "iamhappy" and converts to the hex string "69616D6861707079". Convert the key to binary and apply PC1.
>

2) Break the key into 2 halves and create arrays of the halves shifted 16 times according to the given formula.
>

3) Put the halves back together and apply PC2 to produce the 16 keys in the key vector.
>

4) Use keyexpander to do the same thing in a single command. Make sure that your answer agrees with your answer to exercise 3.
>

E xercises - Encryption 5) The message "Be Happy" converts to the hex string "4265204861707079". Convert this to binary and apply the initial permutation,
>

6) Create 2 arrays of length 16 entitled R and L. Define R0 and L0 as the appropriate halves of your previous answer. Use these numbers and your key to define R[1] and L[1]. Inductively define R[i] and L[i] for i from 2 through 16.
>

7) Put the 2 halves back together in reverse order and use IPI to get the coded message.
>

8) Encrypt the message with a single command using qdDEShex.


>

9) Use DES and the "iamhappy" key to encrypt the first 8 letters of your name. Put the results to the bulletin board. Decrypt someone else's message.
>

You might also like