您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > socket+dh+cr4实现文件传输加密要点
socket+dh+cr4实现文件传输加密client.cpp//client.cpp:Definestheentrypointfortheconsoleapplication.//#includestdio.h#includeWinsock2.h#includestdlib.h#includestring.h//你用了winsock但没有链接相应的lib文件。加上这一句:#pragmacomment(lib,ws2_32.lib)#defineFNAME30voidInitial(char*);//初始化的函数voidCrypt();unsignedintS[256];//S数组voidnego(int*a);//初始化的函数intRun(intx,intr,intp,intt){inta,b,c;a=x;b=r;c=t;if(b==0){returnc;}if((b0)&&(b%2==0)){b=b/2;a=(a*a)%p;}else{b=b-1;c=(a*c)%p;}Run(a,b,p,c);}voidswap(char*s1,char*s2){chartemp;temp=*s1;*s1=*s2;*s2=temp;}voidre_S(char*S){inti;for(i=0;i256;i++)S[i]=i;}voidre_T(char*T,char*key){inti;intkeylen;keylen=strlen(key);for(i=0;i256;i++)T[i]=key[i%keylen];}voidre_Sbox(char*S,char*T){inti;intj=0;for(i=0;i256;i++){j=(j+S[i]+T[i])%256;swap(&S[i],&S[j]);}}voidRC4_1(FILE*readfile,FILE*writefile,char*key)//加密{charS[256]={0};charreadbuf[1];inti,j,t;charT[256]={0};re_S(S);re_T(T,key);re_Sbox(S,T);i=j=0;while(fread(readbuf,1,1,readfile)){i=(i+1)%256;j=(j+S[i])%256;swap(&S[i],&S[j]);t=(S[i]+(S[j]%256))%256;readbuf[0]=readbuf[0]^S[t];fwrite(readbuf,1,1,writefile);memset(readbuf,0,1);}}intmain(intargc,char*argv[]){printf(----客户端:启动-----\n);intx=0;printf(----说明:由客户端定义大素数和原根,客户端加密文件后发给服务器端,服务器短解密得到正确的消息----\n);printf(\n);printf(----发送大素数和原根给服务器端----\n);nego(&x);charkey[]=;FILE*file1,*file2;charfilePath1[50];printf(\n);printf(----开始文件传输----\n);printf(请输入要发送的文件路径:);scanf(%s,filePath1);printf(准备加密文件,请输入共享密钥:);scanf(%s,&key);file1=fopen(filePath1,r);file2=fopen(1.txt,w);RC4_1(file1,file2,key);fclose(file1);fclose(file2);printf(加密成功,准备发送给服务器端);WSADATAwsa;WSAStartup(MAKEWORD(2,2),&wsa);SOCKETsock=socket(AF_INET,SOCK_STREAM,0);if(sock==INVALID_SOCKET){printf(socketerror\n);return0;}SOCKADDR_INlocal;charserver[20];//printf(请输入IP地址:);//scanf(%s,server);memcpy(server,127.0.0.1,sizeof(127.0.0.1));charfilePath[50];strcpy(filePath,1.txt);HANDLEfp=CreateFile(filePath,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);while(fp==INVALID_HANDLE_VALUE){printf(文件路径错误,请重新输入:);scanf(%s,filePath);fp=CreateFile(filePath,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);}printf(%s\n,filePath);DWORDfileLen=GetFileSize(fp,&fileLen);printf(你选择的文件大小为:%u字节\n,fileLen);local.sin_addr.S_un.S_addr=inet_addr(server);local.sin_family=AF_INET;local.sin_port=htons(10000);if(connect(sock,(SOCKADDR*)&local,sizeof(SOCKADDR))==SOCKET_ERROR){printf(connecterror:%i\n,WSAGetLastError());return0;}char*buffer=newchar[fileLen];DWORDbufferLen=0;//读取文件到缓存中ReadFile(fp,buffer,fileLen,&bufferLen,NULL);boolisFirst=true;//第一次发送intsendLen=0;//实际发送数据的长度charsendBuffer[200];//发送帧intsendedLen=0;//当前已发送的文件长度intleftLen=fileLen;//当前剩下的文件长度while(true){if(isFirst){sendLen=send(sock,~,1,0);isFirst=false;if(sendLen0){printf(第一帧发送失败,程序结束\n);return0;}Sleep(1000);continue;}if((leftLen=0)||(sendedLen=fileLen))//剩下长度为0或当前已发送长度为文件长则退出break;if(leftLen=200){memcpy(sendBuffer,buffer+sendedLen,200);sendLen=send(sock,sendBuffer,200,0);if(sendLen0){printf(帧发送失败,程序结束\n);return0;}}else{memcpy(sendBuffer,buffer+sendedLen,leftLen);sendLen=send(sock,sendBuffer,leftLen,0);if(sendLen0){printf(帧发送失败,程序结束\n);return0;}Sleep(1000);send(sock,#,1,0);}sendedLen+=sendLen;leftLen=fileLen-sendedLen;printf(已经发送:%d字节\n,sendedLen);}if(sendedLen==fileLen){printf(文件发送成功\n);}elseprintf(文件发送失败\n);closesocket(sock);WSACleanup();delete[]buffer;return0;}voidnego(int*a){intt=1;intQ;intA;intXA1=0;longintXA2=1;intXB1=0;longintk=1;int*k1;WORDwVersionRequested;WSADATAwsaData;interr;SOCKETsockClient;SOCKADDR_INaddrSrv;charrecvBuf[50];wVersionRequested=MAKEWORD(1,1);err=WSAStartup(wVersionRequested,&wsaData);if(err!=0){printf(somethingerror!);}if(LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion)!=1){WSACleanup();}sockClient=socket(AF_INET,SOCK_STREAM,0);addrSrv.sin_addr.S_un.S_addr=inet_addr(127.0.0.1);addrSrv.sin_family=AF_INET;addrSrv.sin_port=htons(2120);connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));printf(请输入协商的素数:);charsu[10];gets(su);send(sockClient,su,strlen(su)+1,0);Q=atoi(su);printf(请输入协商的原根:);gets(su);send(sockClient,su,strlen(su)+1,0);A=atoi(su);printf(\n);printf(----输入自己的私钥并计算出公钥发给服务器端----\n);printf(请输入自己的私钥:);scanf(%d,&XA1);XA2=Run(A,XA1,Q,t);printf(发送自己的公钥%d给服务器端\n,XA2);sprintf(su,%d,XA2);send(sockClient,su,strlen(su)+1,0);printf(\n);printf(----等待服务器端的公钥----\n);recv(sockClient,recvBuf,50,0);*a=atoi(recvBuf);printf(得到服务器端公钥:%d\n,*a);k=Run(*a,XA1,Q,t);printf(\n);printf(----计算共享的秘密密钥----\n);printf(共享的秘密密钥KEY为%d\n,k);closesocket(sockClient);WSACleanup();}server.cpp//server.cpp:Definestheentrypointfortheconsoleapplication.//#includestdio.h#includewinsock2.h#includestring.h#includewindows.h#includestdlib.h#pragmacomment(lib,ws2_32.lib)voidnego(int*a,int*b,int*c);//初始化的函数voidswap(char*s1,char*s2){chartemp;temp=*s1;*s1=*s2;*s2=temp;}voidre_S(char*S){inti;for(i=0;i256;i++)S[i]=i;}voidre_T(char*T,char*key){inti;intkeylen;keylen=strlen(key);for(i=0;i256;i++)T[i]=key[i%keylen];}voidre_Sbox(char*S,char*T){inti;intj=0;for(i=0;i256;i++){j=(j+S[i]+T[i])%256;swap(&S[i
本文标题:socket+dh+cr4实现文件传输加密要点
链接地址:https://www.777doc.com/doc-5470548 .html