📄 krbtestdlg.cpp
字号:
// krbtestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "krbtest.h"
#include "krbtestDlg.h"
#include "openssl/evp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CKrbtestDlg dialog
CKrbtestDlg::CKrbtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKrbtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CKrbtestDlg)
m_Path = _T("");
m_MsgBox = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CKrbtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CKrbtestDlg)
DDX_Control(pDX, IDC_SIGN, m_Sign);
DDX_Control(pDX, IDC_ENCODE, m_Encode);
DDX_Control(pDX, IDC_DESIGN, m_Design);
DDX_Control(pDX, IDC_DECODE, m_Decode);
DDX_Control(pDX, IDC_ENCRYPT, m_Ecrypt);
DDX_Control(pDX, IDC_DIGEST, m_Digest);
DDX_Control(pDX, IDC_DECRYPT, m_Decrypt);
DDX_Text(pDX, IDC_EDIT1, m_Path);
DDX_Text(pDX, IDC_EDIT2, m_MsgBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CKrbtestDlg, CDialog)
//{{AFX_MSG_MAP(CKrbtestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_DIGEST, OnDigest)
ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
ON_BN_CLICKED(IDC_BUTBRO, OnButbro)
ON_BN_CLICKED(IDC_SIGN, OnSign)
ON_BN_CLICKED(IDC_ENCODE, OnEncode)
ON_BN_CLICKED(IDC_DESIGN, OnSignVerify)
ON_BN_CLICKED(IDC_DECODE, OnDecode)
ON_BN_CLICKED(IDOK, OnAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKrbtestDlg message handlers
BOOL CKrbtestDlg::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
((CButton*)GetDlgItem(IDC_RADIO_MD5))->SetCheck(TRUE);
((CButton*)GetDlgItem(IDC_RADIO2_MD5))->SetCheck(TRUE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CKrbtestDlg::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 CKrbtestDlg::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 CKrbtestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CKrbtestDlg::OnEncrypt()
{
UpdateData(TRUE); //将要加密的文件路径加载文档中 由view--->doc
if(m_Path.GetLength() <= 0)
{
m_MsgBox = "请选择要进行加密的文件\r\n";
UpdateData(FALSE);
return;
}
char SavePath[MAX_PATH];
memset(SavePath,0,MAX_PATH);
strcpy(SavePath,(char*)(LPCSTR)m_Path);
strcat(SavePath,".enc");
FILE *fp = fopen((char*)(LPCSTR)m_Path,"rb");//将CString转换成char类
FILE *fpout = fopen(SavePath,"wb"); //打开将要写密钥文件的句柄
if(fp == NULL||fpout == NULL)
{
return;
}
int len = 0;
char txt[1024];
int rel = 0;
int outlen = 0;
char outbuf[2048];
unsigned char key[] = "610039"; //密钥
unsigned char iv[] = "7855555"; //初始向量
memset(txt,0,1024);
memset(outbuf,0,2048);
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
rel = EVP_EncryptInit_ex(&ctx,EVP_des_ecb(),NULL,key,iv);
if(!rel)
goto ERR;
while(!feof(fp))
{
len = fread(txt,1,1024,fp);
rel = EVP_EncryptUpdate(&ctx,(unsigned char*)outbuf,&outlen,(unsigned char*)txt,len);
if(!rel)
goto ERR;
fwrite(outbuf,1,outlen,fpout);
}
rel = EVP_EncryptFinal(&ctx,(unsigned char*)outbuf,&outlen);
if(!rel)
goto ERR;
fwrite(outbuf,1,outlen,fpout);
fclose(fp);
fclose(fpout);
m_MsgBox = "加密后的文件数据文件为原文件名加上.enc\r\n";
m_MsgBox += "\n";
m_MsgBox += "加密完成!";
UpdateData(FALSE);
return;
ERR:
m_MsgBox += "加密错误!";
UpdateData(FALSE);
return ;
}
void CKrbtestDlg::OnDigest()
{
UpdateData(TRUE);
if(m_Path.GetLength() <= 0)
{
m_MsgBox = "请选择要摘要的文件\r\n";
UpdateData(FALSE);
return;
}
char inbuf[1024];
char outbuf[2048];
int inlen = 0;
int i = 0;
char SavePath[MAX_PATH];
memset(SavePath,0,MAX_PATH);
strcpy(SavePath,(char*)(LPCSTR)m_Path);
strcat(SavePath,".digest");
int outlen = 0;
memset(inbuf,0,sizeof(inbuf));
memset(outbuf,0,sizeof(outbuf));
FILE *fpin = NULL;
FILE *fpout = NULL;
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
int rel = 0;
if(((CButton*)GetDlgItem(IDC_RADIO_MD5))->GetCheck() == 1)
{
rel = EVP_DigestInit_ex(&ctx,EVP_md5(),NULL);
}
else
{
rel = EVP_DigestInit_ex(&ctx,EVP_sha1(),NULL);
}
if (!rel) goto ERR;
fpin = fopen((char *)(LPCSTR)m_Path,"rb");
fpout = fopen(SavePath,"wb");
if(!fpin||!fpout)
{
return;
}
while(!feof(fpin))
{
inlen = fread(inbuf,1,1024,fpin);
rel = EVP_DigestUpdate(&ctx,inbuf,inlen);
if(!rel)
goto ERR;
}
inlen = 0;
memset(inbuf,0,sizeof(inbuf));
EVP_DigestFinal_ex(&ctx,(unsigned char *)outbuf,(unsigned int *)&outlen);
EVP_MD_CTX_cleanup(&ctx);
fwrite(outbuf,1,outlen,fpout);
fclose(fpin);
fclose(fpout);
m_MsgBox = "文件摘要成功!\r\n";
UpdateData(FALSE);
return;
ERR:
m_MsgBox = "文件摘要失败!\r\n";
UpdateData(FALSE);
return ;
}
void CKrbtestDlg::OnDecrypt()
{
UpdateData(TRUE);
if(m_Path.GetLength() <= 0)
{
m_MsgBox = "请选择要进行解密的文件!\r\n";
UpdateData(FALSE);
return;
}
char SavePath[MAX_PATH];
memset(SavePath,0,MAX_PATH);
strcpy(SavePath,(char*)(LPCSTR)m_Path);
strcat(SavePath,".dec");
FILE *fp = fopen((char*)(LPCSTR)m_Path,"rb");
FILE *fpout = fopen(SavePath,"wb");
if(fp == NULL||fpout == NULL)
{
return;
}
int len = 0;
char txt[1024];
int rel = 0;
int outlen = 0;
char outbuf[2048];
unsigned char key[] = "610039";
unsigned char iv[] = "7855555";
memset(txt,0,1024);
memset(outbuf,0,2048);
EVP_CIPHER_CTX ctx;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -