Exploring DES, Modes of encryption
© Mike May, S. J., 2002, maymk@slu.edu
| > | restart: |
This worksheet does some exploration of DES. It focuses on the 4 standard modes of using DES to encrypt and decrypt longer message .
Before you start this worksheet, you need to establish the file DES.m with a collection of constants and functions used by this worksheet. This can be done by opening the worksheets entitled DES constants and DES functions and executing everything on those worksheets to define all the constants and functions we need.
This worksheet assumes that you have worked through the worksheet DES Example which looks at the mechanics of DES encryption on a single block of data. (You don't require to have done that in this session. We don't need results defined on that worksheet to make this one run.)
| > | read `DES.m`: |
We want to start with a standard key and a standard message for this worksheet.
| > | plaintext := "Good morning Mr. Phelps. Your mission, should you choose to accept it, is to learn to use DES."; DESkeyASCII := "Be-Happy"; |
We want to convert our key to a hex string. Then we want to expand the key out to the 16 subkeys.
| > | hexkey := ASCII2hex16(DESkeyASCII); keytable := keyexpander(hexkey): |
We also want to break our message into a list of hexwords.
| > | plainhex := asciistrtohexword(plaintext); plainlength := linalg[vectdim](plainhex); |
Since our message is 12 blocks long we will need to use DES 12 times to encode our standard message.
Mode 1 - ECB - Electronic Code Book mode
The easiest mode to understand is the ECB mode. This mode simply applied DES to each message block in turn. The mode is easy to understand, can be adapted to parallel processing, and errors in one block of message do not cause problems in later blocks.
| > | ECBcipher := linalg[vector](plainlength): for i from 1 to plainlength do ECBcipher[i] := qdDEShex(plainhex[i],keytable): od; print(ECBcipher); |
For decryption in this mode we simply apply unDEShex to each cipherblock in turn.
| > | outofECB := linalg[vector](plainlength): for i from 1 to plainlength do outofECB[i] := unDEShex(ECBcipher[i],keytable): od: print(outofECB); |
| > | hexwordtoasciistr(outofECB); |
Exercises:
1) Create a short message (50-150 characters) that you want to encrypt. Encrypt it with DES and the standard key. Make sure that you can decrypt the message. Post your message to the bulletin board and decrypt someone else's message. (You probably want to avoid the variable names plainmess, plainhex, and plainlength, since we may want to use the current values of those variables later in the worksheet.)
| > |
2) Pick a DES key. (It can be either 8 ASCII characters or 16 hex characters.) Expand your key into a list of 16 subkeys. (You probably want to avoid the variable names DESkeyASCII, hexkey, and keytable).
| > |
3) Use your key to encode the the standard message. Make sure that it decodes as well.
| > |
Mode 2 - CBC - Cipher Block Chaining
The next mode is the CBC or Cipher Block Chaining mode. In this mode each block is XORed with the previous cipherblock before encryption. For the first block we start with an initialization vector. For this worksheet, we will use`0123456789ABCDEF` as the initialization vector. In real practice, it is not unusual to start with the zero vector as the initial vector.
| > | IVhex := `0123456789ABCDEF`: CBCfeedin := linalg[vector](plainlength): CBCcipher := linalg[vector](plainlength): CBCfeedin[1] := xor64hex(IVhex, plainhex[1]); CBCcipher[1] := qdDEShex(CBCfeedin[1],keytable); for i from 2 to plainlength do CBCfeedin[i] := xor64hex(CBCcipher[i-1], plainhex[i]); CBCcipher[i] := qdDEShex(CBCfeedin[i],keytable): od; print(CBCcipher); |
To decipher in this mode we unDES each block, then XOR the result with the previous block of plaintext result. Note that we treat the initialization vector as the zeroth block of plaintext.
| > | outofCBC := linalg[vector](plainlength): CBCbeforeXOR := linalg[vector](plainlength): CBCbeforeXOR[1] := unDEShex(CBCcipher[1],keytable); outofCBC[1] := xor64hex(CBCbeforeXOR[1], IVhex); for i from 2 to plainlength do CBCbeforeXOR[i] := unDEShex(CBCcipher[i],keytable): outofCBC[i] := xor64hex(CBCbeforeXOR[i], CBCcipher[i-1]); od; print(CBCcipher); print(outofECB); hexwordtoasciistr(outofCBC); |
Exercises:
4) Encrypt your short message with DES and the standard key using CBC mode. Make sure that you can decrypt the message. Post your message to the bulletin board and decrypt someone else's message.
| > |
5) Use your key to encode the the standard message with DES in CBC mode. Make sure that you can decrypt back to the standard message.
| > |
Mode 3 - CFB - Cipher Feedback Mode
CFB looks very similar to CBC. The difference between the two modes is that CBC does an XOR, then encrypts, while CFB encrypts then does an XOR. The advantage of CFB is the encryption part can be done before the block of message is entered. That makes this mode better for devices where we want to encrypt a character or bit at a time.
| > | CFBoutput := linalg[vector](plainlength): CFBcipher := linalg[vector](plainlength): CFBcipher[1] := qdDEShex(IVhex,keytable); CFBoutput[1] := xor64hex(CFBcipher[1], plainhex[1]); for i from 2 to plainlength do CFBcipher[i] := qdDEShex(CFBoutput[i-1],keytable): CFBoutput[i] := xor64hex(CFBcipher[i], plainhex[i]); od; print(CFBoutput); |
To decrypt in this mode, we want to XOR each cipher block with the result of encrypting the previous ciphertext block. Note that the decryption algorithm uses encryption rather than decryption for DES.
| > | outofCFB := linalg[vector](plainlength): CFBunDES := linalg[vector](plainlength): CFBunDES[1] := qdDEShex(IVhex,keytable); outofCFB[1] := xor64hex(CFBunDES[1], CFBoutput[1]); for i from 2 to plainlength do CFBunDES[i] := qdDEShex(CFBoutput[i-1],keytable): outofCFB[i] := xor64hex(CFBoutput[i], CFBunDES[i]); od; print(CFBunDES); print(outofCFB); hexwordtoasciistr(outofCFB); |
Exercises
6) Encrypt your short message with DES and the standard key using CFB mode. Make sure that you can decrypt the message. Post your message to the bulletin board and decrypt someone else's message.
| > |
7) Use your key to encode the the standard message with DES in CFB mode. Make sure that you can decrypt back to the standard message.
| > |
Mode 4 - OFB - Output Feedback mode
In many ways this fourth mode is a return to the one time pad with DES used as a generator of a pseudo random string. Once again we start with an initialization vector. This time we simply chain the encryption of the initialization vector back on itself without any interaction with the message. We then XOR the message with the bitstream we have produced. Since this is essentially a one time pad, it is crucial that each message start with a different initialization vector.
| > | OFBoutput := linalg[vector](plainlength): OFBbinpad := qdDEShex(IVhex,keytable); for i from 1 to plainlength do CFBoutput[i] := xor64hex(OFBbinpad, plainhex[i]); OFBbinpad := qdDEShex(OFBbinpad,keytable); od; print(CFBoutput); |
Note that the creation of OFBbinpad does not interact with the message. It could be done separately.
Once again we want to decipher the message to get back to the original message.
| > | outofOFB := linalg[vector](plainlength): OFBbinpad := qdDEShex(IVhex,keytable); for i from 1 to plainlength do outofOFB[i] := xor64hex(OFBbinpad, CFBoutput[i]); OFBbinpad := qdDEShex(OFBbinpad,keytable); od; print(outofOFB); hexwordtoasciistr(outofOFB); |
Exercises:
8) Encrypt your short message with DES and the standard key using OFB mode. Make sure that you can decrypt the message. Post your message to the bulletin board and decrypt someone else's message.
| > |
9) Use your key to encode the the standard message with DES in OFB mode. Make sure that you can decrypt back to the standard message.
| > |