Question: unsigned int pad _ text ( char * padded, char * unpadded ) { 2 unsigned int len = 0 ; 3 unsigned int ptr
unsigned int padtextchar padded, char unpadded unsigned int len ; unsigned int ptr ; Get string length whileunpaddedlen; Get padding amount unsigned int npadding len ; Copy the original string do paddedptr unpaddedptr; whileunpaddedptr && unpaddedptr
; Padding forint i ; i npadding; i paddedptr i; return len ;
unsigned int sdesgenkeychar pw unsigned int k ; char c ; whilepw && pw
char c pw; k k c; k k & xFFk ; k k c; return k;
unsigned int sdespermunsigned int d unsigned int r d; r r & xFFFFllur & xFFFFllu; r r & xFFFFllur & xFFFFllu; r r & xFFFFllur & xFFFFllu; r r & xCCCCCCCCllur & xllu; r r & xAAAAAAAAllur & xllu; return r;
unsigned int sdesgenroundkeyunsigned int i unsigned k Just a Simple Rotation unsigned int nbits i ; unsigned mask llu nbits; unsigned res k & mask nbitsk nbits; return res;
void sdesencryptcharclear, charcipher, unsigned int key, unsigned int nblocks unsigned int clear unsigned intclear; unsigned int cipher unsigned intcipher; forint i ; i nblocks; i Get a block from the string unsigned int block cleari; Encrypt unsigned int cipheri sdesencryptblockblock key; cipheri eblock;
unsigned int sdesencryptblockunsigned int x unsigned int key unsigned int d ; d sdespermx; for int i ; i ; i unsigned int roundkey sdesgenroundkeyi key; d sdesencryptroundi d roundkey; d sdespermd; r
unsigned int sdesencryptroundunsigned int i unsigned int block, unsigned int rkey unsigned int cipher ; cipher b; cipher cipher rkey; XOR cipher sdesencryptsboxi cipher; Substitution return cipher;
TheSboxesaresimplelookuptables:
char sbox; unsigned int sdesencryptsboxunsigned int i unsigned int block unsigned int res ; i i ; for int n ; n ; n char c block & xF; char s sboxic; res s res ; block block ; Done with one nibble return res;
void sdesdecryptcharcipher, charclear, unsigned int key, unsigned int nblocks unsigned int clear unsigned intclear; unsigned int cipher unsigned intcipher; forint i ; i nblocks; i Get a block from the string unsigned int eblock cipheri; Encrypt unsigned int block sdesdecryptblockeblock, key; cleari block;
Then,weimplementafunctiontoimplementSDESdecryptionofoneblock:
unsigned int sdesdecryptblockunsigned int x unsigned int key unsigned int d sdespermx; for int i ; i ; i unsigned int roundkey sdesgenroundkey i key; d sdesdecryptround i d roundkey; d sdespermd; return d;
unsigned int sdesdecryptroundunsigned int i unsigned int ciph, unsigned int rkey unsigned int block ; block sdesdecryptsboxi ciph; Substitution block block rkey; XOR
