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

📄 qqencryptdlg.cpp

📁 一个QQ-TEA加密解密算法的应用程序, 可以直接对数据进行加密和解密, 对于分析QQ登录协议很有帮助
💻 CPP
字号:
// QQEncryptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QQEncrypt.h"
#include "QQEncryptDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define LINE 1024

unsigned int swapu32(unsigned int n)
{
	return(((n & 0xff000000) >> 24) | ((n & 0x000000ff) << 24) |
		((n & 0x00ff0000) >> 8) | ((n & 0x0000ff00) << 8));
}
void encrypt_qword(unsigned long *, unsigned long *, unsigned long *);
void decrypt_qword(unsigned long *, unsigned long *, unsigned long *);
/* use key to encrypt in buffer
* key must be 16+ bytes long!
*/
void encrypt_msg(unsigned char *in, int inlen, unsigned long *key, unsigned char *out, unsigned long *outlen)
{
	register int m, i, j, count, p = 1;
	unsigned char q[12], *q1, *q2, *inp;
	unsigned char mkey[8];

	m = (inlen+10)%8;
	if (m)  m = 8-m;
	q[0] = (rand()&0xf8) | m;
	i = j = 1;
	while(m>0) {
		q[i++] = rand()&0xff;
		m --;
	}
	count = *outlen = 0;
	q2 = q1 = out;
	memset(mkey, 0, sizeof(mkey));
	while( p <= 2 ) {
		if (i < 8) {
		q[i++] = rand()&0xff;
		p ++;
	}
	if (i == 8) {
		for (i = 0; i < 8; i ++)
			q[i] ^= mkey[i];
			encrypt_qword((unsigned long *)q, key, (unsigned long *)out);
			for (i = 0; i < 8; i ++)
				q1[i] ^= mkey[i];
			q2 = q1;
			q1 += 8;
			count += 8;
			memcpy(mkey, q, 8);
			j = i = 0;
		}
	}
	inp = in;
	while (inlen > 0) {
		if (i < 8) {
			q[i] = inp[0];
			inp ++;
			i ++;
			inlen --;
		}
		if (i == 8) {
			for (i = 0; i < 8; i ++)  {
				if (j) q[i] ^= mkey[i];
				else q[i] ^= q2[i];
			}
			j = 0;
			encrypt_qword((unsigned long *)q, key, (unsigned long *)q1);
			for (i = 0; i < 8; i ++)
				q1[i] ^= mkey[i];
			count += 8;
			memcpy(mkey, q, 8);
			q2 = q1;
			q1 += 8;
			i = 0;
		}
	}
	p = 1;
	while (p < 8) {
	if (i < 8) {
		memset(q+i, 0, 4);
		p++;
		i++;
	}
	if (i == 8) {
		for (i = 0; i < 8; i ++)
			q[i] ^= q2[i];
		encrypt_qword((unsigned long *)q, key, (unsigned long *)q1);
		for (i = 0; i < 8; i ++)
			q1[i] ^= mkey[i];
			memcpy(mkey, q, 8);
			count += 8;
			q2 = q1;
			q1 += 8;
			i = 0;
		}
	}
	*outlen = count;
}
/* decrypt a encrypted string
* key must be 16+ bytes long
* return 0 if failed.
* otherwise return 1.
*/
int decrypt_msg(unsigned char *in, int inlen, unsigned long *key, unsigned char *out, unsigned long *outlen)
{
	unsigned char q[8], mkey[8], *q1, *q2, *outp;
	register int count, i, j, p;

	if (inlen%8 || inlen<16) return 0;
	/* get basic information of the packet */
	decrypt_qword((unsigned long *)in, key, (unsigned long *)q);
	j = q[0]&0x7;
	count = inlen - j - 10;
	if ((int)*outlen < count || count < 0) return 0;
	*outlen = count;
	memset(mkey, 0, 8);
	q2 = mkey;
	i = 8; p = 1;
	q1 = in+8;
	j ++;
	while (p <= 2) {
		if (j < 8) {
			j ++;
			p ++;
		} else if (j == 8) {
			q2 = in;
			for (j = 0; j < 8; j ++ ) {
				if (i + j >= inlen) return 0;
				q[j] ^= q1[j];
			}
			decrypt_qword((unsigned long *)q, key, (unsigned long *) q);
			i += 8;
			q1 += 8;
			j = 0;
		}
	}
	outp = out;
	while(count !=0) {
		if (j < 8) {
			outp[0] = q2[j] ^ q[j];
			outp ++;
			count --;
			j ++;
		} else if (j == 8) {
			q2 = q1-8;
			for (j = 0; j < 8; j ++ ) {
				if (i + j >= inlen) return 0;
				q[j] ^= q1[j];
			}
			decrypt_qword((unsigned long *)q, key, (unsigned long *) q);
			i += 8;
			q1 += 8;
			j = 0;
		}
	}
	for (p = 1; p < 8; p ++) {
		if (j < 8) {
			if (q2[j]^q[j])
			return 0;
			j ++;
		} else if (j == 8 ) {
			q2 = q1;
			for (j = 0; j < 8; j ++ ) {
				if (i + j >= inlen) return 0;
				q[j] ^= q1[j];
			}
			decrypt_qword((unsigned long *)q, key, (unsigned long *) q);
			i += 8;
			q1 += 8;
			j = 0;
		}
	}
	return 1;
}
void encrypt_qword(unsigned long *in, unsigned long *key, unsigned long *out)
{
	unsigned long code[4];
	register unsigned long i = 16, j = 0, m, n;

	m = swapu32(in[0]);
	n = swapu32(in[1]);
	code[0] = swapu32(key[0]); code[1] = swapu32(key[1]);
	code[2] = swapu32(key[2]); code[3] = swapu32(key[3]);
	while(i-->0) {
		j -= 0x61c88647;
		m += (n>>5)+code[1] ^ (n<<4) +code[0] ^ j+n;
		n += (m>>5)+code[3] ^ (m<<4) +code[2] ^ j+m;
	}
	out[0] = swapu32(m);
	out[1] = swapu32(n);
}

