换IP-1表 const static char IPR_Table64 40848471555236331 386461454226230375451353216129 364441252206028353431151195927 3424210509175725 //E位选择表 static const char E_Table48 3212345456789 89101112617 1202122232425 24252627282928293031321 //P换位表 const static char P_Table32 281710 282414322739425 //PC1选位表 const static char PC1_Table56 57494042342618 102595143352736 6355473931233022 1466352820124 //PC2选位表 const static char PC2_Table48 14171124 232720132 4304051453348 444939563453464250362932 //左移位数表 const static char LOOP_Table16 1122222212222221 // S盒 const static char S_Box8416 // S1 14406125907 00612119538 4114831050 11314100613 //S2 97213120510 32011069115 01471110415 1671205149 //S3 1009146328 85141211151 1212510147 11043115212 //S4 72851112415 7212110149 10690121174 34511127214 //S5 21241710 14112124786 42111103014 6150910453 //S6 1211047511 10131401138 9140113116 43212950813 //S7 41121451061 14351221586 145680592 65142312 //S8 10931450127 2561101492 71141315358 21147410811 static bool SubKey1648// 16圈子密钥 void Des_Runchar Out8 char In8 bool Type static bool M64 Tmp32 Li ampM0 Ri ampM32 ByteToBitM In 64 TransformM M IP_Table 64 if Type ENCRYPT forint i0 ilt16 i memcpyTmp Ri 32 F_funcRi SubKeyi XorRi Li 32 memcpyLi Tmp 32 else forint i15 igt0 i-- 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_INETSOCK_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 bits //置换IP表 const static char IP_Table64 5850423426362820124 625446383022146645648403224168 57494143352719113 635635547393123157 //逆置换IP-1表 const static char IPR_Table64 40848471555236331 386461454226230375451353216129 364441252206028353431151195927 3424210509175725 //E位选择表 static const char E_Table48 3212345456789 89101112617 1202122232425 24252627282928293031321 //P换位表 const static char P_Table32 281710 282414322739425 //PC1选位表 const static char PC1_Table56 57494042342618 102595143352736 6355473931233022 1466352820124 //PC2选位表 const static char PC2_Table48 14171124 232720132 4304051453348 444939563453464250362932 //左移位数表 const static char LOOP_Table16 1122222212222221 // S盒 const static char S_Box8416 // S1 14406125907 00612119538 4114831050 11314100613 //S2 97213120510 32011069115 01471110415 1671205149 //S3 1009146328 8514121115.
上一篇:
STL源码剖析笔记
下一篇:
基于HTML5的摄影网站设计与实现(静态网页)参考资料