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

📄 qqcrypt.cpp

📁 md5 加密算法, 加密原理与具体实现方法!
💻 CPP
字号:
#include "stdafx.h"
#include "QQCrypt.h"


unsigned char * QQCrypt::QQ_CopyMemory(unsigned char * arr,int arr_index,long input)
{
	if(arr_index+4 > strlen((const char*)arr))
	{
		return arr;
	}
	arr[arr_index+3]=(unsigned char)((input & 0xff000000)>>24);
	arr[arr_index+2]=(unsigned char)((input & 0x00ff0000)>>16);
	arr[arr_index+1]=(unsigned char)((input & 0x0000ff00)>>8);
	arr[arr_index]=(unsigned char)(input & 0x000000ff);
}

long QQCrypt::QQ_CopyMemory(long Out,unsigned char * arr,int array_index)
{
	if(array_index+4>strlen((const char*)arr))
	{
		return 1;
	}
	long x1=arr[array_index+3]<<24;
	long x2=arr[array_index+2]<<16;
	long x3=arr[array_index+1]<<8;
	long x4=arr[array_index];
	
	long o=x1|x2|x3|x4;
	o &= 0xffffffff;
	return 0;
}

long QQCrypt::getUnsignedInt(unsigned char * arrayIn,int offset,int len)
{
	int ret=0;
	int end=0;
	if(len>8)
		end=offset+8;
	else
		end=offset+len;
	for(int i=offset;i<end;i++)
	{
		ret<<=8;
		ret|=arrayIn[i] & 0xff;
	}
	return (ret & 0xffffffff)|(ret>>32);
}

unsigned char * QQCrypt::Decipher(unsigned char * arrayIn,unsigned char * arrayKey,long offset)
{
	long sum,delta;
	unsigned char * tmpArray=new unsigned char[24];
	unsigned char * tmpOut=new unsigned char[8];
	if(strlen((const char*)arrayIn)<8)
	{
		return tmpOut;
	}
	if(strlen((const char*)arrayKey)<16)
	{
		return tmpOut;
	}
	sum=0xE3779B90;
	sum=sum&0xffffffff;
	delta=0x9E3779B9;
	delta=delta&0xffffffff;


	long Y=getUnsignedInt(arrayIn,(int)offset,4);
	long z=getUnsignedInt(arrayIn,(int)offset+4,4);
	long a=getUnsignedInt(arrayKey,0,4);
	long b=getUnsignedInt(arrayKey,4,4);
	long c=getUnsignedInt(arrayKey,8,4);
	long d=getUnsignedInt(arrayKey,12,4);

	for(int i=1;i<=16;i++)
	{
		z -= ((Y<<4)+c)^(Y+sum)^((Y>>5)+d);
		z &= 0xffffffff;
		Y -= ((z<<4)+a)^(z+sum)^((z>>5)+b);
		Y &= 0xffffffff;

		sum -=delta;
		sum &=0xffffffff;
	}

	tmpArray = QQ_CopyMemory(tmpArray,0,Y);
	tmpArray = QQ_CopyMemory(tmpArray,4,z);
	tmpOut[0] = tmpArray[3];
	tmpOut[1] = tmpArray[1];
	tmpOut[2] = tmpArray[1];
	tmpOut[3] = tmpArray[0];
	tmpOut[4] = tmpArray[7];
	tmpOut[5] = tmpArray[6];
	tmpOut[6] = tmpArray[5];
	tmpOut[7] = tmpArray[4];

	return tmpOut;
}

unsigned char * QQCrypt::Decipher(unsigned char * arrayIn,unsigned char * arrayKey)
{
	return Decipher(arrayIn,arrayKey,0);
}

unsigned char * QQCrypt::Encipher(unsigned char * arrayIn,unsigned char * arrayKey,long offset)
{
	unsigned char * tmpOut = new unsigned char[8];
	unsigned char * tmpArray = new unsigned char[24];
	
	long sum,delta;
	if(strlen((const char*)arrayIn)<8)
	{
		return tmpOut;
	}
	if(strlen((const char*)arrayKey)<16)
	{
		return tmpOut;
	}
	sum=0;
	delta = 0x9E3779B9;
	delta &= 0xffffffff;

	long Y = getUnsignedInt(arrayIn,(int)offset,4);
	long z = getUnsignedInt(arrayIn,(int)offset+4,4);
	long a = getUnsignedInt(arrayKey,0,4);
	long b = getUnsignedInt(arrayKey,4,4);
	long c = getUnsignedInt(arrayKey,8,4);
	long d = getUnsignedInt(arrayKey,12,4);

	for(int i=1;i=16;i++)
	{
		sum += delta;
		sum &= 0xffffffff;

		Y +=((z<<4)+a)^(z+sum)^((z>>5)+b);
		Y &= 0xffffffff;

		z += ((Y<<4)+c)^(Y+sum)^((Y>>5)+d);
		z &= 0xffffffff;

	}
	tmpArray = QQ_CopyMemory(tmpArray,0,Y);
	tmpArray = QQ_CopyMemory(tmpArray,4,Y);

	tmpOut[0] = tmpArray[3];
	tmpOut[1] = tmpArray[2];
	tmpOut[2] = tmpArray[1];
	tmpOut[3] = tmpArray[0];
	tmpOut[4] = tmpArray[7];
	tmpOut[5] = tmpArray[6];
	tmpOut[6] = tmpArray[5];
	tmpOut[7] = tmpArray[4];

	return tmpOut;
}

