Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Des Class Reference

Performs DES encryption and decryption in-memory. More...

#include <Des.h>

List of all members.

Public Methods

 Des ()
 ~Des ()
bool setKey (unsigned char *key)
int encrypt (unsigned char *in, int inlen, unsigned char *out)
int decrypt (unsigned char *in, int inlen, unsigned char *out)

Static Public Methods

void generateRandomKey (unsigned char *key_buff)

Static Public Attributes

const int blockSize = DES_BLOCK_SIZE
 basic block size for key/encoding/decoding. More...


Private Attributes

des_key_schedule m_schedule


Detailed Description

Performs DES encryption and decryption in-memory.

Des encryptes and decryptes data using des_ecb_encrypt().
des_ecb_encrypt() encryptes and decrypts 8-byte data block.
But, Des encryptes and descryptes data of arbitrary size.

 Encryption Process 
 
  fig.1
  
    [dddddddd][dddddddd]....[dddddddd][ddd*****]   : original data
        |         |             |         |
        V         V             V         V
 [p][eeeeeeee][eeeeeeee]....[eeeeeeee][eeeeeeee]   : encrypted data

 d: original data
 *: padding data
 e: encrypted data
 p: size of padding data, (p >= 0 and p < 8 so, p is 1-byte data)

 1. Des divides original data into a sequence of 8-byte blocks and
    fills last block with padding data to make it be a 8-byte block.
 2. Store the size of padding data into the first byte of output data.
 3. Encrypt each block of original data and add the result to the output data. 
 4. The output data becomes the encrypted data of the original data.
 5. Fig.1 shows the above process and the format of encrypted data.
    The size of encrypted data is always the form of 8*n+1.
    The size of encrypted data is 1 + the size of original data + the size of
    padding data.
    Because the size of padding data is less or equal than 7, The size of
    encrypted data is less or equal than the size of original data + 8. 
    So, It is sufficent to reserve the size of (original data + 8) bytes buffer
    for encrypted data.

 Decryption  Process
 
 The decryption process is the reverse of the encryption process.
 This is straight forward.

=============
Des Class API
============= 

 Member Variables
    m_schedule:
        There are two phases of DES encryption.  The
        first is the generation of a des_key_schedule from a key,
        The second is the actual encryption.
        m_schedule is a des_key_schedule used in encryption.

 Member Functions

    Des() is a constructor of Des. It does nothing.
    
    ~Des() is a destructor of Des. It does nothing.

    generateRandomKey() generates a DES key at random. 
    A DES key is a 8-byte data block. 

    setKey() receives a DES key, generates a des_key_schedule from the key and
    sets m_schedule to the generated schedule.

    encrypt() encrypts input data and stores the result into output buffer.

    decrypt() decrypts input data and stores the result into output buffer.

=======
Example
=======

Des des;
unsigned char key[8];

int  enc_len;
char enc_data[1024+8];
int  dec_len;
char dec_data[1024];

// Generate a DES key at random and store it to a buffer 'key'.
Des::generateRandomKey(key);

// Encrypt a "test data!!!" and store the result to enc_data.
enc_len = des.encrypt("test data!!!", strlen("test data!!!"), enc_data);
ASSERT(enc_len <= strlen("test data!!!") + 8);

// Decrypt enc_data and store the result to dec_data.
dec_len = des.decrypt(enc_data, enc_len, dec_data);

  

Definition at line 13 of file Des.h.


Constructor & Destructor Documentation

Des::Des
 

Definition at line 120 of file Des.cpp.

Des::~Des
 

Definition at line 125 of file Des.cpp.


Member Function Documentation

int Des::decrypt unsigned char * in,
int inlen,
unsigned char * out
 

Decrypt data with internally stored DES key.

Parameters:
in   input data
inlen   the size of input data
out   output buffer. the output size is less or equal than intput size - 1.
Returns:
the size of output data.

Definition at line 193 of file Des.cpp.

int Des::encrypt unsigned char * in,
int inlen,
unsigned char * out
 

Encrypt data with internally stored DES key.

Parameters:
in   input data
inlen   the size of input data
out   output buffer. the output size is less or equal than intput size + Des::blockSize.
Returns:
the size of output data.

Definition at line 155 of file Des.cpp.

void Des::generateRandomKey unsigned char * key_buff [static]
 

Generate a random DES key.

Parameters:
key_buff   the buffer to store the generated key. generated key size is Des::blockSize.

Definition at line 108 of file Des.cpp.

bool Des::setKey unsigned char * key
 

Set DES key for use with encryption and decryption functions.

Parameters:
key   A DES key, such as one produced by Des::generateRandomKey.

Definition at line 135 of file Des.cpp.


Member Data Documentation

const int Des::blockSize = DES_BLOCK_SIZE [static]
 

basic block size for key/encoding/decoding.

Definition at line 99 of file Des.cpp.

des_key_schedule Des::m_schedule [private]
 

Definition at line 32 of file Des.h.


The documentation for this class was generated from the following files:
Generated at Thu Jul 11 13:31:56 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001