Linux下DES安全通信编程

des.h

1/*********************************************************************/ 2/*-文件名:des.h 3/*-功能: 实现DES加密算法的加密解密功能    for linux 4/*********************************************************************/ 5#define SUCCESS 1; 6typedef  int INT32; 7typedef  char INT8; 8typedef  unsigned char  ULONG8; 9typedef  unsigned short ULONG16; 10typedef  unsigned long  ULONG32; 11static const ULONG8 DESENCRY=11; 12static const ULONG8 DESDECRY=22; 13static const ULONG8  pc_first[64] = {/*初始置换IP*/ 14     58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4, 15     62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8, 16     57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3, 17     61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7 } ; 18static const ULONG8 pc_last[64] = { /*逆初始置换IP-1*/ 19  40,8,48,16,56,24,64,32, 39,7,47,15,55,23,63,31, 20  38,6,46,14,54,22,62,30, 37,5,45,13,53,21,61,29, 21  36,4,44,12,52,20,60,28, 35,3,43,11,51,19,59,27, 22  34,2,42,10,50,18,58,26, 33,1,41,9,49,17,57,25}; 23static const ULONG32  pc_by_bit[64] = { /*按位取值或赋值*/ 24   0x80000000L,0x40000000L,0x20000000L,0x10000000L, 0x8000000L,  25   0x4000000L, 0x2000000L, 0x1000000L, 0x800000L, 0x400000L, 26   0x200000L, 0x100000L,  0x80000L, 0x40000L, 0x20000L,0x10000L,  27   0x8000L, 0x4000L, 0x2000L, 0x1000L, 0x800L, 0x400L, 0x200L, 28   0x100L, 0x80L,0x40L,0x20L, 0x10L, 0x8L, 0x4L, 0x2L, 0x1L, 29   0x80000000L,0x40000000L,0x20000000L,0x10000000L, 0x8000000L, 30   0x4000000L, 0x2000000L, 0x1000000L, 0x800000L, 0x400000L, 31   0x200000L, 0x100000L,  0x80000L, 0x40000L, 0x20000L, 0x10000L,  32   0x8000L, 0x4000L, 0x2000L, 0x1000L, 0x800L, 0x400L, 0x200L,  33   0x100L, 0x80L, 0x40L,0x20L, 0x10L, 0x8L,  0x4L, 0x2L, 0x1L,   }; 34static const ULONG8 des_P[32] = {/*置换运算P*/ 35       16,7,20,21, 29,12,28,17, 1,15,23,26, 36       5,18,31,10, 2,8,24,14, 32,27,3,9, 37       19,13,30,6, 22,11,4,25}; 38static const ULONG8 des_E[48] = {/*数据扩展*/ 39        32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13, 40        12,13,14,15,16,17,16,17,18,19,20,21, 41        20,21,22,23,24,25,24,25,26,27,28,29, 42        28,29,30,31,32,1 }; 43static const ULONG8 des_S[8][64] = {/*数据压缩*/ 44  { 45    0xe,0x0,0x4,0xf,0xd,0x7,0x1,0x4,0x2,0xe,0xf,0x2,0xb, 46    0xd,0x8,0x1,0x3,0xa,0xa,0x6,0x6,0xc,0xc,0xb,0x5,0x9, 47    0x9,0x5,0x0,0x3,0x7,0x8,0x4,0xf,0x1,0xc,0xe,0x8,0x8, 48    0x2,0xd,0x4,0x6,0x9,0x2,0x1,0xb,0x7,0xf,0x5,0xc,0xb, 49    0x9,0x3,0x7,0xe,0x3,0xa,0xa,0x0,0x5,0x6,0x0,0xd   50  }, 51  {  52    0xf,0x3,0x1,0xd,0x8,0x4,0xe,0x7,0x6,0xf,0xb,0x2,0x3, 53    0x8,0x4,0xf,0x9,0xc,0x7,0x0,0x2,0x1,0xd,0xa,0xc,0x6, 54    0x0,0x9,0x5,0xb,0xa,0x5,0x0,0xd,0xe,0x8,0x7,0xa,0xb, 55    0x1,0xa,0x3,0x4,0xf,0xd,0x4,0x1,0x2,0x5,0xb,0x8,0x6, 56    0xc,0x7,0x6,0xc,0x9,0x0,0x3,0x5,0x2,0xe,0xf,0x9 57  }, 58  {  59    0xa,0xd,0x0,0x7,0x9,0x0,0xe,0x9,0x6,0x3,0x3,0x4,0xf, 60    0x6,0x5,0xa,0x1,0x2,0xd,0x8,0xc,0x5,0x7,0xe,0xb,0xc, 61    0x4,0xb,0x2,0xf,0x8,0x1,0xd,0x1,0x6,0xa,0x4,0xd,0x9, 62    0x0,0x8,0x6,0xf,0x9,0x3,0x8,0x0,0x7,0xb,0x4,0x1,0xf, 63    0x2,0xe,0xc,0x3,0x5,0xb,0xa,0x5,0xe,0x2,0x7,0xc                                           64  }, 65  {  66    0x7,0xd,0xd,0x8,0xe,0xb,0x3,0x5,0x0,0x6,0x6,0xf,0x9, 67    0x0,0xa,0x3,0x1,0x4,0x2,0x7,0x8,0x2,0x5,0xc,0xb,0x1, 68    0xc,0xa,0x4,0xe,0xf,0x9,0xa,0x3,0x6,0xf,0x9,0x0,0x0, 69    0x6,0xc,0xa,0xb,0xa,0x7,0xd,0xd,0x8,0xf,0x9,0x1,0x4, 70    0x3,0x5,0xe,0xb,0x5,0xc,0x2,0x7,0x8,0x2,0x4,0xe                          71  }, 72  {  73    0x2,0xe,0xc,0xb,0x4,0x2,0x1,0xc,0x7,0x4,0xa,0x7,0xb, 74    0xd,0x6,0x1,0x8,0x5,0x5,0x0,0x3,0xf,0xf,0xa,0xd,0x3, 75    0x0,0x9,0xe,0x8,0x9,0x6,0x4,0xb,0x2,0x8,0x1,0xc,0xb, 76    0x7,0xa,0x1,0xd,0xe,0x7,0x2,0x8,0xd,0xf,0x6,0x9,0xf, 77    0xc,0x0,0x5,0x9,0x6,0xa,0x3,0x4,0x0,0x5,0xe,0x3 78  }, 79  {  80    0xc,0xa,0x1,0xf,0xa,0x4,0xf,0x2,0x9,0x7,0x2,0xc,0x6, 81    0x9,0x8,0x5,0x0,0x6,0xd,0x1,0x3,0xd,0x4,0xe,0xe,0x0, 82    0x7,0xb,0x5,0x3,0xb,0x8,0x9,0x4,0xe,0x3,0xf,0x2,0x5, 83    0xc,0x2,0x9,0x8,0x5,0xc,0xf,0x3,0xa,0x7,0xb,0x0,0xe, 84    0x4,0x1,0xa,0x7,0x1,0x6,0xd,0x0,0xb,0x8,0x6,0xd 85  }, 86  {  87    0x4,0xd,0xb,0x0,0x2,0xb,0xe,0x7,0xf,0x4,0x0,0x9,0x8, 88    0x1,0xd,0xa,0x3,0xe,0xc,0x3,0x9,0x5,0x7,0xc,0x5,0x2, 89    0xa,0xf,0x6,0x8,0x1,0x6,0x1,0x6,0x4,0xb,0xb,0xd,0xd, 90    0x8,0xc,0x1,0x3,0x4,0x7,0xa,0xe,0x7,0xa,0x9,0xf,0x5, 91    0x6,0x0,0x8,0xf,0x0,0xe,0x5,0x2,0x9,0x3,0x2,0xc 92  }, 93  {  94    0xd,0x1,0x2,0xf,0x8,0xd,0x4,0x8,0x6,0xa,0xf,0x3,0xb, 95    0x7,0x1,0x4,0xa,0xc,0x9,0x5,0x3,0x6,0xe,0xb,0x5,0x0, 96    0x0,0xe,0xc,0x9,0x7,0x2,0x7,0x2,0xb,0x1,0x4,0xe,0x1, 97    0x7,0x9,0x4,0xc,0xa,0xe,0x8,0x2,0xd,0x0,0xf,0x6,0xc, 98    0xa,0x9,0xd,0x0,0xf,0x3,0x3,0x5,0x5,0x6,0x8,0xb 99  } }; 100static const ULONG8 keyleft[28] = {/*等分密钥*/ 101  57,49,41,33,25,17,9,1,58,50,42,34,26,18, 102  10,2,59,51,43,35,27,19,11,3,60,52,44,36}; 103   104static const ULONG8 keyright[28] = {/*等分密钥*/ 105  63,55,47,39,31,23,15,7,62,54,46,38,30,22, 106  14,6,61,53,45,37,29,21,13,5,28,20,12,4}; 107static const ULONG8 lefttable[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};/*密钥移位*/ 108static const ULONG8 keychoose[48] ={/*密钥选取*/ 109  14,17,11,24,1,5,3,28,15,6,21,10, 110  23,19,12,4,26,8,16,7,27,20,13,2, 111  41,52,31,37,47,55,30,40,51,45,33,48, 112  44,49,39,56,34,53,46,42,50,36,29,32}; 113static const ULONG32 wz_leftandtab[3] = {0x0 , 0x80000000 , 0xc0000000 } ; 114 115class CDesOperate 116{ 117private: 118 ULONG32 m_arrOutKey[16][2];/*输出的key*/ 119 ULONG32 m_arrBufKey[2];/*形成起始密钥*/ 120 INT32 MakeData(ULONG32  *left ,ULONG32  *right ,ULONG32 number)  121 { 122  INT32 j;  123  ULONG32 oldright = *right;          124  ULONG8 rexpbuf[8] = { 0} ; 125  ULONG32 datatmp = 0;         126  ULONG32 exdes_P[2] = { 0} ;                          127  //由32扩充至48位 128   129  for ( = 0 ; j < 48 ; j++) 130  { 131   /*两个32位,每个存放24位*/ 132   if ( < 24 ) 133   { 134    if ( *right&pc_by_bit[des_E[j]-1] ) 135    { 136     exdes_P[0] |= pc_by_bit[j] ; 137    }             138   }             139   else 140   { 141    if ( *right&pc_by_bit[des_E[j]-1] ) 142    { 143     exdes_P[1] |= pc_by_bit[j-24] ; 144    } 145   } 146  } 147   148  for ( = 0 ; j < 2 ; j++) 149  {             150   exdes_P[j] ^= m_arrOutKey[number][j] ; 151  }     152   153  /*由48->32*/       154   155  exdes_P[1] >>= 8 ; 156  rexpbuf[7] = (ULONG8) (exdes_P[1]&0x0000003fL) ; 157  exdes_P[1] >>= 6 ; 158  rexpbuf[6] = (ULONG8) (exdes_P[1]&0x0000003fL) ; 159  exdes_P[1] >>= 6 ; 160  rexpbuf[5] = (ULONG8) (exdes_P[1]&0x0000003fL) ; 161  exdes_P[1] >>= 6 ; 162  rexpbuf[4] = (ULONG8) (exdes_P[1]&0x0000003fL) ; 163  exdes_P[0]  >>=  8 ; 164  rexpbuf[3] = (ULONG8) (exdes_P[0]&0x0000003fL) ;      165  exdes_P[0] >>= 6 ; 166  rexpbuf[2] = (ULONG8) (exdes_P[0]&0x0000003fL) ; 167  exdes_P[0] >>= 6 ; 168  rexpbuf[1] = (ULONG8) (exdes_P[0]&0x0000003fL) ; 169  exdes_P[0] >>= 6 ; 170  rexpbuf[0] = (ULONG8) (exdes_P[0]&0x0000003fL) ;      171  exdes_P[0] = 0 ; 172  exdes_P[1] = 0 ; 173   174  /*由48-> 32*/ 175   176  *right = 0 ; 177  for ( = 0 ; j < 7 ; j++) 178  { 179   *right |= des_S[j][rexpbuf[j]] ; 180   *right <<= 4 ; 181  } 182  *right |= des_S[j][rexpbuf[j]] ; 183   184  /*又要换位了*/ 185   186  datatmp = 0; 187  for ( = 0 ; j < 32 ; j++) 188  { 189   if ( *right&pc_by_bit[des_P[j]-1] ) 190   { 191    datatmp |= pc_by_bit[j] ; 192   } 193  } 194  *right = datatmp ; 195   196  /*一轮结束收尾操作*/                 197   198  *right ^= *left;        199  *left = oldright; 200   201  return SUCCESS; 202 } 203 INT32 HandleData(ULONG32 *left , ULONG8 choice) 204 { 205  INT32  number = 0 ,= 0;    206  ULONG32 *right = &left[1] ; 207  ULONG32 tmp = 0;        208  ULONG32 tmpbuf[2] = { 0 };              209   210  /*第一次调整pc_first[64]*/ 211  for ( = 0 ; j < 64 ; j++) 212  { 213   if (< 32 )  214   { 215    if ( pc_first[j] > 32)/*属于right*/ 216    { 217     if ( *right&pc_by_bit[pc_first[j]-1] ) 218     { 219      tmpbuf[0] |= pc_by_bit[j] ; 220     } 221    } 222    else 223    { 224     if ( *left&pc_by_bit[pc_first[j]-1] ) 225     { 226      tmpbuf[0] |= pc_by_bit[j] ; 227     } 228    } 229   } 230   else 231   { 232    if ( pc_first[j] > 32)/*属于right*/ 233    { 234     if ( *right&pc_by_bit[pc_first[j]-1] ) 235     { 236      tmpbuf[1] |= pc_by_bit[j] ; 237     } 238    } 239    else 240    { 241     if ( *left&pc_by_bit[pc_first[j]-1] ) 242     { 243      tmpbuf[1] |= pc_by_bit[j] ; 244     } 245    } 246   } 247  } 248  *left  = tmpbuf[0]; 249  *right = tmpbuf[1]; 250  tmpbuf[0] = 0 ; 251  tmpbuf[1] = 0 ; 252   253  switch(choice) 254  { 255  case DESENCRY: 256   for ( number = 0 ; number < 16 ; number++) 257   {             258    MakeData( left , right , (ULONG32)number); 259   } 260   break; 261  case DESDECRY: 262   for (number=15;number>=0;number--) 263   {             264    MakeData( left , right ,(ULONG32)number); 265   } 266   break; 267  default: 268   break; 269  } 270   271  /*最后一轮操作不交换左右值*/ 272   273  tmp = *left ; 274  *left = *right ; 275  *right = tmp ;         276   277  /*最后一次调整pc_last[64]*/ 278   279  for ( = 0 ; j < 64 ; j++) 280  { 281   if (< 32 )  282   { 283    if ( pc_last[j] > 32)/*属于right*/ 284    { 285     if ( *right&pc_by_bit[pc_last[j]-1] ) 286     { 287      tmpbuf[0] |= pc_by_bit[j] ; 288     } 289    } 290    else 291    { 292     if ( *left&pc_by_bit[pc_last[j]-1] ) 293     { 294      tmpbuf[0] |= pc_by_bit[j] ; 295     } 296    } 297   } 298   else 299   { 300    if ( pc_last[j] > 32)/*属于right*/ 301    { 302     if ( *right&pc_by_bit[pc_last[j]-1] ) 303     { 304      tmpbuf[1] |= pc_by_bit[j] ; 305     } 306    } 307    else 308    { 309     if ( *left&pc_by_bit[pc_last[j]-1] ) 310     { 311      tmpbuf[1] |= pc_by_bit[j] ; 312     } 313    } 314   } 315  } 316   317  *left =  tmpbuf[0] ; 318  *right = tmpbuf[1]; 319   320  return SUCCESS; 321 } 322 INT32 MakeKey(  ULONG32 *keyleft,ULONG32 *keyright ,ULONG32 number) 323 { 324  ULONG32 tmpkey[2] ={0}; 325  ULONG32 *Ptmpkey = (ULONG32*)tmpkey;      326  ULONG32 *Poutkey = (ULONG32*)&m_arrOutKey[number];  327  INT32 j;         328  memset((ULONG8*)tmpkey,0,sizeof(tmpkey));           329  /*要最高的一位或两位*/ 330  *Ptmpkey = *keyleft&wz_leftandtab[lefttable[number]] ;            331  Ptmpkey[1] = *keyright&wz_leftandtab[lefttable[number]] ;               332  if ( lefttable[number] == 1) 333  { 334   *Ptmpkey >>= 27; 335   Ptmpkey[1] >>= 27; 336  } 337  else 338  { 339   *Ptmpkey >>= 26; 340   Ptmpkey[1] >>= 26;                     341  } 342  Ptmpkey[0] &= 0xfffffff0; 343  Ptmpkey[1] &= 0xfffffff0; 344  /*得到高位的值*/ 345  *keyleft <<= lefttable[number] ; 346  *keyright <<= lefttable[number] ; 347  *keyleft |= Ptmpkey[0] ; 348  *keyright |= Ptmpkey[1] ;             349  Ptmpkey[0] = 0; 350  Ptmpkey[1] = 0; 351      352  /*从56位中选出48位,3个16位*/ 353  for ( = 0 ; j < 48 ; j++) 354  { 355   if ( < 24 ) 356   { 357     358    if ( *keyleft&pc_by_bit[keychoose[j]-1]) 359    { 360     Poutkey[0] |= pc_by_bit[j] ; 361    }                    362   }             363    364   else /*j>=24*/ 365   {                    366    if ( *keyright&pc_by_bit[(keychoose[j]-28)]) 367    { 368     Poutkey[1] |= pc_by_bit[j-24] ; 369    }                    370   } 371  } 372  return SUCCESS; 373 } 374 375 INT32 MakeFirstKey( ULONG32 *keyP ) 376 { 377  ULONG32 key[2] = {0}; 378  ULONG32 *Pkey ; 379  ULONG32 *Pbufkey ; 380  INT32 j;  381  Pbufkey = (ULONG32*)m_arrBufKey ; 382  Pkey = (ULONG32*)key; 383   384  memset((ULONG8*)m_arrBufKey,0,sizeof(m_arrBufKey));     385  memcpy((ULONG8*)&key,(ULONG8*)keyP ,8) ;       386  memset((ULONG8*)m_arrOutKey,0,sizeof(m_arrOutKey)); 387  for(  j = 0 ; j < 28 ; j++) 388  { 389   if ( keyleft[j] > 32 )  390   { 391    if ( Pkey[1]&pc_by_bit[keyleft[j]-1] ) 392    { 393     Pbufkey[0] |= pc_by_bit[j] ; 394    } 395   } 396   else 397   { 398    if ( Pkey[0]&pc_by_bit[keyleft[j]-1] ) 399    { 400     Pbufkey[0] |= pc_by_bit[j] ; 401    } 402   } 403    404   if ( keyright[j] > 32 )  405   { 406    if ( Pkey[1]&pc_by_bit[keyright[j]-1] ) 407    { 408     Pbufkey[1] |= pc_by_bit[j]; 409    } 410   } 411   else 412   { 413    if ( Pkey[0]&pc_by_bit[keyright[j]-1] ) 414    { 415     Pbufkey[1] |= pc_by_bit[j]; 416    } 417   } 418  } 419  for (= 0 ; j < 16 ; j++) 420  { 421   MakeKey(&Pbufkey[0],&Pbufkey[1] , j ) ; 422  } 423  return SUCCESS; 424 } 425 426  427public: 428 CDesOperate() 429 { 430  for(int i =0 ;i<2;i++) 431  { 432   for(int j = 0;j<16;j++) 433   { 434    this->m_arrOutKey[j][i] = 0; 435   } 436   this->m_arrBufKey[i] = 0; 437  } 438 } 439 INT32 Encry(char* pPlaintext,int nPlaintextLength,char *pCipherBuffer,int &nCipherBufferLength, char *pKey,int nKeyLength) 440 { 441  if(nKeyLength != 8) 442  { 443   return 0; 444  } 445  MakeFirstKey((ULONG32 *)pKey); 446  int nLenthofLong = ((nPlaintextLength+7)/8)*2; 447  if(nCipherBufferLength<nLenthofLong*4) 448  {//out put buffer is not enough 449   nCipherBufferLength=nLenthofLong*4; 450   return 0; 451  } 452  memset(pCipherBuffer,0,nCipherBufferLength); 453  ULONG32 *pOutPutSpace = (ULONG32 *)pCipherBuffer; 454  ULONG32 *pSource; 455  if(nPlaintextLength != sizeof(ULONG32)*nLenthofLong) 456  { 457   pSource = new ULONG32[nLenthofLong]; 458   memset(pSource,0,sizeof(ULONG32)*nLenthofLong); 459   memcpy(pSource,pPlaintext,nPlaintextLength); 460  } 461  else 462  { 463   pSource = (ULONG32 *)pPlaintext; 464  } 465  ULONG32 gp_msg[2] = {0,0}; 466  for (int i=0;i<(nLenthofLong/2);i++) 467  { 468   gp_msg[0] = pSource[2*i]; 469   gp_msg[1] =pSource[2*i+1]; 470   HandleData(gp_msg,DESENCRY); 471   pOutPutSpace[2*i] = gp_msg[0]; 472   pOutPutSpace[2*i+1] = gp_msg[1]; 473  } 474  if(pPlaintext!=(char *)pSource) 475  { 476   delete []pSource; 477  } 478      479  return SUCCESS; 480 } 481 INT32 Decry(char* pCipher,int nCipherBufferLength,char *pPlaintextBuffer, int &nPlaintextBufferLength, char *pKey,int nKeyLength) 482 { 483  if(nCipherBufferLength%8!=0) 484  { 485   return 0; 486  } 487  if(nPlaintextBufferLength<nCipherBufferLength) 488  { 489   nPlaintextBufferLength = nCipherBufferLength; 490   return 0; 491  } 492  if(nKeyLength != 8) 493  { 494   return 0; 495  } 496  MakeFirstKey((ULONG32 *)pKey); 497  memset(pPlaintextBuffer,0,nPlaintextBufferLength); 498  ULONG32 *pSouce = (ULONG32 *)pCipher; 499  ULONG32 *pDest = (ULONG32 *)pPlaintextBuffer; 500  ULONG32 gp_msg[2] = {0,0}; 501  for (int i=0;i<(nCipherBufferLength/8);i++) 502  { 503   gp_msg[0] = pSouce[2*i]; 504   gp_msg[1] = pSouce[2*i+1]; 505   HandleData(gp_msg,DESDECRY); 506   pDest[2*i] = gp_msg[0]; 507   pDest[2*i+1] = gp_msg[1]; 508  } 509  return SUCCESS; 510 } 511};

