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

📄 aes.cpp

📁 AES加密算法实现与测试
💻 CPP
字号:
// 4thTry.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <afx.h>
#include <fstream>

#ifdef _DEBUG
#  pragma comment ( lib, "cryptlib" )
#else
#  pragma comment ( lib, "cryptlib" )
#endif

#include <iostream>
using namespace std;

#include <cryptopp/aes.h>
using namespace CryptoPP;

int main(int argc, char* argv[])
{
	int key_length=32;
	cout << "AES Parameters: " << endl;
	cout << "Algorithm name : " << AES::StaticAlgorithmName() << endl;      

	//Crypto++库中一般用字节数来表示长度,而不是常用的字节数
	cout << "Block size     : " << AES::BLOCKSIZE * 8 << endl;
	cout << "Min key length : " << AES::MIN_KEYLENGTH * 8 << endl;
	cout << "Max key length : " << AES::MAX_KEYLENGTH * 8 << endl;
	


	CString name1="1.bmp";
	CString name2="hellow3.bmp";

	CFile rfile,wfile;
	rfile.Open(name1,CFile::modeRead);
	wfile.Open(name2,CFile::modeWrite|CFile::modeCreate);

	DWORD length=rfile.GetLength();

	//确定原始文件有多少个16字节的块
	long block_count;

	if((length-54)%AES::BLOCKSIZE==0)
	{
		block_count=(length-54)/AES::BLOCKSIZE;
	}
	else
	{
		block_count=(length-54)/AES::BLOCKSIZE+1;
	}

	BYTE head_buffer[54];
	BYTE *data_buffer=new BYTE[length-54];

	rfile.Read(head_buffer,54);
	wfile.Write(head_buffer,54);

	rfile.Read(data_buffer,length-54);

	AESEncryption aesEncryptor; //加密器
	
	unsigned char aesKey[AES::DEFAULT_KEYLENGTH];                   //密钥
	unsigned char inBlock[AES::BLOCKSIZE];    //要加密的数据块
	unsigned char outBlock[AES::BLOCKSIZE];                                 //加密后的密文块
	unsigned char xorBlock[AES::BLOCKSIZE];                                 //必须设定为全零
	
	cout << "\n开始加密 " <<endl;

	for(int j=0;j<block_count;j++)
	{
		memset( inBlock, 0, AES::BLOCKSIZE ); 
		memset( xorBlock, 0, AES::BLOCKSIZE ); 
		memset( outBlock, 0, AES::BLOCKSIZE ); 

		for(int k=0;k<AES::BLOCKSIZE;k++)
		{
			inBlock[k]=*(data_buffer+j*AES::BLOCKSIZE+k);
		}
		
		aesEncryptor.SetKey( aesKey, key_length/*AES::DEFAULT_KEYLENGTH */);  //设定加密密钥		
		aesEncryptor.ProcessAndXorBlock( inBlock, xorBlock, outBlock );  //加密

		wfile.Write(outBlock,AES::BLOCKSIZE);
	}

	rfile.Close();
	wfile.Close();


	
	//cout << "\n加密完成,处理时间是 "<< run_time_compute.dfTim <<endl;

	CString name3="hellow2.bmp";
	rfile.Open(name2,CFile::modeRead);
	wfile.Open(name3,CFile::modeWrite|CFile::modeCreate);

	rfile.Read(head_buffer,54);
	wfile.Write(head_buffer,54);

	rfile.Read(data_buffer,length-54);

	AESDecryption aesDecryptor; //加密器
		
	cout << "\n开始解密 " <<endl;

	for(j=0;j<block_count;j++)
	{
		memset( inBlock, 0, AES::BLOCKSIZE ); 
		memset( xorBlock, 0, AES::BLOCKSIZE ); 
		memset( outBlock, 0, AES::BLOCKSIZE ); 

		for(int k=0;k<AES::BLOCKSIZE;k++)
		{
			inBlock[k]=*(data_buffer+j*AES::BLOCKSIZE+k);
		}

		aesDecryptor.SetKey( aesKey, key_length/*AES::DEFAULT_KEYLENGTH */ );  //设定加密密钥		
		aesDecryptor.ProcessAndXorBlock( inBlock, xorBlock, outBlock );  //加密

		wfile.Write(outBlock,AES::BLOCKSIZE);
	}

	rfile.Close();
	wfile.Close();
	
	return 0;
}

⌨️ 快捷键说明

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