资源描述
socket+dh+cr4实现文件传输加密client.cpp/ client.cpp : Defines the entry point for the console application. /#include #include #include#include/你用了 winsock但没有链接相应的lib文件。加上这一句: #pragma comment(lib, ws2_32.lib )#define FNAME 30void Initial(char *);初始化的函数void Crypt();unsigned int S256;/ S 数组void nego(int *a) ;/初始化的函数int Run(int x,int r,int p,int t) int a,b,c;a=x;b=r;c=t;if(b=0)return c;if(b0)&(b%2=0)b=b a=(a*a)%p; elseb=b-1;c=(a*c)%p;Run(a,b,p,c);void swap(char *s1,char *s2)char temp;temp=*s1;*s1=*s2;*s2=temp;void re_S(char *S).int i;for(i=0;i256;i+)Si=i;void re_T(char *T,char *key).int i;int keylen;keylen=strlen(key);for(i=0;i256;i+)Ti=keyi%keylen;void re_Sbox(char *S,char *T) .int i;int j=0;for(i=0;i256;i+)j=(j+Si+Ti)%256;swap(&Si,&Sj);void RC4_1(FILE *readfile,FILE *writefile,char *key)/ 力口密 .char S256=0;char readbuf1;int i,j,t;char T256=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 + Si) % 256;swap(&Si,&Sj);t = (Si + (Sj % 256) % 256;readbuf0=readbuf0ASt;fwrite(readbuf,1,1,writefile); memset(readbuf,0,1); int main(int argc, char* argv口)printf(-客户端:启动-n);int x=0;printf(-说明:由客户端定义大素数和原根,客户端加密文件后发给服务器 端,服务器短解密得到正确的消息-n);printf(n);printf(发送大素数和原根给服务器端n);nego(&x);char key=;FILE *file1,*file2;char filePath150;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(filel);fclose(file2);printf(加密成功,准备发送给服务器端);WSADATA wsa;WSAStartup(MAKEWORD(2,2),&wsa);SOCKET sock=socket(AF_INET,SOCK_STREAM,0); if(sock=INVALID_SOCKET)printf(socket errorn);return 0;SOCKADDR_IN local;char server20;/printf(请输入 IP 地址:);scanf(%s,server);memcpy(server,127.0.0.1,sizeof(127.0.0.1);char filePath50;strcpy(filePath, 1.txt);HANDLEfp=CreateFile(filePath,GENERIC_READ,0,NULL,OPEN_EXISTING ,FILE_ATTRI BUTE_NORMAL,NULL);while(fp=INVALID_HANDLE_V ALUE) printf(文件路径错误,请重新输入:”);scanf(%s,filePath);fp=CreateFile(filePath,GENERIC_READ,0,NULL,OPEN_EXISTING ,FILE_ATTRI BUTE_NORMAL,NULL);printf(%sn,filePath);DWORD fileLen=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(connect error:%in,WSAGetLastError(); return 0;char* buffer=new charfileLen;DWORD bufferLen=0;/读取文件到缓存中ReadFile(fp,buffer,fileLen,&bufferLen,NULL);bool isFirst=true;/第一次发送int sendLen=0;/实际发送数据的长度char sendBuffer200;/发送帧int sendedLen=0;/S前已发送的文件长度int leftLen=fileLen;/当前剩下的文件长度while(true)if(isFirst)sendLen=send(sock,1,0);isFirst=false;if(sendLen0) printf(第一帧发送失败,程序结束n);return 0;Sleep(1000);continue;if(leftLen=fileLen)/剩下长度为 0 或当前已发送长度 为文件长则退出break;if(leftLen=200)memcpy(sendBuffer,buffer+sendedLen,200);sendLen=send(sock,sendBuffer,200,0);if(sendLen0)printf(帧发送失败,程序结束n);return 0;elsememcpy(sendBuffer,buffer+sendedLen,leftLen); sendLen=send(sock,sendBuffer,leftLen,0);if(sendLen0)printf(帧发送失败,程序结束n);return 0;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;return 0;void nego(int *a)int t=1;int Q;int A;int XA1=0;long int XA2=1;int XB1=0;long int k=1;int *k1;WORD wVersionRequested;WSADATA wsaData;int err;SOCKET sockClient;SOCKADDR_IN addrSrv;char recvBuf50;wVersionRequested = MAKEWORD( 1,1);err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) printf(something error!);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(请输入协商的素数:);char su10;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(得到服务器端公钥:%dn,*a);k=Run(*a,XA1,Q,t);printf(n);printf(-计算共享的秘密密钥-n);printf(共享的秘密密钥KEY为肝卜);closesocket(sockClient);WSACleanup();server.cpp/ server.cpp : Defines the entry point for the console application./#include #include #include #include #include #pragma comment(lib, ws2_32.lib )void nego(int *a,int *b,int *c) ;/初始化的函数void swap(char *s1,char *s2)char temp;temp=*s1;*s1=*s2;*s2=temp;void re_S(char *S)int i;for(i=0;i256;i+)Si=i;void re_T(char *T,char *key).int i;int keylen;keylen=strlen(key);for(i=0;i256;i+)Ti=keyi%keylen;void re_Sbox(char *S,char *T)int i;int j=0;for(i=0;i0)&(b%2=0)b=ba=(a*a)%p; elseb=b-1;c=(a*c)%p; Run(a,b,p,c); int main(int argc, char* argv口)printf(服务器端:启动n);int x=0,y=0,z=0;printf(-说明:由客户端定义大素数和原根,客户端加密文件后发给服务器 端,服务器短解密得到正确的消息-n);printf(n);printf(-等待客户端客户端定义大素数和原根-n);nego(&x,&y,&z);WSADATA wsa;WSAStartup(MAKEWORD(2,2),&wsa);SOCKET sock=socket(AF_INET,SOCK_STREAM,0);if(sock=INVALID_SOCKET)printf(socket errorn); return 0;SOCKADDR_IN local;local.sin_addr.S_un.S_addr=INADDR_ANY;local.sin_family=AF_INET;local.sin_port=htons(10000);if(bind(sock,(SOCKADDR*)&local,sizeof(SOCKADDR)=SOCKET_ERROR) printf(%in,WSAGetLastError();return 0;if(listen(sock,5)=SOCKET_ERROR) printf(%in,WSAGetLastError();return 0;printf(n);printf(-等待客户端传输文件-n);SOCKADDR_IN client;SOCKET clientSock;int addrLen=sizeof(SOCKADDR);clientSock=accept(sock,(SOCKADDR*)&client,&addrLen); printf(welcome:%sn”,inet_ntoa(client.sin_addr);int recvLen=0;/收至ij的缓冲的长度int recvedLen=0;/当前已经收到的长度DWORD writeLen;/实际写入文件的长度int fileLen=0;/文件长度char* buffer;/文件缓冲char recvBuffer200;/ 接收缓冲区buffer=new char1024*1024*10;/接收字节数 =10MrecvLen=recv(clientSock,recvBuffer,200,0);if(recvBuffer0=)printf(开始接收文件n);memset(recvBuffer,0,200); while(true)recvLen=recv(clientSock,recvBuffer,200,0);if(recvLen=1)&(recvBuffer0=#)break;memcpy(buffer+recvedLen,recvBuffer,recvLen);recvedLen+=recvLen;printf(接收:dn,recvLen);printf(已经接收 %i 字节数据 n,recvedLen);HANDLEfp=CreateFile(1.txt,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIB UTE_NORMAL,NULL);WriteFile(fp,buffer,recvedLen,&writeLen,NULL);printf(写入文件长度:un,writeLen);CloseHandle(fp);delete口 buffer;WSACleanup();char key=;printf(准备解密文件:请输入共享密钥:”);scanf(%s”,&key);FILE *file1,*file2;file1= fopen(1.txt,r);file2 = fopen(2.txt,w);RC4_1(file1,file2,key);fclose(file1);fclose(file2);printf(解密成功,保存在server文件夹2.txt中)void nego(int *a,int *b,int *c)int t=1;int Q;int A;int XA1=0;long int XA2=1;int XB1=0;long int k=1;WORD wVersionRequested;WSADATA wsaData;int err;SOCKET sockSrv;SOCKADDR_IN addrSrv;int len;SOCKADDR_IN addrClient;char recvBuf50;wVersionRequested = MAKEWORD( 1,1);err = WSAStartup( wVersionRequested, &wsaData );if ( err != 0 ) printf(something error!);if ( LOBYTE( wsaData.wVersion ) != 1 |HIBYTE( wsaData.wVersion ) != 1 ) WSACleanup();sockSrv=socket(AF_INET,SOCK_STREAM,0);addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);addrSrv.sin_family=AF_INET;addrSrv.sin_port=htons(2120);bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR);listen(sockSrv,5);len=sizeof(SOCKADDR);while(1)SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); char su10;recv(sockConn,recvBuf,50,0);*a = atoi(recvBuf);printf(客户端定义的大素数:dn,*a);recv(sockConn,recvBuf,50,0);*b = atoi(recvBuf);printf(客户端定义的原根:dn,*b);printf(n);printf(-等待客户端的公钥-n);recv(sockConn,recvBuf,50,0);*c = atoi(recvBuf);printf(客户端的公钥:dn,*c);Q=*a;A=*b;XB1=*c;printf(n);printf(-输入自己的私钥并计算出公钥发给客户端-n);printf(请输入自己的私钥:);scanf(%d,&XA1);XA2=Run(A,XA1,Q,t);printf(发送自己的公钥 %d给对方n,XA2);sprintf(su,%d,XA2);send(sockConn,su,strlen(su)+1,0);k=Run(*c,XA1,Q,t);printf(n);printf(-计算共享的秘密密钥-n);printf(共享的秘密密钥KEY为肝卜);closesocket(sockConn);break;2.cpp#include #include #include #include #include#pragma comment(lib,ws2_32.lib)int Run(int x,int r,int p,int t) int a,b,c;a=x;b=r;c=t;if(b=0)return c;if(b0)&(b%2=0)b=b a=(a*a)%p; elseb=b-1;c=(a*c)%p;Run(a,b,p,c);int main(int argc, CHAR* argv)int t=1;int Q=11351;int A=5;int XA1=0;long int XA2=1;int XB1=0;int i;long int k=1;printf(请输入自己的私钥XA1:);scanf(%d,&XA1);XA2=Run(A,XA1,Q,t);printf(自己的公钥的公钥XA2:%d,XA2);printf(n);printf(请输入对方的公钥XB1:);scanf(%d,&XB1);k=Run(XB1,XA1,Q,t);printf(KEY 为卜); printf(n);
展开阅读全文