H a r b i n I n s t i t u t e o f T e c h n o l o g y a t W e i h a i
密码学原理与应用 课程设计报告
设计题目: 密钥交换协议
班 级: 1204201 学 号: 120420128 姓 名: 王笑晗 验收日期: 2014 12 22
哈尔滨工业大学(威海)计算机学院
二零一二年一月
1. 密码算法原理
MD5加密算法 对MD5算法简要的叙述可以为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。
在MD5算法中,首先需要对信息进行填充,使其位长对512求余的结果等于448。因此,信息的位长(Bits Length )将被扩展至N*512+448,N 为一个非负整数,N 可以是零。填充的方法如下,在信息的后面填充一个1和无数个0,直到满足上面的条件时 才停止用0对信息的填充。然后,在这个结果后面附加一个以64位二进制表示的填充前信息长度。经过这两步的处理,信息的位长=N*512+448+64= (N+1)*512,即长度恰好是512的整数倍。这样做的原因是为满足后面处理中对信息长度的要求。总体流程如下图所示,表示第i 个分组,每次的运算都由前一轮的128位结果值和第i 块512bit 值进行运算。
2. 设计思想
密钥交换协议的原理是A 和B 双方互相交换彼此的密钥,A 给B a,B 给A b。然后A 就有了ab B 也有了ab ,通过对ab 就是他们的密钥。过程是简单的,为了保证明文ab 不被攻击者获得所以在传输期间需要使用加密算法对其进行加密。
3. 设计流程图
4. 功能实现
在两台电脑间的通信通过socket 来实现
Socket 又称" 套接字" ,应用程序通常通过" 套接字" 向网络发出请求或者应答网络请求。
以J2SDK-1.3为例,Socket 和ServerSocket 类库位于java.net 包中。 ServerSocket用于服务器
操作这个实例, 完成所需的会话。对于一个网络连接来说,套接字是平等的,并没有差别,不因为在服务器端或在客户端而产生不同级别。不管是Socket 还是 ServerSocket它们的工作都是通过SocketImpl 类及其子类完成的。
Socket 非常类似于电话插座。以一个国家级电话网为例,电话的通话双方相当于相互通信的2个进程,区号是它的网络地址;区内一个单位的交换机相当于一台主机, 主机分配给每个用户的局内号码相当于Socket 号。任何用户在通话之前,首先要占有一部电话机,相当于申请一个Socket ;同时要知道对方的号码,相 当于对方有一个固定的Socket 。然后向对方拨号呼叫,相当于发出连接请求(假如对方不在同一区内,还要拨对方区号,相当于给出网络地址)。假如对方在场并空闲(相当于通信的另一主机开 机且可以接受连接请求),拿起电话话筒,双方就可以正式通话,相当于连接成功。双方通话的过程,是一方向电话机发出信号和对方从电话机接收信号的过程,相 当于向Socket 发送数据和从socket 接收数据。通话结束后,一方挂起电话机相当于关闭Socket ,撤消连接。
在电话系统中,一般用户只能感受到本地电话机和对方电话号码的存在,建立通话的过程,话音传输的过程以及整个电话系统的技术细节对他都是透明的,这也与Socket 机制非常相似。Socket 利用网间网通信设施实现进程通信,但它对通信设施的细节毫不关心,只要通信设施能提供足够的通信能力,它就满足了。
至此,我们对Socket 进行了直观的描述。抽象出来,Socket 实质上提供了进程通信的端点。进程通信之前,双方首先必须各自创建一个端点,否则是没有办法建立联系并相互通信的。正如打电话之前,双方必须各自拥有一台电话机一样。
在网间网内部,每一个Socket 用一个半相关描述:(协议,本地地址,本地端口)。 一个完整的Socket 有一个本地唯一的Socket 号,由操作系统分配。
最重要的是,Socket 是面向客户/服务器模型而设计的,针对客户和服务器程序提供不同的Socket 系统调用。客户随机申请一个Socket (相当于一个想打电话的人可以在任何一台入网电话上拨号呼叫),系统为之分配一个Socket 号;服务器拥有全局公认的Socket ,任何客户都可以向它发出连接请求和信息请求(相当于一个被呼叫的电话拥有一个呼叫方知道的电话号码)。
Socket 利用客户/服务器模式巧妙地解决了进程之间建立通信连接的问题。服务器Socket 半相关为全局所公认非常重要。读者不妨考虑一下,两个完全随机的用户进程之间如何建立通信?假如通信双方没有任何一方的Socket 固定,就好比打电话的双方彼此不知道对方的电话号码,要通话是不可能的。
根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。
(1)服务器监听:是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。 (2)客户端请求:是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。
(3)连接确认:是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
5. 总结
主要是在实现了SOCKET 之后使用哪种算法进行加密的问题,DES 是对称密码算法,就是加密密
钥能够从解密密钥中推算出来,反过来也成立。密钥较短,加密处理简单,加解密速度快,适用于加密大量数据的场合。
RSA是非对称算法,加密密钥和解密密钥是不一样的,或者说不能由其中一个密钥推导出另一个密钥。密钥尺寸大,加解密速度慢,一般用来加密少量数据,比如DES 的密钥。
SHA1 和 MD5 是散列算法,将任意大小的数据映射到一个较小的、固定长度的唯一值。加密性强的散列一定是不可逆的,这就意味着通过散列结果,无法推出任何部分的
为雪崩效应。散列还应该是防冲突的,即找不出具有相同散列结果的两条信息。具有这些特性的散列结果就可以用于验证信息是否被修改。MD5 比 SHA1 大约快 33%。所以选择了MD5
6. 源代码
#pragma comment(lib,"ws2_32.lib") #include #include
void main() {
char strIp[255]; int iPort; /*
** 获得 IP & Port */
while(1) { cout>strIp; cout>iPort; /* ** 判断IP 地址是否合法 4 */ if (inet_addr(strIp)==INADDR_NONE) { cout65535) { cout
** 初始化套接字DLL */
WSADATA wsa;
if(WSAStartup(MAKEWORD(1,1),&wsa)) { cout
** 创建套接字 */
SOCKET skt;
if ((skt=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) { cout
sockaddr_in servAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_addr.S_un.S_addr=inet_addr(strIp); servAddr.sin_port=htons(iPort); /*
** 与服务器连接 */
if (connect(skt,(sockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR) { cout
** 与服务器连接 */
char buf[1000]; while (1) { cout>buf; //发送数据 if (send(skt,buf,sizeof(buf),0)==SOCKET_ERROR) { cout
** 清理套接字占用的资源 */
WSACleanup(); }
#pragma comment(lib,"ws2_32.lib") #include #include
void main() {
int iPort=100;
// /*
// ** 获得 IP & Port // */
// while(1) // { // cout>strIp; // cout>iPort; // // /* // ** 判断IP 地址是否合法 // */ // if (inet_addr(strIp)==INADDR_NONE) // { // cout
** 初始化套接字DLL */
WSADATA wsa;
if(WSAStartup(MAKEWORD(1,1),&wsa)) { cout
** 创建套接字 */
SOCKET skt;
if ((skt=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) { cout
sockaddr_in servAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); servAddr.sin_port=htons(iPort); /*
** 绑定 */
if (bind(skt,(sockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR) { cout
** 监听 */
if (listen(skt,SOMAXCONN)==SOCKET_ERROR) { cout
cout
SOCKET sktContact;//通信套接字 sockaddr_in clientAddr; //客户端地址 /*
** 接受连接 */
int len=sizeof(clientAddr);
if ((sktContact=accept(skt,(sockaddr*)&clientAddr,&len))==SOCKET_ERROR) { cout
cout
//通信
char buf[1000]; while (1) { int bytes; if ((bytes=recv(sktContact,buf,sizeof(buf),0))==SOCKET_ERROR) { cout>buf; if ((send(sktContact,buf,sizeof(buf),0))==SOCKET_ERROR) { cout
/*
** 清理套接字占用的资源 */
WSACleanup();
} #include "md5.h"
/* Constants for MD5Transform routine. */
#define S11 7 #define S12 12 #define S13 17 #define S14 22 #define S21 5 #define S22 9 #define S23 14 #define S24 20 #define S31 4 #define S32 11 #define S33 16 #define S34 23 #define S41 6 #define S42 10 #define S43 15 #define S44 21
/* F, G, H and I are basic MD5 functions. */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT rotates x left n bits. */
#define ROTATE_LEFT(x, n) (((x) > (32-(n)))) /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */
#define FF(a, b, c, d, x, s, ac) { /
(a) += F ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
#define GG(a, b, c, d, x, s, ac) { /
(a) += G ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); /
#define HH(a, b, c, d, x, s, ac) { /
(a) += H ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
#define II(a, b, c, d, x, s, ac) { /
(a) += I ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
MD5_CTX::MD5_CTX() {
MD5Init (); }
MD5_CTX::~MD5_CTX() { }
void MD5_CTX::MD5Init () {
this->count[0] = this->count[1] = 0; /* Load magic initialization constants.*/ this->state[0] = 0x67452301; this->state[1] = 0xefcdab89; this->state[2] = 0x98badcfe; this->state[3] = 0x10325476; /* Add by Liguangyi */
MD5_memset(PADDING, 0, sizeof(PADDING)); *PADDING=0x80; //PADDING = {
// 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; }
/* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */
void MD5_CTX::MD5Update (unsigned char *input,unsigned int inputLen) {
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int)((this->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((this->count[0] += ((unsigned long int)inputLen count[1]++;
this->count[1] += ((unsigned long int)inputLen >> 29);
partLen = 64 - index;
/* Transform as many times as possible. */
if (inputLen >= partLen) {
MD5_memcpy((unsigned char*)&this->buffer[index], (unsigned char*)input, partLen);
MD5Transform (this->state, this->buffer);
for (i = partLen; i + 63 state, &input[i]);
index = 0; } else i = 0;
/* Buffer remaining input */
MD5_memcpy ((unsigned char*)&this->buffer[index], (unsigned char*)&input[i], inputLen-i); }
/* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */
void MD5_CTX::MD5Final (unsigned char digest[16]) {
unsigned char bits[8];
unsigned int index, padLen;
/* Save number of bits */ Encode (bits, this->count, 8);
/* Pad out to 56 mod 64. */
index = (unsigned int)((this->count[0] >> 3) & 0x3f); padLen = (index
/* Append length (before padding) */ MD5Update (bits, 8); /* Store state in digest */
Encode (digest, this->state, 16);
/* Zeroize sensitive information.
MD5_memset ((unsigned char*)this, 0, sizeof (*this));
this->MD5Init();
}
/* MD5 basic transformation. Transforms state based on block.
*/
void MD5_CTX::MD5Transform (unsigned long int state[4], unsigned char block[64]) {
unsigned long int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode (x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information.
*/
MD5_memset ((unsigned char*)x, 0, sizeof (x));
}
/* Encodes input (unsigned long int) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Encode (unsigned char *output, unsigned long int *input,unsigned int len) {
unsigned int i, j;
for (i = 0, j = 0; j
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (unsigned long int). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Decode (unsigned long int *output, unsigned char *input, unsigned int len) {
unsigned int i, j;
for (i = 0, j = 0; j
output[i] = ((unsigned long int)input[j]) | (((unsigned long int)input[j+1])
/* Note: Replace "for loop" with standard memcpy if possible.
*/
void MD5_CTX::MD5_memcpy (unsigned char* output, unsigned char* input,unsigned int len)
{
unsigned int i;
for (i = 0; i
output[i] = input[i];
}
/* Note: Replace "f or loop" with standard memset if possible.
*/
void MD5_CTX::MD5_memset (unsigned char* output,int value,unsigned int len) {
unsigned int i;
for (i = 0; i
((char *)output)[i] = (char)value;
}
H a r b i n I n s t i t u t e o f T e c h n o l o g y a t W e i h a i
密码学原理与应用 课程设计报告
设计题目: 密钥交换协议
班 级: 1204201 学 号: 120420128 姓 名: 王笑晗 验收日期: 2014 12 22
哈尔滨工业大学(威海)计算机学院
二零一二年一月
1. 密码算法原理
MD5加密算法 对MD5算法简要的叙述可以为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。
在MD5算法中,首先需要对信息进行填充,使其位长对512求余的结果等于448。因此,信息的位长(Bits Length )将被扩展至N*512+448,N 为一个非负整数,N 可以是零。填充的方法如下,在信息的后面填充一个1和无数个0,直到满足上面的条件时 才停止用0对信息的填充。然后,在这个结果后面附加一个以64位二进制表示的填充前信息长度。经过这两步的处理,信息的位长=N*512+448+64= (N+1)*512,即长度恰好是512的整数倍。这样做的原因是为满足后面处理中对信息长度的要求。总体流程如下图所示,表示第i 个分组,每次的运算都由前一轮的128位结果值和第i 块512bit 值进行运算。
2. 设计思想
密钥交换协议的原理是A 和B 双方互相交换彼此的密钥,A 给B a,B 给A b。然后A 就有了ab B 也有了ab ,通过对ab 就是他们的密钥。过程是简单的,为了保证明文ab 不被攻击者获得所以在传输期间需要使用加密算法对其进行加密。
3. 设计流程图
4. 功能实现
在两台电脑间的通信通过socket 来实现
Socket 又称" 套接字" ,应用程序通常通过" 套接字" 向网络发出请求或者应答网络请求。
以J2SDK-1.3为例,Socket 和ServerSocket 类库位于java.net 包中。 ServerSocket用于服务器
操作这个实例, 完成所需的会话。对于一个网络连接来说,套接字是平等的,并没有差别,不因为在服务器端或在客户端而产生不同级别。不管是Socket 还是 ServerSocket它们的工作都是通过SocketImpl 类及其子类完成的。
Socket 非常类似于电话插座。以一个国家级电话网为例,电话的通话双方相当于相互通信的2个进程,区号是它的网络地址;区内一个单位的交换机相当于一台主机, 主机分配给每个用户的局内号码相当于Socket 号。任何用户在通话之前,首先要占有一部电话机,相当于申请一个Socket ;同时要知道对方的号码,相 当于对方有一个固定的Socket 。然后向对方拨号呼叫,相当于发出连接请求(假如对方不在同一区内,还要拨对方区号,相当于给出网络地址)。假如对方在场并空闲(相当于通信的另一主机开 机且可以接受连接请求),拿起电话话筒,双方就可以正式通话,相当于连接成功。双方通话的过程,是一方向电话机发出信号和对方从电话机接收信号的过程,相 当于向Socket 发送数据和从socket 接收数据。通话结束后,一方挂起电话机相当于关闭Socket ,撤消连接。
在电话系统中,一般用户只能感受到本地电话机和对方电话号码的存在,建立通话的过程,话音传输的过程以及整个电话系统的技术细节对他都是透明的,这也与Socket 机制非常相似。Socket 利用网间网通信设施实现进程通信,但它对通信设施的细节毫不关心,只要通信设施能提供足够的通信能力,它就满足了。
至此,我们对Socket 进行了直观的描述。抽象出来,Socket 实质上提供了进程通信的端点。进程通信之前,双方首先必须各自创建一个端点,否则是没有办法建立联系并相互通信的。正如打电话之前,双方必须各自拥有一台电话机一样。
在网间网内部,每一个Socket 用一个半相关描述:(协议,本地地址,本地端口)。 一个完整的Socket 有一个本地唯一的Socket 号,由操作系统分配。
最重要的是,Socket 是面向客户/服务器模型而设计的,针对客户和服务器程序提供不同的Socket 系统调用。客户随机申请一个Socket (相当于一个想打电话的人可以在任何一台入网电话上拨号呼叫),系统为之分配一个Socket 号;服务器拥有全局公认的Socket ,任何客户都可以向它发出连接请求和信息请求(相当于一个被呼叫的电话拥有一个呼叫方知道的电话号码)。
Socket 利用客户/服务器模式巧妙地解决了进程之间建立通信连接的问题。服务器Socket 半相关为全局所公认非常重要。读者不妨考虑一下,两个完全随机的用户进程之间如何建立通信?假如通信双方没有任何一方的Socket 固定,就好比打电话的双方彼此不知道对方的电话号码,要通话是不可能的。
根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。
(1)服务器监听:是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。 (2)客户端请求:是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。
(3)连接确认:是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
5. 总结
主要是在实现了SOCKET 之后使用哪种算法进行加密的问题,DES 是对称密码算法,就是加密密
钥能够从解密密钥中推算出来,反过来也成立。密钥较短,加密处理简单,加解密速度快,适用于加密大量数据的场合。
RSA是非对称算法,加密密钥和解密密钥是不一样的,或者说不能由其中一个密钥推导出另一个密钥。密钥尺寸大,加解密速度慢,一般用来加密少量数据,比如DES 的密钥。
SHA1 和 MD5 是散列算法,将任意大小的数据映射到一个较小的、固定长度的唯一值。加密性强的散列一定是不可逆的,这就意味着通过散列结果,无法推出任何部分的
为雪崩效应。散列还应该是防冲突的,即找不出具有相同散列结果的两条信息。具有这些特性的散列结果就可以用于验证信息是否被修改。MD5 比 SHA1 大约快 33%。所以选择了MD5
6. 源代码
#pragma comment(lib,"ws2_32.lib") #include #include
void main() {
char strIp[255]; int iPort; /*
** 获得 IP & Port */
while(1) { cout>strIp; cout>iPort; /* ** 判断IP 地址是否合法 4 */ if (inet_addr(strIp)==INADDR_NONE) { cout65535) { cout
** 初始化套接字DLL */
WSADATA wsa;
if(WSAStartup(MAKEWORD(1,1),&wsa)) { cout
** 创建套接字 */
SOCKET skt;
if ((skt=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) { cout
sockaddr_in servAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_addr.S_un.S_addr=inet_addr(strIp); servAddr.sin_port=htons(iPort); /*
** 与服务器连接 */
if (connect(skt,(sockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR) { cout
** 与服务器连接 */
char buf[1000]; while (1) { cout>buf; //发送数据 if (send(skt,buf,sizeof(buf),0)==SOCKET_ERROR) { cout
** 清理套接字占用的资源 */
WSACleanup(); }
#pragma comment(lib,"ws2_32.lib") #include #include
void main() {
int iPort=100;
// /*
// ** 获得 IP & Port // */
// while(1) // { // cout>strIp; // cout>iPort; // // /* // ** 判断IP 地址是否合法 // */ // if (inet_addr(strIp)==INADDR_NONE) // { // cout
** 初始化套接字DLL */
WSADATA wsa;
if(WSAStartup(MAKEWORD(1,1),&wsa)) { cout
** 创建套接字 */
SOCKET skt;
if ((skt=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) { cout
sockaddr_in servAddr;
servAddr.sin_family=AF_INET;
servAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); servAddr.sin_port=htons(iPort); /*
** 绑定 */
if (bind(skt,(sockaddr*)&servAddr,sizeof(servAddr))==SOCKET_ERROR) { cout
** 监听 */
if (listen(skt,SOMAXCONN)==SOCKET_ERROR) { cout
cout
SOCKET sktContact;//通信套接字 sockaddr_in clientAddr; //客户端地址 /*
** 接受连接 */
int len=sizeof(clientAddr);
if ((sktContact=accept(skt,(sockaddr*)&clientAddr,&len))==SOCKET_ERROR) { cout
cout
//通信
char buf[1000]; while (1) { int bytes; if ((bytes=recv(sktContact,buf,sizeof(buf),0))==SOCKET_ERROR) { cout>buf; if ((send(sktContact,buf,sizeof(buf),0))==SOCKET_ERROR) { cout
/*
** 清理套接字占用的资源 */
WSACleanup();
} #include "md5.h"
/* Constants for MD5Transform routine. */
#define S11 7 #define S12 12 #define S13 17 #define S14 22 #define S21 5 #define S22 9 #define S23 14 #define S24 20 #define S31 4 #define S32 11 #define S33 16 #define S34 23 #define S41 6 #define S42 10 #define S43 15 #define S44 21
/* F, G, H and I are basic MD5 functions. */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT rotates x left n bits. */
#define ROTATE_LEFT(x, n) (((x) > (32-(n)))) /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */
#define FF(a, b, c, d, x, s, ac) { /
(a) += F ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
#define GG(a, b, c, d, x, s, ac) { /
(a) += G ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); /
#define HH(a, b, c, d, x, s, ac) { /
(a) += H ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
#define II(a, b, c, d, x, s, ac) { /
(a) += I ((b), (c), (d)) + (x) + (unsigned long int)(ac); / (a) = ROTATE_LEFT ((a), (s)); / (a) += (b); / }
/* MD5 initialization. Begins an MD5 operation, writing a new context. */
MD5_CTX::MD5_CTX() {
MD5Init (); }
MD5_CTX::~MD5_CTX() { }
void MD5_CTX::MD5Init () {
this->count[0] = this->count[1] = 0; /* Load magic initialization constants.*/ this->state[0] = 0x67452301; this->state[1] = 0xefcdab89; this->state[2] = 0x98badcfe; this->state[3] = 0x10325476; /* Add by Liguangyi */
MD5_memset(PADDING, 0, sizeof(PADDING)); *PADDING=0x80; //PADDING = {
// 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; }
/* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */
void MD5_CTX::MD5Update (unsigned char *input,unsigned int inputLen) {
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int)((this->count[0] >> 3) & 0x3F);
/* Update number of bits */
if ((this->count[0] += ((unsigned long int)inputLen count[1]++;
this->count[1] += ((unsigned long int)inputLen >> 29);
partLen = 64 - index;
/* Transform as many times as possible. */
if (inputLen >= partLen) {
MD5_memcpy((unsigned char*)&this->buffer[index], (unsigned char*)input, partLen);
MD5Transform (this->state, this->buffer);
for (i = partLen; i + 63 state, &input[i]);
index = 0; } else i = 0;
/* Buffer remaining input */
MD5_memcpy ((unsigned char*)&this->buffer[index], (unsigned char*)&input[i], inputLen-i); }
/* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */
void MD5_CTX::MD5Final (unsigned char digest[16]) {
unsigned char bits[8];
unsigned int index, padLen;
/* Save number of bits */ Encode (bits, this->count, 8);
/* Pad out to 56 mod 64. */
index = (unsigned int)((this->count[0] >> 3) & 0x3f); padLen = (index
/* Append length (before padding) */ MD5Update (bits, 8); /* Store state in digest */
Encode (digest, this->state, 16);
/* Zeroize sensitive information.
MD5_memset ((unsigned char*)this, 0, sizeof (*this));
this->MD5Init();
}
/* MD5 basic transformation. Transforms state based on block.
*/
void MD5_CTX::MD5Transform (unsigned long int state[4], unsigned char block[64]) {
unsigned long int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode (x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
/* Zeroize sensitive information.
*/
MD5_memset ((unsigned char*)x, 0, sizeof (x));
}
/* Encodes input (unsigned long int) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Encode (unsigned char *output, unsigned long int *input,unsigned int len) {
unsigned int i, j;
for (i = 0, j = 0; j
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
/* Decodes input (unsigned char) into output (unsigned long int). Assumes len is
a multiple of 4.
*/
void MD5_CTX::Decode (unsigned long int *output, unsigned char *input, unsigned int len) {
unsigned int i, j;
for (i = 0, j = 0; j
output[i] = ((unsigned long int)input[j]) | (((unsigned long int)input[j+1])
/* Note: Replace "for loop" with standard memcpy if possible.
*/
void MD5_CTX::MD5_memcpy (unsigned char* output, unsigned char* input,unsigned int len)
{
unsigned int i;
for (i = 0; i
output[i] = input[i];
}
/* Note: Replace "f or loop" with standard memset if possible.
*/
void MD5_CTX::MD5_memset (unsigned char* output,int value,unsigned int len) {
unsigned int i;
for (i = 0; i
((char *)output)[i] = (char)value;
}