# 电信协同拨号器算法及示例

MD5算法

#include "stdafx.h"

class CMD5
{
public:
CMD5()
{

}
~CMD5()
{

}
public:
//每次操作中用到的四个非线性函数F,G,H,I
unsigned int F(unsigned int x, unsigned int y, unsigned int z)
{
return (x & y) | ((~x) & z);
}

unsigned int G(unsigned int x, unsigned int y, unsigned int z)
{
return (x & z) | (y & (~z));
}

unsigned int H(unsigned int x, unsigned int y, unsigned int z)
{
return x ^ y ^ z;
}

unsigned int I(unsigned int x, unsigned int y, unsigned int z)
{
return y^(x|(~z));
}

//循环左移
unsigned int RotateLeft(unsigned int x, unsigned int s)
{
unsigned int a, b;
a=x<<s;
b=x>>(32-s);
return a|b;
}

//t应该为t[i],其中1<=i<=64,t[i]=4294967296*abs(sin(i))的整数部分，i的单位是弧度(4294967296等于2的32次方)
void FF(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d, unsigned int &m, unsigned int s, unsigned int t)
{
a=b+RotateLeft((a+F(b, c, d)+m+t), s);
}

void GG(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d, unsigned int &m, unsigned int s, unsigned int t)
{
a=b+RotateLeft((a+G(b, c, d)+m+t), s);
}

void HH(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d, unsigned int &m, unsigned int s, unsigned int t)
{
a=b+RotateLeft((a+H(b, c, d)+m+t), s);
}

void II(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d, unsigned int &m, unsigned int s, unsigned int t)
{
a=b+RotateLeft((a+I(b, c, d)+m+t), s);
}

//定义算法的四轮循环运算
void Recurrence(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d, unsigned int *M)
{
//第一轮
FF(a,b,c,d,M[0],7,0xd76aa478);
FF(d,a,b,c,M[1],12,0xe8c7b756);
FF(c,d,a,b,M[2],17,0x242070db);
FF(b,c,d,a,M[3],22,0xc1bdceee);
FF(a,b,c,d,M[4],7,0xf57c0faf);
FF(d,a,b,c,M[5],12,0x4787c62a);
FF(c,d,a,b,M[6],17,0xa8304613);
FF(b,c,d,a,M[7],22,0xfd469501);
FF(a,b,c,d,M[8],7,0x698098d8);
FF(d,a,b,c,M[9],12,0x8b44f7af);
FF(c,d,a,b,M[10],17,0xffff5bb1);
FF(b,c,d,a,M[11],22,0x895cd7be);
FF(a,b,c,d,M[12],7,0x6b901122);
FF(d,a,b,c,M[13],12,0xfd987193);
FF(c,d,a,b,M[14],17,0xa679438e);
FF(b,c,d,a,M[15],22,0x49b40821);

//第二轮
GG(a,b,c,d,M[1],5,0xf61e2562);
GG(d,a,b,c,M[6],9,0xc040b340);
GG(c,d,a,b,M[11],14,0x265e5a51);
GG(b,c,d,a,M[0],20,0xe9b6c7aa);
GG(a,b,c,d,M[5],5,0xd62f105d);
GG(d,a,b,c,M[10],9,0x02441453);
GG(c,d,a,b,M[15],14,0xd8a1e681);
GG(b,c,d,a,M[4],20,0xe7d3fbc8);
GG(a,b,c,d,M[9],5,0x21e1cde6);
GG(d,a,b,c,M[14],9,0xc33707d6);
GG(c,d,a,b,M[3],14,0xf4d50d87);
GG(b,c,d,a,M[8],20,0x455a14ed);
GG(a,b,c,d,M[13],5,0xa9e3e905);
GG(d,a,b,c,M[2],9,0xfcefa3f8);
GG(c,d,a,b,M[7],14,0x676f02d9);
GG(b,c,d,a,M[12],20,0x8d2a4c8a);

//第三轮
HH(a,b,c,d,M[5],4,0xfffa3942);
HH(d,a,b,c,M[8],11,0x8771f681);
HH(c,d,a,b,M[11],16,0x6d9d6122);
HH(b,c,d,a,M[14],23,0xfde5380c);
HH(a,b,c,d,M[1],4,0xa4beea44);
HH(d,a,b,c,M[4],11,0x4bdecfa9);
HH(c,d,a,b,M[7],16,0xf6bb4b60);
HH(b,c,d,a,M[10],23,0xbebfbc70);
HH(a,b,c,d,M[13],4,0x289b7ec6);
HH(d,a,b,c,M[0],11,0xeaa127fa);
HH(c,d,a,b,M[3],16,0xd4ef3085);
HH(b,c,d,a,M[6],23,0x04881d05);
HH(a,b,c,d,M[9],4,0xd9d4d039);
HH(d,a,b,c,M[12],11,0xe6db99e5);
HH(c,d,a,b,M[15],16,0x1fa27cf8);
HH(b,c,d,a,M[2],23,0xc4ac5665);

//第四轮
II(a,b,c,d,M[0],6,0xf4292244);
II(d,a,b,c,M[7],10,0x432aff97);
II(c,d,a,b,M[14],15,0xab9423a7);
II(b,c,d,a,M[5],21,0xfc93a039);
II(a,b,c,d,M[12],6,0x655b59c3);
II(d,a,b,c,M[3],10,0x8f0ccc92);
II(c,d,a,b,M[10],15,0xffeff47d);
II(b,c,d,a,M[1],21,0x85845dd1);
II(a,b,c,d,M[8],6,0x6fa87e4f);
II(d,a,b,c,M[15],10,0xfe2ce6e0);
II(c,d,a,b,M[6],15,0xa3014314);
II(b,c,d,a,M[13],21,0x4e0811a1);
II(a,b,c,d,M[4],6,0xf7537e82);
II(d,a,b,c,M[11],10,0xbd3af235);
II(b,c,d,a,M[9],21,0xeb86d391);
}
//转16进制
CString UcToHex(unsigned char num)
{
int n = 0;
char array[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char temp[10000];
CString result;
while(num > 0)
{
temp[n++] = array[num %16];
num = num >> 4;
}
for(int i = n-1; i >=0 ; i--)
{
result = result + temp[i];
}
return result;
}
CString ToHex(long num)
{
int n = 0;
char array[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char temp[10000];
CString result;
while(num > 0)
{
temp[n++] = array[num%16];
num = num >> 4;
}
for(int i = n-1; i >=0 ; i--)
{
result = result + temp[i];
}
return result;
}
//这个MD5算法实现的主函数
CString Hexmd5(CString str)
{
unsigned int M[16];
unsigned short *T;

//定义链接变量
unsigned int A = 0x67452301;
unsigned int B = 0xefcdab89;
unsigned int D = 0x10325476;

unsigned int AA;
unsigned int BB;
unsigned int CC;
unsigned int DD;

unsigned int N;
unsigned int length=(unsigned int)str.GetLength();
unsigned long hexLen;

unsigned int n;
int ones = 1;
int m = length % 64;

if (m == 56)
{
ones = 0;
}

if(m<56)
{
N = length/64+1;
}
else
{
N = length/64+2;
}

T=new unsigned short[N*64];
for(n=0; n < N*64; n++)
{
T[n]=0;
}

for(n=0; n < length; n++)
{
T[n]=(unsigned short)str.GetAt(n);
}

if(ones==1)
{
T[n]=0x80;

for(n=length+1; n < N*64-8; n++)
{
T[n]=0x00;
}
}

hexLen=length*8;
for(; n<N*64; n++)
{
if(n<60)
{
T[n]=(unsigned short)hexLen&0xff;
hexLen=hexLen>>8;
}
else
{
T[n]=(unsigned short)hexLen&0xff;
hexLen=hexLen>>8;
}
}

for(unsigned int i=0; i<N; i++)
{
for(unsigned int j=0; j<16; j++)
{
M[j]=((int)T[i*64+j*4]|(int)(T[i*64+j*4+1]<<8)|(int)(T[i*64+j*4+2]<<16)|(int)(T[i*64+j*4+3]<<24));
}

//将上面四个链接变量A,B,C,D复制到另外四个变量AA,BB,CC,DD中
AA=A;
BB=B;
CC=C;
DD=D;

//进入四轮循环运算
Recurrence(A, B, C, D, M);

//将A,B,C,D加上AA,BB,CC,DD，继续参与到下一次的运算中
A=A+AA;
B=B+BB;
C=C+CC;
D=D+DD;
}

//将最后得到的A,B,C,D按顺序以十六进制表示方法输出
unsigned char output[16];
unsigned int bits[4]={A, B, C, D};

for (int i = 0,j = 0; j < 16; i++, j += 4)
{
output[j] = (unsigned char)(bits[i] & 0xff);
output[j + 1] = (unsigned char)((bits[i] >> 8) & 0xff);
output[j + 2] = (unsigned char)((bits[i] >> 16) & 0xff);
output[j + 3] = (unsigned char)((bits[i] >> 24) & 0xff);
}
//char outch[33];
CString tmp,reslut;
for(int j = 0; j < 16; j++ )
{
tmp.Format(_T("%02x"),output[j]);
reslut +=tmp;
//sprintf(outch+ j * 2,"%02x",output[j]);
}
return reslut;
}

};

#pragma once
#include "md5.h"
class CRasDial
{
public:
CRasDial();
~CRasDial();
public:
CMD5 m_md5;
public:
LONG GetTime();
BOOL ClosePPPoE();
BOOL GetConnectState();

BOOL CreatePPPoE(LPCTSTR entryname);
};

CPP

#include "stdafx.h"
#include "DialNet.h"
CRasDial::CRasDial()
{

}
CRasDial::~CRasDial()
{

}

BOOL CRasDial::CreatePPPoE(LPCTSTR entryname)
{
LPRASENTRY lpRasEntry = NULL;
DWORD cb = sizeof(RASENTRY);
DWORD dwBufferSize = 0;
// 取得entry的大小
DWORD dwRet = RasGetEntryProperties(NULL, "", NULL, &dwBufferSize, NULL, NULL);
if (dwBufferSize == 0)
return -1;

lpRasEntry = (LPRASENTRY)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBufferSize);
if (lpRasEntry == NULL)
return -2;

ZeroMemory(lpRasEntry, sizeof(RASENTRY));
lpRasEntry->dwSize = dwBufferSize;
//RASEO_PreviewUserPw需要显示ui
lpRasEntry->dwfOptions = RASEO_PreviewUserPw|RASEO_RemoteDefaultGateway;

lstrcpy(lpRasEntry->szDeviceType, RASDT_PPPoE);
lstrcpy(lpRasEntry->szDeviceName, "");
lpRasEntry->dwfNetProtocols = RASNP_Ip;
lpRasEntry->dwFramingProtocol = RASFP_Ppp;

//创建连接
dwRet = RasSetEntryProperties(NULL, entryname, lpRasEntry, dwBufferSize, NULL, 0);
HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasEntry);

if (dwRet != 0)
return TRUE;

return FALSE;
}
BOOL CRasDial::GetConnectState()
{
LPRASCONN lpRasConn = NULL;
DWORD cbBuf = 0;
DWORD cConn = 0;
DWORD dwRet = 0;
HRASCONN hrasconn;
RASCONNSTATUS rasStatus;
UINT ndx;

//枚举连接
cbBuf = sizeof(RASCONN);

if ( (lpRasConn = ( LPRASCONN ) malloc((UINT)cbBuf)) != NULL)
{
lpRasConn->dwSize = sizeof( RASCONN );
dwRet = RasEnumConnections( lpRasConn, &cbBuf, &cConn );
for (ndx = 0; ndx < cConn; ndx++)
{
// 得到HRASCONN
hrasconn = lpRasConn[ndx].hrasconn;
// 得到连接状态
rasStatus.dwSize = sizeof(RASCONNSTATUS);
dwRet = RasGetConnectStatus( hrasconn, &rasStatus );
if ( dwRet )
return FALSE;
else
{
if (rasStatus.rasconnstate == RASCS_DONE)
return TRUE;
else return FALSE;
}
}
}
else
{
return FALSE;
}
return FALSE;
}
BOOL CRasDial::ClosePPPoE()
{
RASCONN ras[20];
DWORD dwSize, dwNumber, dwCount;
ras[0].dwSize=sizeof(RASCONN);
dwSize =sizeof(ras);

if (RasEnumConnections(ras, &dwSize, &dwNumber) == 0)
{
//依次关闭所有的连接
for (dwCount=0; dwCount<dwNumber; dwCount++ )
{
if (RasHangUp(ras[dwCount].hrasconn)!=0)
{
RASCONNSTATUS rstatus;
while(RasGetConnectStatus(ras[dwCount].hrasconn,&rstatus) != ERROR_INVALID_HANDLE) Sleep(0);
break;
}
}
}
return TRUE;
}

