📄 des.c
字号:
i = ii; /* * Save the R array, * which will be the new L. */ for (j=0; j<32; j++) tempL[j] = R[j]; /* * Expand R to 48 bits using the E selector; * exclusive-or with the current key bits. */ for (j=0; j<48; j++) preS[j] = R[E[j]-1] ^ KS[i][j]; /* * The pre-select bits are now considered * in 8 groups of 6 bits each. * The 8 selection functions map these * 6-bit quantities into 4-bit quantities * and the results permuted * to make an f(R, K). * The indexing into the selection functions * is peculiar; it could be simplified by * rewriting the tables. */ for (j=0; j<8; j++) { t = 6*j; k = S[j][(preS[t+0]<<5)+ (preS[t+1]<<3)+ (preS[t+2]<<2)+ (preS[t+3]<<1)+ (preS[t+4]<<0)+ (preS[t+5]<<4)]; t = 4*j; f[t+0] = (k>>3)&01; f[t+1] = (k>>2)&01; f[t+2] = (k>>1)&01; f[t+3] = (k>>0)&01; } /* * The new R is L ^ f(R, K). * The f here has to be permuted first, though. */ for (j=0; j<32; j++) R[j] = L[j] ^ f[P[j]-1]; /* * Finally, the new L (the original R) * is copied back. */ for (j=0; j<32; j++) L[j] = tempL[j]; } /* * The output L and R are reversed. */ for (j=0; j<32; j++) { t = L[j]; L[j] = R[j]; R[j] = t; } /*============================================== */ for (j=32;j<64;j++) L[j] = R[j-32];/*============================================== */ /* * The final output * gets the inverse permutation of the very original. */ for (j=0; j<64; j++) block[j] = L[FP[j]-1]; return 0;}int expand(unsigned char* in, unsigned char* out){ int i,j; for (i=0;i<8;i++) { for (j=0;j<8;j++) { *out = (in[i] <<j) & 0x80; if (*out == 0x80) *out = 0x01; out++; } } return 0;}int compress(unsigned char* in, unsigned char* out){ int temp; int i,j; for(i=0;i<8;i++) { out[i] = 0; temp = 1; for (j=7;j>=0;j--) { out[i] = out[i] + ( in[i*8+j] * temp); temp *= 2; } } return 0;}void key_uncrypt_Asc(unsigned char* input,unsigned char* key){ AscToBcd(cdeskey,key,8); AscToBcd(cdesinput,input,8); key_uncrypt_Bcd(cdesinput,cdeskey);}int key_uncrypt_Bcd(unsigned char* input,unsigned char* key){ int i; unsigned char clear_txt[PINLEN]; unsigned char pin_block[PINLEN]; unsigned char key_block[PINLEN]; unsigned char bits[64]; memcpy(cdeskey, key, 8 ); memcpy(cdesinput, input, 8 ); for(i=0;i<8;i++) key_block[i]=cdeskey[i];/******* key_block[0] = 0x31; key_block[1] = 0x31; key_block[2] = 0x31; key_block[3] = 0x31; key_block[4] = 0x31; key_block[5] = 0x31; key_block[6] = 0x31; key_block[7] = 0x31;********/ /* get pan_block */ expand(key_block,bits); setkey(bits); /* set key *//***** pin[0] = 0x65; pin[1] = 0x5e; pin[2] = 0xa6; pin[3] = 0x28; pin[4] = 0xcf; pin[5] = 0x62; pin[6] = 0x58; pin[7] = 0x5f;*******/ expand(cdesinput,bits); /* expand to bit stream */ encrypt(bits,DESCRYPT); /* descrypt */ compress(bits,clear_txt); /* compress to 8 characters */ for (i=0;i<8;i++) pin_block[i] = clear_txt[i];/***** len=pin_block[0]&0x0f; for(i=0;i<len/2;i++) { cipher_txt[2*i]= ((pin_block[i+1]>>4)&0x0f)+'0'; cipher_txt[2*i+1]= (pin_block[i+1]&0x0f)+'0'; }*******/ memcpy(cdesoutput, pin_block, 8); cdesoutput[8] = '0'; BcdToAsc(cascoutput,cdesoutput,8); return 0;}void pan_encrypt_Bcd(unsigned char* input, unsigned char* pan,unsigned char* key){ int i,len; unsigned char clear_txt[8],cipher_txt[8]; unsigned char pin_block[8],pan_block[8]; unsigned char key_block[8]; unsigned char bits[64]; memcpy(cdeskey, key, 8 ); memcpy(cdesinput,input, 8 ); cdeskey[8] ='\0'; cdesinput[6] = '\0'; pan[16] = '\0'; // TRACE("input[%s] pan[%s] key[%s]\n",cdesinput,pan,cdeskey); for(i=0;i<8;i++) key_block[i]=cdeskey[i];/**** key_block[0] = 0x31; key_block[1] = 0x31; key_block[2] = 0x31; key_block[3] = 0x31; key_block[4] = 0x31; key_block[5] = 0x31; key_block[6] = 0x31; key_block[7] = 0x31;*****/ pan_block[0]=pan_block[1]='\0'; for (i=1;i<7;i++) pan_block[i+1] = ( (pan[2*i+1]-'0') <<4) | (pan[2*i+2]-'0') ; len=strlen((const char*) cdesinput);// if(len%2)// {// TRACE("密码不能为奇数..0!!\n");// } pin_block[0]=len; for(i=len/2+1;i<8;i++) pin_block[i] = 0xff; for ( i=0;i<len/2;i++) pin_block[i+1] = ((cdesinput[2*i]-'0')<<4) | (cdesinput[2*i+1]-'0'); for (i=0;i<8;i++) clear_txt[i] = pin_block[i] ^ pan_block[i]; expand(key_block,bits); setkey(bits); expand(clear_txt,bits); /* expand to bit stream */ encrypt(bits,ENCRYPT); /* encrypt */ compress(bits,cipher_txt); /* compress to 8 characters */ memcpy(cdesoutput, cipher_txt, 8); cdesoutput[8] = '0'; BcdToAsc(cascoutput,cdesoutput,8);} void pan_encrypt_Asc(unsigned char* input, unsigned char* pan,unsigned char* key){ AscToBcd(cdeskey,key,8); AscToBcd(cdesinput,input,8); pan_encrypt_Bcd(cdesinput, pan, cdeskey);} void pan_uncrypt_Bcd(unsigned char* input,unsigned char* pan,unsigned char* key){ int i,len; unsigned char clear_txt[PINLEN],cipher_txt[17]; unsigned char pin_block[PINLEN],pan_block[PINLEN]; unsigned char key_block[PINLEN]; unsigned char bits[64]; memcpy(cdeskey, key, 8 ); memcpy(cdesinput, input, 8 ); for(i=0;i<8;i++) key_block[i]=cdeskey[i]; /***** key_block[0] = 0x31; key_block[1] = 0x31; key_block[2] = 0x31; key_block[3] = 0x31; key_block[4] = 0x31; key_block[5] = 0x31; key_block[6] = 0x31; key_block[7] = 0x31; *****/ /* get pan_block */ pan_block[0]=pan_block[1]=0; for (i=1;i<7;i++) pan_block[i+1] = ( (pan[2*i+1]-'0') <<4) | (pan[2*i+2]-'0') ; expand(key_block,bits); setkey(bits); /* set key *//******** pin[0] = 0x65; pin[1] = 0x5e; pin[2] = 0xa6; pin[3] = 0x28; pin[4] = 0xcf; pin[5] = 0x62; pin[6] = 0x58; pin[7] = 0x5f;*********/ expand(cdesinput,bits); /* expand to bit stream */ encrypt(bits,DESCRYPT); /* descrypt */ compress(bits,clear_txt); /* compress to 8 characters */ for (i=0;i<8;i++) pin_block[i] = clear_txt[i] ^ pan_block[i]; /* get pin_block */ len=pin_block[0]&0x0f; for(i=0;i<len/2;i++) { cipher_txt[2*i]= ((pin_block[i+1]>>4)&0x0f)+'0'; cipher_txt[2*i+1]= (pin_block[i+1]&0x0f)+'0'; } memcpy(cdesoutput, cipher_txt, 8); cdesoutput[8] = '0'; BcdToAsc(cascoutput,cdesoutput,8); } void pan_uncrypt_Asc(unsigned char* input, unsigned char* pan,unsigned char* key){ AscToBcd(cdeskey,key,8); AscToBcd(cdesinput,input,8); pan_uncrypt_Bcd( cdesinput, pan, cdeskey);} int StringToMAC(unsigned char * string, unsigned char * MacKey,unsigned char * MAC){ int zeros; int t; int i = 0; int j = 0; unsigned char dataTemp[9]; unsigned char cToXO[9]; memset(cToXO,0x00,9); if((zeros=strlen((char *)string)%8)!=0) { return 1; } t=strlen((char *)string)/8; for(i=0;i<t;i++) { memcpy(dataTemp,&string[i*8],8); for(j=0;j<8;j++) { dataTemp[j]=dataTemp[j]^cToXO[j] ; dataTemp[8]='\0'; } key_encrypt_Bcd(dataTemp,MacKey); memcpy(cToXO,cdesoutput,9); } BcdToAsc(MAC,cToXO,8);// TRACE("MAC is %s\n",MAC); return 0;}void Des(unsigned char *pin, unsigned char *workkey, unsigned char *cipher_pin){ int i; unsigned char clear_txt[PINLEN],cipher_txt[PINLEN];// unsigned char pin_block[PINLEN],pan_block[PINLEN]; unsigned char key_block[PINLEN]; unsigned char bits[64]; for(i=0;i<8;i++) key_block[i]=workkey[i]; for (i=0;i<8;i++) clear_txt[i] = pin[i]; expand(key_block,bits); setkey(bits); expand(clear_txt,bits); /* expand to bit stream */ encrypt(bits,ENCRYPT); /* encrypt */ compress(bits,cipher_txt); /* compress to 8 characters */ memcpy(cipher_pin ,cipher_txt,8);}void Undes(unsigned char *pin, unsigned char *workkey, unsigned char *cipher_pin){ int i; unsigned char clear_txt[PINLEN]; unsigned char pin_block[PINLEN]; unsigned char key_block[PINLEN]; unsigned char bits[64]; for(i=0;i<8;i++) key_block[i]=workkey[i]; /* get pan_block */ expand(key_block,bits); setkey(bits); /* set key */ expand(pin,bits); /* expand to bit stream */ encrypt(bits,DESCRYPT); /* descrypt */ compress(bits,clear_txt); /* compress to 8 characters */ for (i=0;i<8;i++) pin_block[i] = clear_txt[i]; memcpy(cipher_pin ,pin_block, 8);}void TriDes(unsigned char *input, unsigned char *doublekey, unsigned char *output){ unsigned char left_key[9]; unsigned char right_key[9]; unsigned char tmpstr1[9], tmpstr2[9]; memcpy( left_key, doublekey, 8 ); memcpy( right_key, doublekey+8, 8 ); Des( input, left_key, tmpstr1 ); Undes( tmpstr1, right_key, tmpstr2 ); Des( tmpstr2, left_key, output );}//int main(int argc, char **argv)//{// return 0;//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -