⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servupass.cpp

📁 用C++写的Serv-U的加密算法
💻 CPP
字号:
// ServUPass.cpp: implementation of the ServUPass class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServUPass.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ServUPass::ServUPass()
{
	lpszRand = "";
}

ServUPass::~ServUPass()
{

}




//////////////////////////////////////////////////////////////////////////
//
// 将用户输入的密码和一个经过 ServU 规则加密的密码进行比较
// 如果相同则返回 true,否则返回 false
bool ServUPass::PassCmp(string strInputPass, string strServUPassIni) {
	// strInputPass		- 用户输入的明文密码
	// strServUPassIni	- INI 文件里的密码

	//先提取 INI 文件内密文前两位
	string strPass2Byte, strPass32Byte;
	try {
		strPass2Byte = strServUPassIni.substr( 0, 1 );
		strPass32Byte = strServUPassIni.substr( 2, 32 );
	}catch ( string err_desc ) {
		return false;
	}


	string strMPass = strPass2Byte + strInputPass;

	MD5_CTX	m_md5;
	unsigned char szEncrypt[16]	= {0};
	
	m_md5.MD5Update( (unsigned char *)strMPass.c_str(), strMPass.length() );
	m_md5.MD5Final(szEncrypt);
	
	char szHexTmp[2] = {0};
	char szOutput[32] = {0};
	ZeroMemory(szOutput, sizeof(szOutput));

	for ( int i=0; i<16; i++ ) {
		ZeroMemory(szHexTmp, sizeof(szHexTmp));
		sprintf(szHexTmp, "%02X", szEncrypt[i]);
		strcat(szOutput, szHexTmp);
	}

	string str_inputpass_md5(szOutput);
	if ( str_inputpass_md5 != strPass32Byte ) {
		return false;
	}

	return true;
}






//////////////////////////////////////////////////////////////////////////
//
// 按照 Serv-U 的加密算法进行加密
bool ServUPass::getServUPass( string strInputPass, char szServUPass[34] )
{
	static char staticSzRand[2] = {0};
	char szRand[2] = {0};
	getRandNum( staticSzRand );
	strcpy( szRand, staticSzRand );

	string strMPass = "";
	strMPass = szRand + strInputPass;


	MD5_CTX	m_md5;
	unsigned char szEncrypt[16]	= {0};
	
	m_md5.MD5Update( (unsigned char *)strMPass.c_str(), strMPass.length() );
	m_md5.MD5Final(szEncrypt);

	char szHexTmp[2] = {0};
	char szOutput[32] = {0};

	ZeroMemory(szOutput, sizeof(szOutput));
	ZeroMemory(szServUPass, sizeof(szServUPass));

	for ( int i=0; i<16; i++ ) {
		ZeroMemory(szHexTmp, sizeof(szHexTmp));
		sprintf(szHexTmp, "%02X", szEncrypt[i]);
		strcat(szOutput, szHexTmp);
	}

	strcpy(szServUPass, staticSzRand);
	strcat(szServUPass, szOutput);

	//sprintf(szServUPass, "%02s%32s", szRand, szOutput);


	return true;

}




//////////////////////////////////////////////////////////////////////////
//
// 获取两位随机码
bool ServUPass::getRandNum(LPSTR lpszRandNum)
{
	srand((unsigned)time(NULL));
	int iRandNum1=0, iRandNum2=0;
	iRandNum1 = rand()%100; // 0-100之间的随机数
	iRandNum2 = rand()%100;
	
	iRandNum1 += 60;
	iRandNum2 += 60;
	
	if ( iRandNum1 <97 ) {
		iRandNum1 = 97;
	}
	if ( iRandNum1 > 122 ) {
		iRandNum1 =122;
	}

	if ( iRandNum2 < 97 ) {
		iRandNum2 = 97;
	}
	if ( iRandNum2 > 122 ) {
		iRandNum2 = 122;
	}

	sprintf(lpszRandNum, "%01c%01c", iRandNum1, iRandNum2);

	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -