如果需要加密配置文件,或者某些文件的,请到上面gitub上去下。有什么意见,大家提一下。
采用的base64加上AES16字节秘钥加密,安全非常高,比较实用的程序,接口也很简单。
#pragma once
enum EncError
{
ENC_OK = 0,
ENC_FILE_ERR = 1,
ENC_MEMORY_ERR = 2,
};
//--------------------------------------------------------------------
//函数名 : EncFile
//函数功能描述 :加密文件(通过AES + base64加密)
//函数参数 :inFile明文文件名, outFile密文文件名 KEY-16字节秘钥
//函数返回值 : 0成功 1文件打开错 2内存分配错误
//作者 :Andy
//函数创建日期 : 2016-8-23
//函数修改日期 : 2016-8-26
//修改人 :Andy
//修改原因 :
//版本 : 1.02
//历史版本 : 1.01
//--------------------------------------------------------------------
int EncFile(const char *inFile, const char *outFile, const char *KEY);
//--------------------------------------------------------------------
//函数名 : DecFile
//函数功能描述 :解密文件
//函数参数 :inFile密文文件名, outFile明文文件名 KEY-16字节秘钥
//函数返回值 : 0成功 -1文件打开错 -2内存分配错误
//作者 :Andy
//函数创建日期 : 2016-8-23
//函数修改日期 : 2016-8-26
//修改人 :Andy
//修改原因 :
//版本 : 1.02
//历史版本 : 1.01
//--------------------------------------------------------------------
int DecFile(const char *inFile, const char *outFile, const char *KEY);
// AES_Ini.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "TestMain.h"
#include "Algorithm/EncFile.h"
#include "SysUtils.h"
#include "inifiles.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
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
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
CString FileDir = GetExeFileDir();
CString plainTextFile = FileDir + "\\set.ini";
CString encFile = FileDir + "\\set_enc.ini";
CString decFile = FileDir + "\\set_restored.ini";
char KEY[17] = "0123456789abcdef"; //必须是16字节
int ret = EncFile(plainTextFile, encFile, KEY);
if (ret == ENC_OK)
{
printf("解密成功!\n");
}
else if (ret == ENC_FILE_ERR)
{
printf("EncFile内存分配错误\n");
}
else if (ret == ENC_FILE_ERR)
{
printf("EncFile-文件打开错误\n");
}
ret = DecFile(encFile, decFile, KEY);
if (ret == ENC_OK)
{
printf("解密成功!\n");
}
else if (ret == ENC_FILE_ERR)
{
printf("EncFile内存分配错误\n");
}
else if (ret == ENC_FILE_ERR)
{
printf("EncFile-文件打开错误\n");
}
TIniFile *ini = new TIniFile(decFile);
printf("天道酬勤键值:%s\n", ini->ReadString("APP", "天道酬勤", "路"));
}
system("pause");
return nRetCode;
}