5.4 服务器端核心代码
http://www.paper51.com
图6 服务器完成初始化 http://www.paper51.com 图6为服务器初始化过程和为已登录用户服务的过程。服务器先生成RSA算法所需的公钥和私钥,当连接到来时服务器送出RSA公钥并等待用户回传DES密钥。当用户传回DES密钥后,服务器用RSA私钥解密得到DES密钥。此后用户登录信息由此DES密钥解密,用户发送的消息由服务器转发。 copyright paper51.com l 线程函数,为登陆客户服务 内容来自论文无忧网 www.paper51.com
void service(int connfd, RSA rsa) 内容来自论文无忧网 www.paper51.com { 内容来自论文无忧网 www.paper51.com
charbuf[MAXLINE]; 内容来自论文无忧网 www.paper51.com user_login*login; 内容来自论文无忧网 www.paper51.com
int n,check; 内容来自www.paper51.com
unsignedlong long key; http://www.paper51.com publickeypk; paper51.com pk.e =rsa.e; http://www.paper51.com
pk.n =rsa.n; paper51.com send_rsa_publickey(connfd, pk); //发送RSA公钥 paper51.com
cout<< "公钥已送出!" << endl; 内容来自www.paper51.com cerr<< "正在获取des密钥,请稍后...................................." << ends; http://www.paper51.com key =getdeskey (connfd, rsa); //获取DES私钥 paper51.com cout<< "ok" << endl; copyright paper51.com
if (key ==0) http://www.paper51.com return; 内容来自论文无忧网 www.paper51.com
if ((n =readn (connfd, (void *)buf, MAXLINE)) < 0) //从套接口上读取用户登陆信息 http://www.paper51.com { paper51.com cout<< "Read from socket connfd false! message: " << strerror(errno) << endl; copyright paper51.com exit(ERROR); paper51.com
} copyright paper51.com
else if (n== 0) copyright paper51.com { paper51.com cout<< "The client close socket." << endl; paper51.com
return; copyright paper51.com } 内容来自www.paper51.com
login =(user_login *)buf; paper51.com DES_Data*data = new DES_Data ((void *)login->password, 16, key); http://www.paper51.com bcopy((void *)data->Decrypting (), (void *)login->password, 11); //解密用户登陆口令 paper51.com cout<< "No." << login->user_id << "请求登录..." << endl; 内容来自www.paper51.com if (!(check= Attestation (connfd, login))) //认证用户登陆消息 paper51.com { copyright paper51.com
cout<< "Wrong user name or wrong password!" << endl; http://www.paper51.com cout<< "登录失败!" << endl; copyright paper51.com
return; 内容来自www.paper51.com } 内容来自www.paper51.com else if(check < 0) http://www.paper51.com return; copyright paper51.com cout<< "登录成功!" << endl; http://www.paper51.com
InsertOnline(connfd, login->user_id, key); //插入在线用户表 paper51.com if(GetFriends (connfd, login->user_id) < 0) //获得好友记录 http://www.paper51.com return; paper51.com
if(GetMessages (connfd, login->user_id, key) < 0) //获取离线消息 内容来自论文无忧网 www.paper51.com
return; 内容来自www.paper51.com
ServFreeChat(connfd, login->user_id, key); //服务在线用户,负责转发消息 copyright paper51.com return; copyright paper51.com } 内容来自论文无忧网 www.paper51.com
l 发送RSA公钥 内容来自论文无忧网 www.paper51.com Statussend_rsa_publickey (int sockfd, publickey key) 内容来自www.paper51.com { //向客户端发送RSA公钥 copyright paper51.com
charbuf[MAXLINE]; 内容来自论文无忧网 www.paper51.com
int n; http://www.paper51.com bcopy((void *)&key, (void *)buf, sizeof (key)); //将RSA公钥拷贝到发送缓冲区 http://www.paper51.com
if ((n =writen (sockfd, buf, MAXLINE)) < 0) //向套接口写公钥 http://www.paper51.com { http://www.paper51.com cout<< "Send public key to socket false! message: " << strerror(errno) << endl; copyright paper51.com
return(ERROR); paper51.com
} http://www.paper51.com return(OK); http://www.paper51.com } 内容来自论文无忧网 www.paper51.com l 获取DES密钥 http://www.paper51.com unsigned longlong getdeskey (int sockfd, RSA rsa) 内容来自www.paper51.com { //从客户端取得经RSA加密后的DES密钥并解密出来 copyright paper51.com int n; http://www.paper51.com charbuf[MAXLINE]; 内容来自www.paper51.com
unsignedlong long deskey; 内容来自www.paper51.com BigNum c,m; 内容来自www.paper51.com if ((n =readn (sockfd, buf, MAXLINE)) < 0) //从套接口读取RSA加密后的DES密钥 内容来自论文无忧网 www.paper51.com { 内容来自论文无忧网 www.paper51.com cout<< "Read from socket sockfd for get key false! message: "<< strerror (errno) << endl; paper51.com return(0); copyright paper51.com } copyright paper51.com
else if (n== 0) copyright paper51.com { 内容来自论文无忧网 www.paper51.com cout<< "The client close socket." << endl; paper51.com return(0); 内容来自论文无忧网 www.paper51.com } copyright paper51.com bcopy((void *)buf, (void *)&c, sizeof (c)); 内容来自www.paper51.com m =rsa.decrypting (c); //解密得DES密钥 内容来自www.paper51.com str_to_longlong(m.getnum (), deskey); //将字符串型的DES密钥转换为longlong型 paper51.com
return(deskey); 内容来自www.paper51.com
} 内容来自论文无忧网 www.paper51.com 5.5 客户端核心代码 http://www.paper51.com
内容来自论文无忧网 www.paper51.com
图7 两客户端正常通信 内容来自www.paper51.com 图7为经过DES正常加密解密的两客户端间的正常通信过程。 http://www.paper51.com
内容来自www.paper51.com 图8 无DES解密过程客户端与正常客户端通信 内容来自论文无忧网 www.paper51.com
图8左边为DES正常加密解密的客户端,右边为没有解密过程的客户端,故收到的信息为经DES算法加密过后的乱码。 http://www.paper51.com l 发送消息线程 内容来自论文无忧网 www.paper51.com void*Send_Message (void *arg) http://www.paper51.com { 内容来自www.paper51.com Messagemessage; paper51.com SockFriend*pf; copyright paper51.com charbuf[MAXLINE], *src; paper51.com int i = 0; copyright paper51.com pf =(SockFriend *) arg; 内容来自论文无忧网 www.paper51.com while (1) paper51.com { 内容来自论文无忧网 www.paper51.com
cout<< "Friends Online: "; 内容来自www.paper51.com PrintFriends(pf->friendonline); http://www.paper51.com
cout<< "Friends Offline: "; 内容来自论文无忧网 www.paper51.com PrintFriends(pf->friendoffline); paper51.com cout<< "Sendto >>>" << endl; 内容来自www.paper51.com
InputString(src); //输入要发送用户得id 内容来自论文无忧网 www.paper51.com Change_ston(src, message.friend_id); http://www.paper51.com
cout<< "Message: " << endl; copyright paper51.com InputString(src); //输入聊天信息 paper51.com DES_Data*data = new DES_Data ((void *)src, 296, pf->key); http://www.paper51.com bcopy((void *)data->Encrypting (), (void *)message.message, sizeof(message.message)); //将消息加密后送入 message字符串中 http://www.paper51.com cout<< endl; copyright paper51.com
deletedata; paper51.com
bcopy((void *)&message, (void *)buf, sizeof (message)); paper51.com if(writen (pf->sockfd, buf, MAXLINE) < 0) //发送消息 paper51.com { copyright paper51.com cout<< "Write message to pf.socfd false! message: " <<strerror (errno) << endl; copyright paper51.com exit(ERROR); copyright paper51.com } http://www.paper51.com } 内容来自论文无忧网 www.paper51.com } paper51.com l 接收消息线程 内容来自www.paper51.com void*Get_Message (void *arg) 内容来自论文无忧网 www.paper51.com { copyright paper51.com Messagemessage; http://www.paper51.com Friend*fri; 内容来自www.paper51.com SockFriend*pf; copyright paper51.com
charbuf[MAXLINE]; 内容来自www.paper51.com int n; 内容来自论文无忧网 www.paper51.com
DES_Data*data; copyright paper51.com
pf =(SockFriend *)arg; 内容来自论文无忧网 www.paper51.com
while ((n =readn (pf->sockfd, buf, MAXLINE)) >= 0) //从套接口上读取加密后的信息 内容来自www.paper51.com { copyright paper51.com if(n == 0) 内容来自www.paper51.com break; http://www.paper51.com bcopy((void *)buf, (void *)&message, sizeof (message)); copyright paper51.com
data= new DES_Data ((void *)message.message, 296, pf->key); 内容来自www.paper51.com bcopy((void *)data->Decrypting (), (void *)message.message, sizeof(message.message)); //将消息解密后送到message字符串中 内容来自论文无忧网 www.paper51.com fri= FindFriend (pf->friendonline, message.friend_id); paper51.com if(fri) paper51.com
cout<< "\n" << fri->friendname << "("<< fri->friend_id << ") say: "; //打印出发消息得好友名和id paper51.com else http://www.paper51.com { http://www.paper51.com fri= FindFriend (pf->friendoffline, message.friend_id); http://www.paper51.com if(fri) paper51.com cout<< "\n" << fri->friendname << "("<< fri->friend_id << ") said: "; http://www.paper51.com else paper51.com cout<< "\nSkyGod (" << message.friend_id << ")say: "; http://www.paper51.com
} copyright paper51.com cout<< message.message << "\nSend to >>>" <<endl; //打印出出接收到的消息 copyright paper51.com
} copyright paper51.com cout<< "The server must meet some problem!" << endl; copyright paper51.com close(pf->sockfd); 内容来自论文无忧网 www.paper51.com exit(ERROR); http://www.paper51.com } paper51.com |