memcpyTmp Li 32 F_funcLi SubKeyi XorLi Ri 32 memcpyRi Tmp 32 TransformM M IPR_Table 64 BitToByteOut M 64 void Des_SetKeyconst char Key8 static bool K64 KL ampK0 KR ampK28 ByteToBitK Key 64 TransformK K PC1_Table 56 forint i0 ilt16 i RotateLKL 28 LOOP_Tablei RotateLKR 28 LOOP_Tablei TransformSubKeyi K PC2_Table 48 void F_funcbool In32 const bool Ki48 static bool MR48 TransformMR In E_Table 48 XorMR Ki 48 S_funcIn MR TransformIn In P_Table 32 void S_funcbool Out32 const bool In48 forchar i0jk ilt8 iIn6Out4 j In0ltlt1 In5 k In1ltlt3 In2ltlt2 In3ltlt1 In4 ByteToBitOut ampS_Boxijk 4 void Transformbool Out bool In const char Table int len static bool Tmp256 forint i0 iltlen i Tmpi In Tablei-1 memcpyOut Tmp len void Xorbool InA const bool InB int len forint i0 iltlen i InAi InBi void RotateLbool In int len int loop static bool Tmp256 memcpyTmp In loop memcpyIn Inloop len-loop memcpyInlen-loop Tmp loop void ByteToBitbool Out const char In int bits forint i0 iltbits i Outi Ini/8gtgti8 amp 1 void BitToBytechar Out const bool In int bits memsetOut 0 bits7/8 forint i0 iltbits i Outi/8 Iniltlti8 //服务器 pragma commentlibquotws2_32.libquot include ltstdio.hgt include ltio.hgt include ltwinsock2.hgt include ltfcntl.hgt include ltsysstat.hgt include quotDES_pw.hquot //DES 加密和解密头文件 include quotstring.hquot include quotwindows.hquot int recv_fileSOCKET sock //接收文件函数 char buffer1024 0 char pBuffer1024quot解密后quot int name_lenj0 if recvsockchar ampname_lensizeofint0 lt 0 printfquot远程服务器断开quot return -1 if recvsockbuffername_len0lt 0 printfquot远程服务器断开quot return -1 Des_Runbuffer buffer DECRYPT //解密文件名 strcatpBufferbuffer FILE fp fopenbufferquotwbquot //密文文件 FILE fw fopenpBufferquotwbquot //解密后文件 if fpNULL printfquot文件打开失败nquot return -1 int recv_count 0 while recv_count recvsockbuffer10240gt0 fwritebuffer1recv_countfp forint i0iltrecv_counti8 Des_Runbufferi bufferi DECRYPT //每8个字节解密 fwritebuffer1recv_countfw //保存文件 fclosefp fclosefw printfquot文件接收完成成功保存至当前目录下nquot return 0 void main systemquotcolor F0quot char key819809172 //设置密钥 Des_SetKeykey void recv_msgSOCKET int ch WSADATA wsa_data //初始化套接字 WSAStartup0x0202ampwsa_data SOCKET s_sock s_sock socketAF_I
NETSOCK_STREAM0 sockaddr_in se_addr se_addr.sin_family AF_INET se_addr.sin_port htons1024 se_addr.sin_addr.S_un.S_addr INADDR_ANY binds_socksockaddrampse_addrsizeofsockaddr //服务器邦定 if listens_sock10lt0 int err GetLastError sockaddr_in from_addr int addr_len sizeoffrom_addr printfquot等侍接收信息nquot SOCKET temp_sock accepts_socksockaddr ampfrom_addrampaddr_len //等侍链接 char chuf10 do memsetchuf010 recvtemp_sockchuf100 chint chuf switchch case 1: printfquot接收信息nquot recv_msgtemp_sock break case 2: printfquot接收文件nquot recv_filetemp_sock break case 3: printfquot对方关闭了连接quot whilech3 void recv_msgSOCKET sock //接收信息函数 char rbuf1024 char gbuf1024 memsetrbuf01024 while1 memsetrbuf01024 memsetgbuf01024 int rByterecvsockrbuf10240 ifSOCKET_ERRORrByte closesocketsock break strcpygbufrbuf Des_Runrbuf rbuf DECRYPT ifrbuf0q break else printfquot 收到的信息是s quotgbuf printfquot解密后snquotrbuf ///服务器头文件 include quotmemory.hquot include quotstdio.hquot enum ENCRYPTDECRYPT// ENCRYPT:加密DECRYPT:解密 void Des_Runchar Out8 char In8 bool TypeENCRYPT // 设置密钥 void Des_SetKeyconst char Key8 static void F_funcbool In32 const bool Ki48// f 函数 static void S_funcbool Out32 const bool In48// S 盒代替 // 变换 static void Transformbool Out bool In const char Table int len static void Xorbool InA const bool InB int len// 异或 static void RotateLbool In int len int loop// 循环左移 // 字节组转换成位组 static void ByteToBitbool Out const char In int bits // 位组转换成字节组 static void BitToBytechar Out const bool In int