void decrypt_qword(unsigned long *in, unsigned long *key, unsigned long *out)
{
	unsigned long code[4];
	register unsigned long i=16, j=0xe3779B90, m, n;

	m = swapu32(in[0]);
	n = swapu32(in[1]);
	code[0] = swapu32(key[0]); code[1] = swapu32(key[1]);
	code[2] = swapu32(key[2]); code[3] = swapu32(key[3]);
	while(i-- >0) {
		n -= ((m>>5)+code[3])^((m<<4)+code[2])^(j+m);
		m -= ((n>>5)+code[1])^((n<<4)+code[0])^(j+n);
		j += 0x61C88647;
	}
	out[0] = swapu32(m);
	out[1] = swapu32(n);
}

void transform_msg(const unsigned char *in, int inlen, unsigned char *out, unsigned int *number)
{
	unsigned char c;
	unsigned int i, j;

	for (i = 0, j = 0 ; j < (unsigned int)inlen ; j++) {
		if (i >= 0x3ff) break;
		c = in[j];
		if (c == 0) {
			out[i++] |= 0xff;
			out[i] = 0x30;
		} else if (c == 0xff) {
			out[i++] |= 0xff;
			out[i] |= 0xff;
		} else
			out[i] = c;
		i ++;
	}
	*number = i;
}

void detransform_msg(const unsigned char *in, int inlen, unsigned char *out, unsigned int *number)
{
	unsigned char c;
	unsigned int i, j;

	for ( i = 0,j = 0; j < (unsigned int)inlen; j ++) {
		if (i >= 0x3ff) break;
		c = in[j];
		if (c == 0xff) {
			if (in[j+1] == 0x30) {
				out[i++] = 0;
				j ++;
				continue;
			}
			if (in[j+1] == 0xff) {
				out[i++] = 0xff;
				j ++;
				continue;
			}
		}
		out[i++] = c;
	}
	out[i] = '\0';
	*number = i;
}

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQQEncryptDlg dialog

CQQEncryptDlg::CQQEncryptDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQQEncryptDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQQEncryptDlg)
	m_strEnpass = _T("");
	m_strIn = _T("1 2 3 4 5 11 22 33 44 55");
	m_strKey = _T("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQQEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQQEncryptDlg)
	DDX_Text(pDX, IDC_EDIT_ENPASS, m_strEnpass);
	DDX_Text(pDX, IDC_EDIT_IN, m_strIn);
	DDX_Text(pDX, IDC_EDIT_KEY, m_strKey);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQQEncryptDlg, CDialog)
	//{{AFX_MSG_MAP(CQQEncryptDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ENCODE, OnButtonEncode)
	ON_BN_CLICKED(IDC_BUTTON_DECODE, OnButtonDecode)
	ON_EN_CHANGE(IDC_EDIT_KEY, OnChangeEditKey)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQQEncryptDlg message handlers

