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

📄 main.cpp

📁 Tiny Encrytion Algorithm压缩速度快
💻 CPP
字号:
#include <windows.h>
#include "resource.h"
#include "tea.h"
#include "ac_array.h"

acCArray<char> encryptedText;

int WINAPI DialogProc(HWND, UINT, WPARAM, LPARAM);
void DoEncrypt(HWND hDlg);
void DoDecrypt(HWND hDlg);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), 0, (DLGPROC)DialogProc);

	return 0;
}

int WINAPI DialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch( msg )
	{
	case WM_INITDIALOG:
		SetDlgItemText(hDlg, IDC_KEY, "TEA Encryption key");
		SetDlgItemText(hDlg, IDC_MESSAGE, "This is the text that will be encrypted by the TEA Block Encryption algorithm.");
		return TRUE;

	case WM_CLOSE:
		EndDialog(hDlg, 0);
		return TRUE;

	case WM_COMMAND:
		switch( LOWORD(wParam) )
		{
		case IDC_DOENCRYPT:
			DoEncrypt(hDlg);
			return TRUE;

		case IDC_DODECRYPT:
			DoDecrypt(hDlg);
			return TRUE;
		}
		break;
	}

	// Let windows treat the message with default behaviour
	return FALSE;
}

void DoEncrypt(HWND hDlg)
{
	//---------------------------
	// Get the encryption key from the text box
	UINT keyLen = SendDlgItemMessage(hDlg, IDC_KEY, WM_GETTEXTLENGTH, 0, 0);
	if( keyLen == 0 )
	{
		MessageBox(hDlg, "You must enter a key to encrypt with", "Error", MB_OK);
		return;
	}

	acCArray<char> key;
	key.Allocate(keyLen+1, false); 
	key.SetLength(keyLen);
	GetDlgItemText(hDlg, IDC_KEY, key.AddressOf(), keyLen+1);

	// Make the key 16 chars (128 bit) by repeating characters
	char realKey[16];
	int n;
	for( n = 0; n < 16; n++ )
		realKey[n] = key[n%keyLen];

	//--------------------------
	// Get the text message that will be encrypted
	UINT textLen = SendDlgItemMessage(hDlg, IDC_MESSAGE, WM_GETTEXTLENGTH, 0, 0);
	if( textLen == 0 )
	{
		MessageBox(hDlg, "You must enter message to be encrypted", "Error", MB_OK);
		return;
	}

	acCArray<char> text;
	text.Allocate(textLen+1, false);
	text.SetLength(textLen);
	GetDlgItemText(hDlg, IDC_MESSAGE, text.AddressOf(), textLen*4+1);

	// The text must be larger than 4 bytes
	while( text.GetLength() <= 4 ) text.PushLast(0);

	// Padd text with nulls until even dividable with 4
	while( text.GetLength() & 3 ) text.PushLast(0);

	//------------------------
	// Encrypt the message
	TeaEncrypt((UINT*)text.AddressOf(), text.GetLength()/4, (UINT*)realKey);

	// Store the encrypted text for later decryption
	encryptedText.SetLength(text.GetLength());
	memcpy(encryptedText.AddressOf(), text.AddressOf(), text.GetLength());

	//--------------------------
	// Encode the encrypted text so that we can show it
	// Invisible characters will be substituted with '.'
	for( n = 0; n < text.GetLength(); n++ )
	{
		if( text[n] < 32 ) text[n] = '.';
		if( text[n] == 127 ) text[n] = '.';
	}

	// Add a final null character to terminate the string
	text.PushLast(0);

	// Show the encrypted message
	SetDlgItemText(hDlg, IDC_ENCRYPTED, text.AddressOf());
}

void DoDecrypt(HWND hDlg)
{
	//--------------------------
	// Get the encryption key from the text box
	UINT keyLen = SendDlgItemMessage(hDlg, IDC_KEY, WM_GETTEXTLENGTH, 0, 0);
	if( keyLen == 0 )
	{
		MessageBox(hDlg, "You must enter a key to decrypt with", "Error", MB_OK);
		return;
	}

	acCArray<char> key;
	key.Allocate(keyLen+1, false); 
	key.SetLength(keyLen);
	GetDlgItemText(hDlg, IDC_KEY, key.AddressOf(), keyLen+1);

	// Make the key 16 chars (128 bit) by repeating characters
	char realKey[16];
	int n;
	for( n = 0; n < 16; n++ )
		realKey[n] = key[n%keyLen];

	//----------------------
	// Retrieve the encrypted text for decryption
	acCArray<char> text;
	text.SetLength(encryptedText.GetLength());
	memcpy(text.AddressOf(), encryptedText.AddressOf(), encryptedText.GetLength());

	//-----------------------
	// Decrypt the message
	TeaDecrypt((UINT*)text.AddressOf(), text.GetLength()/4, (UINT*)realKey);

	//------------------------
	// Show the decrypted message
	
	// The string must be null terminated
	text.PushLast(0);

	SetDlgItemText(hDlg, IDC_DECRYPTED, text.AddressOf());
}

⌨️ 快捷键说明

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