Question: unsigned int pad _ text ( char * padded, char * unpadded ) { 2 unsigned int len = 0 ; 3 unsigned int ptr

unsigned int pad_text(char* padded, char* unpadded){2 unsigned int len =0; 3 unsigned int ptr =0; 45/* Get string length */6 while(unpadded[len++]); 78/* Get padding amount */9 unsigned int npadding =(4-(len %4))%4; 1011/* Copy the original string */12 do {13 padded[ptr]= unpadded[ptr]; 14} while(unpadded[ptr] && unpadded[ptr++]!=
); 1516/* Padding */17 for(int i =0; i < npadding; i++){18 padded[ptr + i]=\0; 19}2021 return ((len +3)/4); 22}
unsigned int sdes_gen_key(char* pw){2 unsigned int k =0; 3 char c =0; 45 while(*pw && *pw !=
){6 char c =*pw++; 7 k = k ^ c; 8 k =((k & 0xFF)<<24)|(k >>8); 9 k = k + c; 10}1112 return k; 13}
unsigned int sdes_perm(unsigned int d){23 unsigned int r = d; 45 r =((r & 0xFFFF0000llu)>>16)|((r & 0x0000FFFFllu)<<16); 6 r =((r & 0xFF00FF00llu)>>8)|((r & 0x00FF00FFllu)<<8); 7 r =((r & 0xF0F0F0F0llu)>>4)|((r & 0x0F0F0F0Fllu)<<4); 8 r =((r & 0xCCCCCCCCllu)>>2)|((r & 0x33333333llu)<<2); 9 r =((r & 0xAAAAAAAAllu)>>1)|((r & 0x55555555llu)<<1); 1011 return r; 12}
unsigned int sdes_gen_roundkey(unsigned int i, unsigned k){2/* Just a Simple Rotation */3 unsigned int nbits =(i %8)*4; 45 unsigned mask =(1llu << nbits)-1; 6 unsigned res =((k & mask)<<(32- nbits))|(k >> nbits); 78 return res; 9}
void sdes_encrypt(char*_clear, char*_cipher, unsigned int key, unsigned int nblocks){2 unsigned int* clear =(unsigned int*)_clear; 3 unsigned int* cipher =(unsigned int*)_cipher; 45 for(int i =0; i < nblocks; i++){6/* Get a block from the string */7 unsigned int block = clear[i]; 89/* Encrypt */10 unsigned int cipher[i]= sdes_encrypt_block(block, key); 11 cipher[i]= e_block; 12}13}
unsigned int sdes_encrypt_block(unsigned int x, unsigned int key){2 unsigned int d =0; 34 d = sdes_perm(x); 5 for (int i =0; i <8; i++){6 unsigned int roundkey = sdes_gen_roundkey(i, key); 7 d = sdes_encrypt_round(i, d, roundkey); 8}9 d = sdes_perm(d); 1011 r
unsigned int sdes_encrypt_round(unsigned int i, unsigned int block, unsigned int rkey){2 unsigned int cipher =0; 34 cipher = b; 5 cipher = cipher ^ rkey; /* XOR */6 cipher = sdes_encrypt_sbox(i, cipher); /* Substitution */78 return cipher; 9}
TheS-boxesaresimplelook-uptables: 1
char sbox[][16]={{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},2{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},3{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},4{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}}; 56 unsigned int sdes_encrypt_sbox(unsigned int i, unsigned int block){7 unsigned int res =0; 8 i = i %4; 910 for (int n =0; n <8; n++){11 char c = block & 0xF; 12 char s = sbox[i][c]; 13 res =(s <<28)|(res >>4); 1415 block = block >>4; /* Done with one nibble */16}1718 return res; 19}
void sdes_decrypt(char*_cipher, char*_clear, unsigned int key, unsigned int nblocks){2 unsigned int* clear =(unsigned int*)_clear; 3 unsigned int* cipher =(unsigned int*)_cipher; 45 for(int i =0; i < nblocks; i++){6/* Get a block from the string */7 unsigned int e_block = cipher[i]; 89/* Encrypt */10 unsigned int block = sdes_decrypt_block(e_block, key); 11 clear[i]= block; 12}13}
Then,weimplementafunctiontoimplementS-DESdecryptionofoneblock:
1 unsigned int sdes_decrypt_block(unsigned int x, unsigned int key){2 unsigned int d = sdes_perm(x); 3 for (int i =0; i <8; i++){4 unsigned int roundkey = sdes_gen_roundkey(7- i, key); 5 d = sdes_decrypt_round(7- i, d, roundkey); 6}7 d = sdes_perm(d); 89 return d; 10}
unsigned int sdes_decrypt_round(unsigned int i, unsigned int ciph, unsigned int rkey){2 unsigned int block =0; 34 block = sdes_decrypt_sbox(i, ciph); /* Substitution */5 block = block ^ rkey; /* XOR */

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!