BOOL CQQEncryptDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQQEncryptDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CQQEncryptDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQQEncryptDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CQQEncryptDlg::OnButtonEncode() 
{
	// TODO: Add your control notification handler code here
//	CQQCrypt QQCrypt;
	BYTE *pInData = NULL;
	BYTE *pKeyData = NULL;
	BYTE *pOutData = NULL;
	int nInLen = 0;
	int nKeyLen = 0;
	int nOutLen = 0;

	UpdateData(TRUE);
	nKeyLen = GetHexDataFormString(m_strKey, pKeyData);
	if (nKeyLen != 16) {
		MessageBox(_T("密钥长度应为16!"));
		delete[] pKeyData;
		return;
	}
	nInLen = GetHexDataFormString(m_strIn, pInData);
	nOutLen = (nInLen + 10) % 8;
	if (nOutLen) nOutLen = 8- nOutLen;
	nOutLen += nInLen + 10;
	pOutData = new BYTE[nOutLen];
	encrypt_msg((BYTE *)pInData, nInLen, (unsigned long *)pKeyData, (BYTE *)pOutData, (DWORD *)&nOutLen);
	m_strEnpass = PrintHexDataToString(pOutData, nOutLen);
	UpdateData(FALSE);
	delete[] pInData;
	delete[] pKeyData;
	delete[] pOutData;
}

void CQQEncryptDlg::OnButtonDecode() 
{
	// TODO: Add your control notification handler code here
	BYTE *pOutData = NULL;
	BYTE *pInData = NULL;
	BYTE *pKeyData = NULL;
	int nInLen = 0;
	int nKeyLen = 0;
	int nOutLen = -1;

	UpdateData(TRUE);
	nKeyLen = GetHexDataFormString(m_strKey, pKeyData);
	if (nKeyLen != 16) {
		MessageBox(_T("密钥长度应为16!"));
		delete[] pKeyData;
		return;
	}
	nInLen = GetHexDataFormString(m_strEnpass, pInData);
	unsigned char q[8];
	if ((nInLen % 8) == 0 && nInLen >= 16) {
		decrypt_qword((unsigned long *)pInData, (unsigned long *)pKeyData, (unsigned long *)q);
		nOutLen = nInLen - (q[0]&0x7) - 10;
	}
	if (nOutLen < 0) {
		MessageBox(_T("解密失败!"));
		delete[] pKeyData;
		return;
	}
	pOutData = new BYTE[nOutLen + 1];
	nInLen = GetHexDataFormString(m_strEnpass, pInData);
	if (decrypt_msg((BYTE *)pInData, nInLen, (unsigned long *)pKeyData, (BYTE *)pOutData, (DWORD *)&nOutLen)) {
		m_strIn = PrintHexDataToString(pOutData, nOutLen);
		UpdateData(FALSE);
	} else {
		MessageBox(_T("解密失败!"));
	}
	delete[] pInData;
	delete[] pKeyData;
	delete[] pOutData;
}

int CQQEncryptDlg::GetHexDataFormString(CString strIn, BYTE *&pOutData)
{
	TCHAR cLast = _T(' ');
	TCHAR cCur;
	int nLen = 0;
	int nPos = 0;
	int nData;
	CString str;
	int i;
	
	pOutData = NULL;
	for (i = 0; i < strIn.GetLength(); i++) {		//统计数据个数
		cCur = strIn.GetAt(i);
		if (cLast == _T(' ') && cCur != _T(' ')) {
			nLen++;
		}
		cLast = cCur;
	}
	if (nLen) {
		pOutData = new BYTE[nLen];
		cLast = _T(' ');
		for (i = 0; i < strIn.GetLength(); i++) {
			cCur = strIn.GetAt(i);
			if (cLast == _T(' ') && cCur != _T(' ')) {
				nData = 0;
				_stscanf(strIn.GetBuffer(0) + i, _T("%x"), &nData);
				strIn.ReleaseBuffer();
				pOutData[nPos] = (BYTE)nData;
				nPos++;
			}
			cLast = cCur;
		}
	}

	return nLen;

}

CString CQQEncryptDlg::PrintHexDataToString(BYTE *pInData, int nDataLen)
{
	char *lpszOut = new char[nDataLen * 3 + 1];
	int i;
	CString strTmp;

	for (i = 0; i < nDataLen; i++) {
		sprintf(lpszOut + i * 3, "%02X ", pInData[i]);
	}
	if (nDataLen) {
		lpszOut[nDataLen * 3 - 1] = '\0';
	} else {
		lpszOut[0] = '\0';
	}
	strTmp = lpszOut;
	delete[] lpszOut;

	return strTmp;
}

void CQQEncryptDlg::OnChangeEditKey() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_strKey.Replace(_T("\r\n"), _T(""));
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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