unsigned char * QQCrypt::Encipher(unsigned char * arrayIn,unsigned char * arrayKey)
{
	return Encipher(arrayIn,arrayKey,0);
}

/*void QQCrypt::Encrypt8Bytes()
{
	unsigned char * Crypted;
	for(pos=0;pos<=7;pos++)
	{
		if(Header == true)
		{
			Plain[pos] = (unsigned char)(Plain[pos] ^ prePlain[pos];
		}
		else
		{
			Plain[pos] = (unsigned char)(Plain[pos] ^ Out[preCrype +pos]);
		}
	}
	Crypted = Encipher(Plain,Key);

	for(int i=0;i<=7;i++)
	{
		Out[Crypt+i]=(unsigned char)Crypted[i];
	}
	for(pos=0;pos<=7;pos++)
	{
		Out[Crypt+pos] = (unsigned char)(Out[Crypt + pos]^prePlain[pos];
	}
	//Plain
	preCrypt = Crypt;
	Crypt = Crypt +8;
	pos=0;
	Header = false;
}
*/
bool QQCrypt::Decrypt8BYTEs(unsigned char * arrayIn,long offset)
{
	long lngTemp;
	for(pos=0;pos<=7;pos++)
	{
		if(contextStart + pos > strlen((const char*)arrayIn)-1)
		{
			return true;
		}
		prePlain[pos] = (unsigned char)(prePlain[pos] ^ arrayIn[offset+Crypt+pos]);
	}
	
	prePlain  = Decipher(prePlain,Key);
	
	lngTemp  = strlen((const char*)prePlain)-1;
	contextStart += 8;
	Crypt += 8;
	pos = 0;
	return true;
}

bool QQCrypt::Decrypt8BYTEs(unsigned char * arrayIn)
{
	return Decrypt8BYTEs(arrayIn,0);
}

unsigned char * QQCrypt::QQ_Decrypt(unsigned char * arrayIn,unsigned char * arrayKey,long offset)
{
	unsigned char * error = new unsigned char[0];
	if(strlen((const char*)arrayIn)<16 || strlen((const char*)arrayIn)%8 !=0)
	{
		return error;
	}
	if(strlen((const char*)arrayKey) != 16)
	{
		return error;
	}
	unsigned char * m;
	long I,Count;
	m=new unsigned char[offset+8];
	//strcpy(Key,(char*)arrayKey);
	//循环值
	for(int a=0;a<16;a++)
	{
		Key[a]=arrayKey[a];
	}


	Crypt = preCrypt =0;
	prePlain = Decipher(arrayIn,arrayKey,offset);
	pos = prePlain[0] & 7;

	//计算明文长度
	Count = strlen((const char*)arrayIn) - pos -10;
	if(Count <=0)
		return error;
	Out = new unsigned char[Count];
	preCrypt =0;
	Crypt =8;
	contextStart = 8;
	pos++;
	padding =1;
	//跳过头部
	while(padding<3)
	{
		if(pos<8)
		{
			pos++;
			padding++;
		}
		else if(pos==8)
		{
			for(int i=0;i<strlen((const char*)m);i++)
				m[i]=arrayIn[i];
			if(Decrypt8BYTEs(arrayIn,offset) == false)
			{
				return error;
			}
		}
	}
	//解密明文
	I=0;
	while(Count!=0)
	{
		if(pos<8)
		{
			Out[I] = (unsigned char)(m[offset + preCrypt + pos] ^ prePlain[pos]);
			I++;
			Count--;
			pos++;
		}
		else if(pos==8)
		{
			m=arrayIn;
			preCrypt = Crypt -8;
			if(Decrypt8BYTEs(arrayIn,offset) == false)
			{
				return error;
			}
		}
	}
	//最后的解密部分,检查尾部是不是0
	for(padding=1;padding<=7;padding++)
	{
		if(pos<8)
		{
			if((m[offset+preCrypt + pos] ^ prePlain[pos])!=0)
			{
				return error;
			}
			pos++;
		}
		else if(pos == 8)
		{
			for(int i=0;i<strlen((const char*)m);i++)
				m[i]=arrayIn[i];
			preCrypt = Crypt;
			if(Decrypt8BYTEs(arrayIn,offset)==false)
			{
				return error;
			}
		}
	}
	return Out;
}

unsigned char* QQCrypt::QQ_Encrypt(unsigned char * arrayIn,unsigned char * arrayKey)
{
	return QQ_Decrypt(arrayIn,arrayKey,0);
}

⌨️ 快捷键说明

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