{

RASDIALPARAMS RasDialParams;
RasDialParams.dwSize = sizeof(RASDIALPARAMS);
strcpy_s(RasDialParams.szEntryName,entryname);
strcpy_s(RasDialParams.szPhoneNumber,"");
strcpy_s(RasDialParams.szCallbackNumber,"");
strcpy_s(RasDialParams.szDomain, "");

HRASCONN hRasConn = NULL;
DWORD dwRet = RasDial( NULL, NULL, &RasDialParams,0L, NULL,&hRasConn );

if (dwRet != 0)
{
RasHangUp(hRasConn);

return FALSE;
}

return TRUE;
}
LONG CRasDial::GetTime()
{
CTime nowtime = CTime::GetCurrentTime();
int day = nowtime.GetDay();
int year =nowtime.GetYear();
int month = nowtime.GetMonth();
int hour = nowtime.GetHour();
int minute = nowtime.GetMinute();
int second = nowtime.GetSecond();
long ntime = ((year-1970)*365*24*3600)+(month*30*24*3600)+(day*24*3600)+(hour*3600)+(minute*60+second);

return ntime;
}

{
CString timehex = m_md5.ToHex(GetTime());
CString md5hestr=m_md5.Hexmd5(str);
CString md520hexstr;
for (int i = 0;i<20;i++)
{
md520hexstr += md5hestr.GetAt(i);
}
return  "~ghca" + timehex + "2002" + md520hexstr + username;
}

{
{
return FALSE;
}
return TRUE;
}
{
string md5users = md5str.GetBuffer();
md5str.ReleaseBuffer();
if( OpenClipboard(NULL) )
{
HANDLE hclip;
char *buffer;
EmptyClipboard();
hclip = GlobalAlloc(GMEM_MOVEABLE, strlen(md5users.c_str())+1);
buffer = (char*)GlobalLock(hclip);
strcpy_s(buffer,strlen(md5users.c_str())+1,md5users.c_str());
GlobalUnlock(hclip);
SetClipboardData(CF_TEXT,hclip);
CloseClipboard();
return TRUE;
}
return FALSE;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：电信协同拨号器算法及示例 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)