chat.cpp

1#include <stdio.h> 2#include <netdb.h> 3#include <unistd.h> 4#include <iostream> 5#include <stdlib.h> 6#include <errno.h> 7#include <net/if.h> 8#include <string.h> 9#include <sys/types.h> 10#include <netinet/in.h> 11#include <sys/socket.h> 12#include <sys/wait.h> 13#include <arpa/inet.h>  14#include <memory.h> 15#include <sys/ioctl.h> 16#include "des.h" 17using namespace std; 18#define SERVERPORT 9527 19#define BUFFERSIZE 32  20char strStdinBuffer[BUFFERSIZE]; 21char strSocketBuffer[BUFFERSIZE]; 22char strEncryBuffer[BUFFERSIZE]; 23char strDecryBuffer[BUFFERSIZE]; 24void SecretChat(int nSock,char *pRemoteName, char *pKey) 25{ 26 CDesOperate cDes; 27 if(strlen(pKey)!=8) 28 { 29  printf("Key length error"); 30  return ; 31 } 32  33 pid_t nPid; 34 nPid = fork(); 35 if(nPid != 0) 36 { 37  while(1) 38  { 39   bzero(&strSocketBuffer, BUFFERSIZE); 40   int nLength = 0; 41   nLength = recv(nSock, strSocketBuffer,BUFFERSIZE,0); 42   if(nLength !=BUFFERSIZE) 43   { 44    break; 45   } 46   else 47   { 48    int nLen = BUFFERSIZE; 49    cDes.Decry(strSocketBuffer,BUFFERSIZE,strDecryBuffer,nLen,pKey,8); 50    strDecryBuffer[BUFFERSIZE-1]=0; 51    if(strDecryBuffer[0]!=0&&strDecryBuffer[0]!='\n') 52    { 53     printf("Receive message form <%s>: %s\n", pRemoteName,strDecryBuffer); 54     if(0==memcmp("quit",strDecryBuffer,4)) 55     { 56      printf("Quit!\n"); 57      break; 58     } 59    } 60   } 61  } 62 } 63 else 64 {  65  while(1) 66  { 67   bzero(&strStdinBuffer, BUFFERSIZE); 68   while(strStdinBuffer[0]==0) 69   { 70    if (fgets(strStdinBuffer, BUFFERSIZE, stdin) == NULL)  71    { 72     continue; 73    } 74   }       75   int nLen = BUFFERSIZE; 76   cDes.Encry(strStdinBuffer,BUFFERSIZE,strEncryBuffer,nLen,pKey,8); 77   if(send(nSock, strEncryBuffer, BUFFERSIZE,0)!=BUFFERSIZE) 78   { 79    perror("send"); 80   } 81   else 82   { 83    if(0==memcmp("quit",strStdinBuffer,4)) 84    { 85     printf("Quit!\n"); 86     break; 87    } 88   } 89  } 90 }   91} 92int main(int argc,char * []) 93{ 94 printf("Client or Server?\r\n"); 95 cin>>strStdinBuffer; 96 if(strStdinBuffer[0]=='c'||strStdinBuffer[0]=='C') 97 {//be a client 98  char strIpAddr[16]; 99  printf("Please input the server address:\r\n"); 100  cin>>strIpAddr; 101  int nConnectSocket, nLength; 102  struct sockaddr_in sDestAddr; 103  if ((nConnectSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)  104  { 105   perror("Socket"); 106   exit(errno); 107  } 108  bzero(&sDestAddr, sizeof(sDestAddr)); 109  sDestAddr.sin_family = AF_INET; 110  sDestAddr.sin_port = htons(SERVERPORT); 111  sDestAddr.sin_addr.s_addr = inet_addr(strIpAddr); 112          /* 连接服务器 */ 113  if (connect(nConnectSocket, (struct sockaddr *) &sDestAddr, sizeof(sDestAddr)) != 0)  114  { 115   perror("Connect "); 116   exit(errno); 117  } 118  else 119  { 120   printf("Connect Success!  \nBegin to chat...\n"); 121   SecretChat(nConnectSocket,strIpAddr,"benbenmi"); 122  } 123        close(nConnectSocket);   124 } 125 else 126 {//be a server 127  int nListenSocket, nAcceptSocket; 128  socklen_t nLength = 0; 129  struct sockaddr_in sLocalAddr, sRemoteAddr; 130  if ((nListenSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1)  131  { 132   perror("socket"); 133   exit(1); 134  } 135   136  bzero(&sLocalAddr, sizeof(sLocalAddr)); 137  sLocalAddr.sin_family = PF_INET; 138  sLocalAddr.sin_port = htons(SERVERPORT); 139  sLocalAddr.sin_addr.s_addr = INADDR_ANY; 140   141  if (bind(nListenSocket, (struct sockaddr *) &sLocalAddr, sizeof(struct sockaddr))== -1) 142  { 143   perror("bind"); 144   exit(1); 145  } 146  if (listen(nListenSocket, 5) == -1)  147  { 148   perror("listen"); 149   exit(1); 150  } 151  printf("Listening...\n"); 152  nLength = sizeof(struct sockaddr); 153  if ((nAcceptSocket = accept(nListenSocket, (struct sockaddr *) &sRemoteAddr,&nLength)) == -1)  154  { 155   perror("accept"); 156   exit(errno); 157  }  158  else 159  { 160   close(nListenSocket); 161   printf("server: got connection from %s, port %d, socket %d\n",inet_ntoa(sRemoteAddr.sin_addr),ntohs(sRemoteAddr.sin_port), nAcceptSocket); 162   SecretChat(nAcceptSocket,inet_ntoa(sRemoteAddr.sin_addr),"benbenmi"); 163   close(nAcceptSocket); 164    165  } 166 } 167}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Linux下DES安全通信编程 - HelloWorld