/********************************************** *********************************************** DES Encrypt 0.0.2 by Sean Kane - http://celtickane.com Coder's note: the concept and general instructions to construct this program were taken from http://www.aci.net/kalliste/des.htm. All the DES encryption used is based off this website. Additionally, the general coding design of des_round was modeled after the a DES Javascript example (http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html) ********************************************** *********************************************/ //todo: check goto's on interface #define debug_mode 0 #define super_debug 0 #define TRUE 1 #define FALSE 0 //Header file includes #include #include //exit() #include //strlen() #include //getch() #include //pow() //Primary function prototypes (directly relate to DES) void des_encrypt(int bufMsgBin[], int bufKeyBin[], bool do_encrypt, int buffer_out[]); void des_round(int bufferL[], int prev_bufferL[], int bufferR[], int prev_bufferR[], int bufferK[], int round); void permutate(int buffer[], int pt_list[], int pt_list_count, int outbits, int buffer_out[]); void leftshift(int buffer[], int num_shifts, int buffer_out[]); //Supplementary function prototypes void verifylength(char buffer[], int length); void chr2bin(char buffer_ascii[], int buffer_bin[]); void hex2bin(char buffer_hex[], int buffer_bin[]); void print_binary(char title[], int buffer[], int size, int spacing, bool sameline); //Permutation tables int pt_pc1[] = {57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4}; #define pt_pc1_count 56 int pt_pc2[] = {14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32}; #define pt_pc2_count 48 int pt_ip[] = {58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7}; #define pt_ip_count 64 int pt_ebit[] = {32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1}; #define pt_ebit_count 48 int pt_p[] = {16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25}; #define pt_p_count 32 int pt_ip_1[] = {40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25}; #define pt_ip_1_count 64 //s transformation tables int pt_s[8][4][16] = { //S1 14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7, 0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8, 4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0, 15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13, //S2 15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10, 3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5, 0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15, 13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9, //S3 10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8, 13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1, 13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7, 1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12, //S4 7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15, 13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9, 10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4, 3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14, //S5 2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9, 14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6, 4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14, 11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3, //S6 12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11, 10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8, 9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6, 4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13, //S7 4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1, 13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6, 1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2, 6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12, //S8 13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7, 1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2, 7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8, 2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11 }; int main() { char bufMsgHex[16], bufMsgAscii[8]; //Message variables int bufMsgBin[64], bufKeyBin[64]; //Binary variables char bufKeyHex[16], bufKeyAscii[8]; //Key variables int bufOutputBin[64], bufOutputHex[16], bufOutputAscii[8]; char chrPrompt, chrSecPrompt; int i,j; //The first thing we're concerned about is getting the user input //All the main() function does is process the initial input, parse it //into the data that we want, then call a function that will encrypt for(;;) { printf("(E)ncrypt, (D)ecrypt, (Q)uit: "); chrPrompt = getchar(); switch( chrPrompt ) { //********************************************** //Interface for encryption //********************************************** case 'e': case 'E': //Get the message from the user fflush(stdin); //clear the garbage out of the input buffer printf("Is the message in (H)exidecimal or (A)scii? "); gReevalEMsg: //If they didn't type in H or A...make them do it again chrSecPrompt = getchar(); switch( chrSecPrompt ) { case 'h': case 'H': gEReenterMsgHex: printf("Enter 16-digit hexidecimal message: "); scanf("%16s", bufMsgHex); verifylength(bufMsgHex,16); //Let's verify all the digits are hexidecimal... for(i=0;i<16;i++) { if( !(((bufMsgHex[i]>47) && (bufMsgHex[i]<58)) || ((bufMsgHex[i]>64) && (bufMsgHex[i]<71)) || ((bufMsgHex[i]>96) && (bufMsgHex[i]<103)))) { printf("Hexidecimal numbers may only contain numbers 0-9 and letters A-F\n"); goto gEReenterMsgHex; } } hex2bin(bufMsgHex,bufMsgBin); break; case 'a': case 'A': printf("Enter 8-digit ascii message: "); scanf("%8s", bufMsgAscii); verifylength(bufMsgAscii,8); chr2bin(bufMsgAscii,bufMsgBin); break; default: printf("Please type H or A:"); fflush(stdin); goto gReevalEMsg; break; } if(debug_mode) { print_binary("Message binary: ",bufMsgBin,64,8, FALSE); } //Get the key from the user fflush(stdin); //clear the garbage out of the input buffer printf("Is the key in (H)exidecimal or (A)scii? "); gReevalEKey: //If they didn't type in H or A...make them do it again chrSecPrompt = getchar(); switch( chrSecPrompt ) { case 'h': case 'H': gReenterKeyHex: printf("Enter 16-digit hexidecimal key: "); scanf("%16s", bufKeyHex); verifylength(bufKeyHex,16); //Let's verify all the digits are hexidecimal... for(i=0;i<16;i++) { if( !(((bufKeyHex[i]>47) && (bufKeyHex[i]<58)) || ((bufKeyHex[i]>64) && (bufKeyHex[i]<71)) || ((bufKeyHex[i]>96) && (bufKeyHex[i]<103)))) { printf("Hexidecimal numbers may only contain numbers 0-9 and letters A-F\n"); goto gReenterKeyHex; } } hex2bin(bufKeyHex,bufKeyBin); break; case 'a': case 'A': printf("Enter 8-digit ascii key: "); scanf("%8s", bufKeyAscii); verifylength(bufKeyAscii,8); chr2bin(bufKeyAscii,bufKeyBin); break; default: printf("Please type H or A:"); fflush(stdin); goto gReevalEKey; break; } if(debug_mode) { print_binary("Key binary: ",bufKeyBin,64,8, FALSE); } des_encrypt(bufMsgBin,bufKeyBin,TRUE,bufOutputBin); //Convert bufEncrypted to hex and ascii printf("Hexidecimal output: "); for(i=0;i<16;i++) { j = bufOutputBin[i*4] * 8 + bufOutputBin[i*4+1] * 4 + bufOutputBin[i*4+2] * 2 + bufOutputBin[i*4+3]; bufOutputHex[i] = j; printf("%x",bufOutputHex[i]); } printf("\n"); break; //********************************************** //Interface for decryption //********************************************** case 'd': case 'D': //Get the message from the user fflush(stdin); //clear the garbage out of the input buffer printf("Is the ciphertext in (H)exidecimal or (A)scii? "); gReevalDMsg: //If they didn't type in H or A...make them do it again chrSecPrompt = getchar(); switch( chrSecPrompt ) { case 'h': case 'H': gDReenterMsgHex: printf("Enter 16-digit hexidecimal ciphertext: "); scanf("%16s", bufMsgHex); verifylength(bufMsgHex,16); //Let's verify all the digits are hexidecimal... for(i=0;i<16;i++) { if( !(((bufMsgHex[i]>47) && (bufMsgHex[i]<58)) || ((bufMsgHex[i]>64) && (bufMsgHex[i]<71)) || ((bufMsgHex[i]>96) && (bufMsgHex[i]<103)))) { printf("Hexidecimal numbers may only contain numbers 0-9 and letters A-F\n"); goto gDReenterMsgHex; } } hex2bin(bufMsgHex,bufMsgBin); break; case 'a': case 'A': printf("Enter 8-digit ascii ciphertext: "); scanf("%8s", bufMsgAscii); verifylength(bufMsgAscii,8); chr2bin(bufMsgAscii,bufMsgBin); break; default: printf("Please type H or A:"); fflush(stdin); goto gReevalDMsg; break; } if(debug_mode) { print_binary("Ciphertext binary: ",bufMsgBin,64,8, FALSE); } //Get the key from the user fflush(stdin); //clear the garbage out of the input buffer printf("Is the key in (H)exidecimal or (A)scii? "); gReevalDKey: //If they didn't type in H or A...make them do it again chrSecPrompt = getchar(); switch( chrSecPrompt ) { case 'h': case 'H': gDReenterKeyHex: printf("Enter 16-digit hexidecimal key: "); scanf("%16s", bufKeyHex); verifylength(bufKeyHex,16); //Let's verify all the digits are hexidecimal... for(i=0;i<16;i++) { if( !(((bufKeyHex[i]>47) && (bufKeyHex[i]<58)) || ((bufKeyHex[i]>64) && (bufKeyHex[i]<71)) || ((bufKeyHex[i]>96) && (bufKeyHex[i]<103)))) { printf("Hexidecimal numbers may only contain numbers 0-9 and letters A-F\n"); goto gDReenterKeyHex; } } hex2bin(bufKeyHex,bufKeyBin); break; case 'a': case 'A': printf("Enter 8-digit ascii key: "); scanf("%8s", bufKeyAscii); verifylength(bufKeyAscii,8); chr2bin(bufKeyAscii,bufKeyBin); break; default: printf("Please type H or A:"); fflush(stdin); goto gReevalDKey; break; } if(debug_mode) { print_binary("Key binary: ",bufKeyBin,64,8, FALSE); } des_encrypt(bufMsgBin,bufKeyBin,FALSE,bufOutputBin); //Output as hex and ascii printf("Hexidecimal output: "); for(i=0;i<16;i++) { j = bufOutputBin[i*4] * 8 + bufOutputBin[i*4+1] * 4 + bufOutputBin[i*4+2] * 2 + bufOutputBin[i*4+3]; bufOutputHex[i] = j; printf("%x",bufOutputHex[i]); } printf("\nAscii output: "); for(i=0;i<8;i++) { j = bufOutputBin[i*8] * 128 + bufOutputBin[i*8+1] * 64 + bufOutputBin[i*8+2] * 32 + bufOutputBin[i*8+3] * 16 + bufOutputBin[i*8+4] * 8 + bufOutputBin[i*8+5] * 4 + bufOutputBin[i*8+6] * 2 + bufOutputBin[i*8+7]; bufOutputAscii[i] = j; printf("%c",bufOutputAscii[i]); } printf("\n"); break; case 'q': case 'Q': exit(1); break; default: fflush(stdin); //clear the garbage out of the input buffer printf("Please type E, D, or Q\n"); break; } //end select fflush(stdin); } //end for getch(); return 0; } //********************************************** //Primary functions -- directly relate to DES encryption //********************************************** void des_encrypt(int bufMsgBin[], int bufKeyBin[], bool do_encrypt, int buffer_out[]) { //buffer_out[] will contain the encrypted phrase int bufKeyBin56[56], bufKeyC16[17][28], bufKeyD16[17][28], bufKeyCD56[17][56], bufKeyCD48[17][48]; //Key variables int bufMsgBinIP[64], bufMsgBinL[17][32], bufMsgBinR[17][32], bufMsgBinRL[17][64]; int i,j; //********************************************** //Step 1: Encode the 64-bit key //********************************************** //*********************** //Step 1.1: permutate the 64-bit key into a 56-bit key using table pc_1 [verified] //*********************** permutate(bufKeyBin,pt_pc1,pt_pc1_count,56,bufKeyBin56); if(debug_mode) { print_binary("After permutating the key with table pc_1: ",bufKeyBin56,56,7, FALSE); } //*********************** //Step 1.2: split the 64-bit key into 2, 28-bit keys //*********************** for(i=0;i<28;i++) { bufKeyC16[0][i] = bufKeyBin56[i]; } for(i=28;i<56;i++) { bufKeyD16[0][i-28] = bufKeyBin56[i]; } if(debug_mode) { printf("After splitting the key from 56-bit to 2, 28-bits:\n"); print_binary("C[0]: ",bufKeyC16[0],28,7,FALSE); print_binary("D[0]: ",bufKeyD16[0],28,7,FALSE); } //*********************** //Step 1.3: make 16 CD subkeys, each left shift 1 or 2, then concatenate CD //*********************** if(debug_mode) { printf("\nAfter left-shifting the key and concatenation: \n"); } for(i=0;i<=16;i++) { if ( i==0 ) { //don't do stuff to it... for(j=0;j<28;j++) { //Concatenate half of it bufKeyCD56[i][j] = bufKeyC16[i][j]; } for(j=0;j<28;j++) { //Concatenate the other half bufKeyCD56[i][j+28] = bufKeyD16[i][j]; } goto skipshifts; } if( (i==1) || (i==2) || (i==9) || (i==16) ) { leftshift(bufKeyC16[i-1],1,bufKeyC16[i]); for(j=0;j<28;j++) { //Concatenate half of it bufKeyCD56[i][j] = bufKeyC16[i][j]; } leftshift(bufKeyD16[i-1],1,bufKeyD16[i]); for(j=0;j<28;j++) { //Concatenate the other half bufKeyCD56[i][j+28] = bufKeyD16[i][j]; } } else { leftshift(bufKeyC16[i-1],2,bufKeyC16[i]); for(j=0;j<28;j++) { //Concatenate half of it bufKeyCD56[i][j] = bufKeyC16[i][j]; } leftshift(bufKeyD16[i-1],2,bufKeyD16[i]); for(j=0;j<28;j++) { //Concatenate the other half bufKeyCD56[i][j+28] = bufKeyD16[i][j]; } } skipshifts: if(debug_mode) { printf("CD[%2d] - ",i); for(j=0;j<56;j++) { printf("%d",bufKeyCD56[i][j]); if(((j+1) % 7) == 0) { printf(" "); } } printf("\n"); } } //*********************** //Step 1.4: permutate CD[1-16] using table pc_2 //*********************** if(debug_mode) { printf("\nAfter permutating the key with table pc_2: \n"); } for(i=1;i<=16;i++) { permutate(bufKeyCD56[i],pt_pc2,pt_pc2_count,48,bufKeyCD48[i]); if(debug_mode) { printf("CD[%2d] - ",i); for(j=0;j<48;j++) { printf("%d",bufKeyCD48[i][j]); if(((j+1) % 6) == 0) { printf(" "); } } printf("\n"); } } //*********************** //End of Step 1 //We've completed all the changes to the key that we need to. //In step 2, we'll deal with the plaintext message //*********************** //********************************************** //Step 2: Encode the 64-bit plaintext data //********************************************** //*********************** //Step 2.1: permutate the 64-bit plaintext message into //an encrypted 64-bit data message using table IP //*********************** permutate(bufMsgBin,pt_ip,pt_ip_count,64,bufMsgBinIP); if(debug_mode) { print_binary("After permutating the message with table IP: ",bufMsgBinIP,64,8,FALSE);} //*********************** //Step 2.3: split up the permutated message into a 32-bit left and right section //*********************** for(i=0;i<32;i++) { (do_encrypt) ? bufMsgBinL[0][i] = bufMsgBinIP[i] : bufMsgBinL[17][i] = bufMsgBinIP[i]; //We do 17 for decryption } for(i=32;i<64;i++) { (do_encrypt) ? bufMsgBinR[0][i-32] = bufMsgBinIP[i] : bufMsgBinR[17][i-32] = bufMsgBinIP[i]; //Decryption } if(debug_mode) { printf("After splitting the message into L and R: \n"); print_binary("L[0]",bufMsgBinL[0],32,8,FALSE); print_binary("R[0]",bufMsgBinR[0],32,8,FALSE); } //*********************** //Step 2.4: Run through 16 des rounds //Note: This is the ONLY place where decryption is different than encryption //*********************** //Encrypting or decrypting? if (do_encrypt) { //Encrypting... for(i=1;i<=16;i++) { des_round(bufMsgBinL[i],bufMsgBinL[i-1],bufMsgBinR[i],bufMsgBinR[i-1],bufKeyCD48[i],i); } } else { //Decrypting...just do the reverse of encrypting for(i=16;i>=1;i--) { des_round(bufMsgBinL[i],bufMsgBinL[i+1],bufMsgBinR[i],bufMsgBinR[i+1],bufKeyCD48[i],(17-i)); } } //*********************** //Step 2.5: Concatenate L and R...but reverse the order, so it's RL instead of LR //*********************** for(i=0;i<17;i++) { for(j=0;j<32;j++) { bufMsgBinRL[i][j] = bufMsgBinR[i][j]; } for(j=0;j<32;j++) { bufMsgBinRL[i][j+32] = bufMsgBinL[i][j]; } } if(debug_mode) { printf("After concatenation of L and R in reverse order: \n"); print_binary("RL[16]",bufMsgBinRL[16],64,8,FALSE); } //*********************** //Step 2.6: permutate RL[16] using table ip_1 //*********************** (do_encrypt) ? permutate(bufMsgBinRL[16],pt_ip_1,pt_ip_1_count,64,buffer_out) : permutate(bufMsgBinRL[1],pt_ip_1,pt_ip_1_count,64,buffer_out); if(debug_mode) { print_binary("Final output",buffer_out,64,8,FALSE); } //The function that called this function should use buffer_out and parse it itself... //The output (buffer_out) is a 64-bit, binary variable } void des_round(int bufferL[], int prev_bufferL[], int bufferR[], int prev_bufferR[], int bufferK[], int round) { //We should be accepting L[i], R[i-1], and key[i] -- if we're decrypting, we'll taken R[i+1] int bufferR48[48], bufferXOR48[48], bufferS32[32], bufferP32[32]; int intRows, intCols, tempDec; //Variables for the s-tables int i; //*********************** //Step 3.1: L[i] = R[i-1] //*********************** for(i=0;i<32;i++) { bufferL[i] = prev_bufferR[i]; } //*********************** //Step 3.2: permutate R from 32-bits to 48-bits (matches key) using table ebit //*********************** permutate(prev_bufferR,pt_ebit,pt_ebit_count,48,bufferR48); //*********************** //Step 3.3: XOR bufferR48 and the key //*********************** for(i=0;i<48;i++) { bufferXOR48[i] = bufferR48[i] ^ bufferK[i]; } if(super_debug) { printf("After permutating R with ebit and XORing it with the key:"); print_binary("R[*]",bufferR48,48,6,FALSE); print_binary("K[*]",bufferK,48,6,FALSE); print_binary("R XOR K",bufferXOR48,48,6,FALSE); printf("\n"); } //************ //Substep 3.4 - Split the current 48-bit message into 6-bits, which uses tables S1 through S8 to convert into 8, 4-bit message //************ //Grab the first and sixth bits, convert them to decimal, then add them -- this equals our rows for(i=0;i<8;i++) { //Figure out the row and column of the s-box we'll use intRows = (bufferXOR48[6*i] *2) + bufferXOR48[6*i+5]; intCols = (bufferXOR48[6*i+1]*8) + (bufferXOR48[6*i+2]*4) + (bufferXOR48[6*i+3]*2) + (bufferXOR48[6*i+4]*1); //Find the new values and place them into tempDec, mess with them, then put them into bufMsgS32 tempDec = pt_s[i][intRows][intCols]; //Convert tempDec into binary and place it into bufMsgS32, four bits at a time //1st bit if (tempDec < 8) { (bufferS32[4*i] = 0); } else { bufferS32[4*i] = 1; tempDec = tempDec-8; } //2nd bit if (tempDec < 4) { (bufferS32[4*i+1] = 0); } else { bufferS32[4*i+1] = 1; tempDec = tempDec-4; } //3rd bit if (tempDec < 2) { (bufferS32[4*i+2] = 0); } else { bufferS32[4*i+2] = 1; tempDec = tempDec-2; } //4th bit if (tempDec == 1) { (bufferS32[4*i+3] = 1); } else { bufferS32[4*i+3] = 0;} } if(super_debug) { printf("After permutating with X boxes into 32-bits:"); print_binary("R[32-bit]",bufferS32,32,4,FALSE); printf("\n"); } //************ //Substep 3.5 - Permutate the 32-bit message using table P //************ permutate(bufferS32,pt_p,pt_p_count,32,bufferP32); if(super_debug) { printf("After permutating with table P:"); print_binary("R=",bufferP32,32,8,TRUE); printf("\n"); } //************ //Substep 3.6 - XOR bufferP32 with prev_bufferL to return the bufferR value //************ for(i=0;i<32;i++) { bufferR[i] = prev_bufferL[i] ^ bufferP32[i]; } if(super_debug) { printf("After XORing bufferP32 with bufferL[i-1]"); print_binary("R=",bufferR,32,8,TRUE); printf("\n"); } if(debug_mode) { printf("\nRound %d Summary",round); print_binary("L[i] = ",bufferL,32,8,TRUE); print_binary("R[i] = ",bufferR,32,8,TRUE); } } void permutate(int buffer[], int pt_list[], int pt_list_count, int outbits, int buffer_out[]) { int i, newpos; //reset all bits of buffer_out to 0's for(i=0;i47) && (digit<58) ) { //it's a number...convert it to 0-9 rather than 48-57 (ascii) digit = digit - 48; } //Turn A-F into 10-15 switch ( digit) { case 'A': case 'a': digit = 10; break; case 'B': case 'b': digit = 11; break; case 'C': case 'c': digit = 12; break; case 'D': case 'd': digit = 13; break; case 'E': case 'e': digit = 14; break; case 'F': case 'f': digit = 15; break; } //All that's left is to convert the hex numbers into binary digits (4 binary per 1 hex) //First bit if (digit >= 8) { buffer_bin[curarray] = 1; digit = digit -8; } else { buffer_bin[curarray] = 0; } curarray++; //Second bit if (digit >= 4) { buffer_bin[curarray] = 1; digit = digit -4; } else { buffer_bin[curarray] = 0; } curarray++; //Third bit if (digit >= 2) { buffer_bin[curarray] = 1; digit = digit -2; } else { buffer_bin[curarray] = 0; } curarray++; //Fourth bit if (digit == 1) { buffer_bin[curarray] = 1; } else { buffer_bin[curarray] = 0; } curarray++; } //next j } void chr2bin(char buffer_ascii[], int buffer_bin[]) { int i, j, k, temp[8], highpower, leading0, curarray=0; char letter; for(j=0;j<8;j++) { //cycle through buffer_ascii k=0; letter = buffer_ascii[j]; //we'll work on 'letter' for a while //find the highest possible power for(i=0;;i++) { if(pow(2,i) > letter) { highpower = i; break; } } for(i=highpower;i!=-1;i--) { if(letter>=pow(2,i)) { letter = letter - pow(2,i); temp[k] = 1; } else { temp[k] = 0; } k++; } leading0 = 8 - k; //Put on the number of leading zeros we need to... for(k=0;k