?? 文件保險箱dlg.cpp
字號:
// 文件保險箱Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "文件保險箱.h"
#include "文件保險箱Dlg.h"
#include "GetPassDlg.h"
#include <windows.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
ALG_LIST g_Alg_List[]=
{
{"ECB模式的DES算法",NID_des_ecb},
{"CBC模式的DES算法",NID_des_cbc},
{"CBC模式3DES算法",NID_des_ede3_cbc},
{"ECB模式3DES算法",NID_des_ede3_ecb},
{"CBC模式的IDEA算法",NID_idea_cbc},
{"ECB模式的IDEA算法",NID_idea_ecb},
{"128位的ECB模式的AES算法",NID_aes_128_ecb},
{"128位的CBC模式的AES算法",NID_aes_128_cbc},
{"192位的ECB模式的AES算法",NID_aes_192_ecb},
{"192位的CBC模式的AES算法",NID_aes_192_cbc},
{"256位的ECB模式的AES算法",NID_aes_256_ecb},
{"256位的CBC模式的AES算法",NID_aes_256_cbc},
{NULL,0}
};
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_COMBO1, m_ALGLIST);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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
m_ALGLIST.Clear();
for(int i=0;;i++)
{
if(g_Alg_List[i].nAlgID ==0)
{
break;
}
m_ALGLIST.InsertString(i,g_Alg_List[i].strAlgName);
}
m_ALGLIST.SetCurSel(i-1);
OpenSSL_add_all_algorithms();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//設(shè)置待加密的原文文件路徑
void CMyDlg::OnButton1()
{
static char BASED_CODE szFilter[] = "全部文件 (*.*)|*.*||";
CFileDialog filedlg(
TRUE,NULL,NULL,
OFN_EXPLORER,
szFilter);
if(filedlg.DoModal()==IDOK)
{
SetDlgItemText(IDC_EDIT1,filedlg.GetPathName());
}
}
//設(shè)置加密后的密文文件路徑
void CMyDlg::OnButton2()
{
static char BASED_CODE szFilter[] = "全部文件 (*.*)|*.*||";
CFileDialog filedlg(
TRUE,NULL,NULL,
OFN_EXPLORER,
szFilter);
if(filedlg.DoModal()==IDOK)
{
SetDlgItemText(IDC_EDIT2,filedlg.GetPathName());
}
}
//設(shè)置待解密的密文文件路徑
void CMyDlg::OnButton4()
{
static char BASED_CODE szFilter[] = "全部文件 (*.*)|*.*||";
CFileDialog filedlg(
TRUE,NULL,NULL,
OFN_EXPLORER,
szFilter);
if(filedlg.DoModal()==IDOK)
{
SetDlgItemText(IDC_EDIT3,filedlg.GetPathName());
}
}
//設(shè)置解密后的原文文件路徑
void CMyDlg::OnButton5()
{
static char BASED_CODE szFilter[] = "全部文件 (*.*)|*.*||";
CFileDialog filedlg(
TRUE,NULL,NULL,
OFN_EXPLORER,
szFilter);
if(filedlg.DoModal()==IDOK)
{
SetDlgItemText(IDC_EDIT4,filedlg.GetPathName());
}
}
//加密
void CMyDlg::OnButton3()
{
CString strPlainFilePath;
CString strCipherFilePath;
int nAlg_ID;
CString strPass;
GetPassDlg passdDlg;
//獲得原文和密文文件的路徑
GetDlgItemText(IDC_EDIT1,strPlainFilePath);
GetDlgItemText(IDC_EDIT2,strCipherFilePath);
if(strCipherFilePath.IsEmpty()||strPlainFilePath.IsEmpty())
{
AfxMessageBox("請輸入正確的文件路徑!");
return;
}
if(strCipherFilePath==strPlainFilePath)
{
AfxMessageBox("密文文件和原文文件不能為同一個文件!");
return;
}
//獲得密碼算法
nAlg_ID = g_Alg_List[m_ALGLIST.GetCurSel()].nAlgID;
#ifdef _DEBUG
AfxMessageBox(strPlainFilePath);
AfxMessageBox(strCipherFilePath);
AfxMessageBox(g_Alg_List[m_ALGLIST.GetCurSel()].strAlgName);
#endif
//獲得文件保護(hù)密碼
if(passdDlg.DoModal()==IDOK)
{
strPass = passdDlg.m_Pass;
}
else
{
AfxMessageBox("請輸入加密文件的密碼");
return;
}
#ifdef _DEBUG
AfxMessageBox(strPass);
#endif
if(Encrypt_File(strPlainFilePath,strCipherFilePath,nAlg_ID,strPass))
{
AfxMessageBox("加密文件成功");
}
else
{
AfxMessageBox("加密文件失敗");
}
}
//解密
void CMyDlg::OnButton6()
{
CString strPlainFilePath;
CString strCipherFilePath;
CString strPass;
GetPassDlg passdDlg;
//獲得原文和密文文件的路徑
GetDlgItemText(IDC_EDIT4,strPlainFilePath);
GetDlgItemText(IDC_EDIT3,strCipherFilePath);
if(strCipherFilePath.IsEmpty()||strPlainFilePath.IsEmpty())
{
AfxMessageBox("請輸入正確的文件路徑!");
return;
}
if(strCipherFilePath==strPlainFilePath)
{
AfxMessageBox("密文文件和原文文件不能為同一個文件!");
return;
}
#ifdef _DEBUG
AfxMessageBox(strPlainFilePath);
AfxMessageBox(strCipherFilePath);
#endif
//獲得文件保護(hù)密碼
if(passdDlg.DoModal()==IDOK)
{
strPass = passdDlg.m_Pass;
}
else
{
AfxMessageBox("請輸入加密文件的密碼");
return;
}
#ifdef _DEBUG
AfxMessageBox(strPass);
#endif
if(Decrypt_File(strCipherFilePath,strPlainFilePath,strPass))
{
AfxMessageBox("解密文件成功");
}
else
{
AfxMessageBox("解密文件失敗");
}
}
/**********************************************************************
函數(shù)名稱:Encrypt_File
函數(shù)功能:加密文件
處理過程:
1.根據(jù)選擇的密碼算法以及口令,生成key和iv。
2.把文件頭寫入密文文件
3.循環(huán)讀取原文文件數(shù)據(jù)加密后保存到密文文件路徑中。
參數(shù)說明:
strPstrPlainFilePath:[IN] CString,待加密的原文文件路徑
strCipherFilePath:[IN] CString,加密后的密文文件保存路徑
nAlg_ID:[IN] int 密碼算法ID
strPass:[IN] CString 口令
返回值:成功返回TRUE,否則返回FALSE
備注說明:密文文件由文件頭和密文數(shù)據(jù)組成,文件頭里記錄和加密算法信息。
************************************************************************/
BOOL CMyDlg::Encrypt_File(CString strPlainFilePath, CString strCipherFilePath, int nAlg_ID, CString strPass)
{
unsigned char key[EVP_MAX_KEY_LENGTH]; //保存密鑰的數(shù)組
unsigned char iv[EVP_MAX_KEY_LENGTH]; ////保存初始化向量的數(shù)組
EVP_CIPHER_CTX ctx; //EVP加密上下文環(huán)境
unsigned char out[1024]; //保存密文的緩沖區(qū)
int outl;
unsigned char in[1024]; //保存原文的緩沖區(qū)
int inl;
const EVP_CIPHER * cipher; //加密算法
int rv;
FILE *fpIn; //輸入文件句柄
FILE *fpOut; //輸出文件句柄
char enchead[128]={0}; //保存密文文件頭的數(shù)組
//根據(jù)算法ID獲得EVP_CIPHER算法
cipher = EVP_get_cipherbynid(nAlg_ID);
if(cipher==NULL)
{
return FALSE;
}
//打開待加密的原文文件
fpIn = fopen(strPlainFilePath.GetBuffer(0),"rb");
if(fpIn==NULL)
{
return FALSE;
}
//打開保存密文的文件
fpOut = fopen(strCipherFilePath.GetBuffer(0),"wb");
if(fpOut==NULL)
{
fclose(fpIn);
return FALSE;
}
strPlainFilePath.ReleaseBuffer();
strCipherFilePath.ReleaseBuffer();
//文件頭,保存算法信息
sprintf(enchead,"ALGID:%d\n",nAlg_ID);
fwrite(enchead,1,128,fpOut);
//根據(jù)口令、密碼算法生成key和iv
EVP_BytesToKey(cipher,EVP_md5(),NULL,(const unsigned char *)strPass.GetBuffer(0),strPass.GetLength(),1,key,iv);
//初始化ctx
EVP_CIPHER_CTX_init(&ctx);
//設(shè)置密碼算法、key和iv
rv = EVP_EncryptInit_ex(&ctx,cipher,NULL,key,iv);
if(rv != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
//以1K為單位,循環(huán)讀取原文,加密后后保存到密文文件。
for(;;)
{
inl = fread(in,1,1024,fpIn);
if(inl <= 0)//讀取原文結(jié)束
break;
rv = EVP_EncryptUpdate(&ctx,out,&outl,in,inl);//加密
if(rv != 1)
{
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
fwrite(out,1,outl,fpOut);//保存密文到文件
}
//加密結(jié)束
rv = EVP_EncryptFinal_ex(&ctx,out,&outl);
if(rv != 1)
{
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
fwrite(out,1,outl,fpOut);//保密密文到文件
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);//清除EVP加密上下文環(huán)境
return TRUE;
}
/**********************************************************************
函數(shù)名稱:Decrypt_File
函數(shù)功能:對加密文件解密
處理過程:
1.讀取文件頭獲得加密算法。
2.根據(jù)算法和口令生成key和iv
3.循環(huán)讀取原文文件數(shù)據(jù)解密,并保存在原文文件。
參數(shù)說明:
strCipherFilePath:[IN] CString,密文文件路徑
strPstrPlainFilePath:[IN] CString,解密后的原文文件保存路徑。
strPass:[IN] CString 口令
返回值:成功返回TRUE,否則返回FALSE
************************************************************************/
BOOL CMyDlg::Decrypt_File(CString strCipherFilePath, CString strPlainFilePath, CString strPass)
{
unsigned char key[EVP_MAX_KEY_LENGTH]; //保存密鑰的數(shù)組
unsigned char iv[EVP_MAX_KEY_LENGTH]; //保存初始化向量的數(shù)組
EVP_CIPHER_CTX ctx; //EVP加密上下文環(huán)境
unsigned char out[1024+EVP_MAX_KEY_LENGTH]; //保存解密后明文的緩沖區(qū)數(shù)組
int outl;
unsigned char in[1024]; //保存密文數(shù)據(jù)的數(shù)組
int inl;
const EVP_CIPHER * cipher; //加密算法
int rv;
FILE *fpIn; //輸入文件句柄
FILE *fpOut; //輸出文件句柄
char enchead[128]={0}; //保存密文文件頭的數(shù)組
int nAlg_ID=0; //加密算法
//打開待解密的密文文件
fpIn = fopen(strCipherFilePath.GetBuffer(0),"rb");
if(fpIn==NULL)
{
return FALSE;
}
//打開保存明文的文件
fpOut = fopen(strPlainFilePath.GetBuffer(0),"wb");
if(fpOut==NULL)
{
fclose(fpIn);
return FALSE;
}
strPlainFilePath.ReleaseBuffer();
strCipherFilePath.ReleaseBuffer();
//讀取密文文件頭,獲取加密算法
fread(enchead,1,128,fpIn);
sscanf(enchead,"ALGID:%d\n",&nAlg_ID);
if(nAlg_ID==0)
{
return FALSE;
}
//根據(jù)算法ID獲得EVP_CIPHER算法
cipher = EVP_get_cipherbynid(nAlg_ID);
if(cipher==NULL)
{
return FALSE;
}
//根據(jù)口令、密碼算法生成key和iv
EVP_BytesToKey(cipher,EVP_md5(),NULL,(const unsigned char *)strPass.GetBuffer(0),strPass.GetLength(),1,key,iv);
//初始化ctx
EVP_CIPHER_CTX_init(&ctx);
//設(shè)置解密的算法、key和iv
rv = EVP_DecryptInit_ex(&ctx,cipher,NULL,key,iv);
if(rv != 1)
{
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
//以1K為單位,循環(huán)讀取原文,解密后后保存到明文文件。
for(;;)
{
inl = fread(in,1,1024,fpIn);
if(inl <= 0)
break;
rv = EVP_DecryptUpdate(&ctx,out,&outl,in,inl);//解密
if(rv != 1)
{
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
fwrite(out,1,outl,fpOut);//保存明文到文件
}
//解密結(jié)束
rv = EVP_DecryptFinal_ex(&ctx,out,&outl);
if(rv != 1)
{
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);
return FALSE;
}
fwrite(out,1,outl,fpOut);//保存明文到文件
fclose(fpIn);
fclose(fpOut);
EVP_CIPHER_CTX_cleanup(&ctx);//清除EVP加密上下文環(huán)境
return TRUE;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -