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

Des.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00097 
00098 
00099 const int Des::blockSize = DES_BLOCK_SIZE; 
00100 
00107 void 
00108 Des::generateRandomKey(unsigned char* key_buff)
00109 {
00110     des_cblock* key_block;
00111     key_block = (des_cblock*) key_buff;
00112     
00113     do {
00114         des_random_key (key_block);
00115         des_set_odd_parity (key_block);
00116     } while (des_is_weak_key (key_block));
00117 } // fn generateRandomKey
00118 
00119 
00120 Des::Des ()
00121 {
00122 }
00123 
00124 
00125 Des::~Des ()
00126 {
00127 }
00128 
00129 
00134 bool 
00135 Des::setKey(unsigned char* key)
00136 {
00137     int res;
00138     
00139     res = des_set_key_checked((des_cblock*)key, m_schedule);
00140 
00141     return (res >= 0 ? true : false);
00142 } // fn setKey
00143 
00144 
00154 int 
00155 Des::encrypt(unsigned char* in, int inlen, unsigned char* out)
00156 {
00157     int outlen;
00158     int padding;
00159     unsigned char block[blockSize];
00160 
00161     padding = (blockSize - (inlen % blockSize)) % blockSize;
00162     outlen = inlen + padding + 1;
00163 
00164     *(out++) = (unsigned char) padding;
00165     while (inlen >= blockSize) {
00166         des_ecb_encrypt((des_cblock*) in, (des_cblock*) out,
00167                         m_schedule, true);
00168 
00169         inlen -= blockSize;
00170         in += blockSize;
00171         out += blockSize;
00172     }
00173     if (inlen > 0) {
00174         memcpy(block, in, inlen);
00175         des_ecb_encrypt((des_cblock*) block, (des_cblock*) out,
00176                         m_schedule, true);
00177     }
00178 
00179     return outlen;
00180 } // fn encrypt
00181 
00182 
00192 int 
00193 Des::decrypt(unsigned char* in, int inlen, unsigned char* out)
00194 {
00195     int padding;
00196     int outlen;
00197 
00198     padding = (int) *(in++);
00199     inlen--;
00200 
00201     if (!(padding >= 0 && padding < blockSize && (inlen % blockSize) == 0)) {
00202         return -1;
00203     }
00204 
00205     outlen = inlen - padding;
00206     
00207     while (inlen >= blockSize) {
00208         des_ecb_encrypt((des_cblock*) in, (des_cblock*) out,
00209                         m_schedule, false);
00210 
00211         inlen -= blockSize;
00212         in += blockSize;
00213         out += blockSize;
00214     }
00215     
00216     return outlen;
00217 } // fn decrypt
00218 

Generated at Thu Jul 11 13:31:50 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001