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

📄 aencrypt.cpp

📁 suitable file encryption and decryption aes and rijidael
💻 CPP
字号:
// AEncrypt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "AEncrypt.h"
#include "FileEx.h"
#include "Rijndael.h"

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

const TCHAR szFilename[] = _T("Archive.dat");
/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

UINT Encrypt(const BYTE *pBufIn, UINT nSize, BYTE *pBufOut, DWORD dwParam)
{
	return ((Rijndael*)dwParam)->padEncrypt(pBufIn, nSize, pBufOut);
}

UINT Decrypt(const BYTE *pBufIn, UINT nSize, BYTE *pBufOut, DWORD dwParam)
{
	return ((Rijndael*)dwParam)->padDecrypt(pBufIn, nSize, pBufOut);
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		BYTE pKey[32]={0};	// This is the key for the encryption
		for (int i = 0; i < sizeof(pKey); i++)	// Initialize it with dummy numbers
			pKey[i] = i;
		Rijndael rijEncrypt, rijDecrypt;
		if (rijEncrypt.init(Rijndael::CBC, Rijndael::Encrypt, pKey, Rijndael::Key32Bytes) == RIJNDAEL_SUCCESS && rijDecrypt.init(Rijndael::CBC, Rijndael::Decrypt, pKey, Rijndael::Key32Bytes) == RIJNDAEL_SUCCESS)
		{
			CFileEx f(4096);	// Initialize the file's buffer
			f.SetEncryption(&Encrypt, (DWORD)&rijEncrypt, &Decrypt, (DWORD)&rijDecrypt); // Set the encryption callbacks
			if (f.Open(szFilename, CFile::modeCreate | CFile::modeWrite))	// Create the file
			{
				//	We now insert some data to the file
				CString szString(_T("Hello World"));
				int		nInteger = 12345;
				double	dDouble = 123.45;
				
				// Since the Rijndael encryption takes a fixed byte block (16 byte),
				// we need to initialize the archive buffer to one byte LESS than the file's buffer

				CArchive arStore(&f, CArchive::store, 4095);
				
				arStore << szString << nInteger << dDouble;	// Let's store the data
				arStore.Close();
				f.Close();
				
				szString.Empty();	// We clear the data so w'll be sure it is read from the file
				nInteger = 0;
				dDouble = 0;
				
				if (f.Open(szFilename, CFile::modeRead))
				{
					CArchive arLoad(&f, CArchive::load, 4096);	// This is normal initialization
					arLoad >> szString >> nInteger >> dDouble;	// Read the data
					arLoad.Close();
					f.Close();
					cout << '\"'<<(LPCTSTR)szString << _T("\",") << nInteger << ',' <<dDouble << endl;
				}
				else
					cout << _T("Can't open file !") << endl;
			}
			else
				cout << _T("Can't create file !") << endl;
		}
		else
			cout << _T("Error initializing encryption structures !") << endl;
	}
	
	return nRetCode;
}


⌨️ 快捷键说明

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