📄 testdialogdlg.cpp
字号:
// TestDialogDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestDialog.h"
#include "TestDialogDlg.h"
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
#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()
/////////////////////////////////////////////////////////////////////////////
// CTestDialogDlg dialog
CTestDialogDlg::CTestDialogDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDialogDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDialogDlg)
m_strEncryptingData = _T("");
m_strDecryptingData = _T("");
m_strPassword = _T("");
m_strSignatingData = _T("");
m_strPublicKey = _T("");
m_strPrivateKey = _T("");
m_strSignatedData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
CTestDialogDlg::~CTestDialogDlg()
{
if(m_hCryptProv)
CryptReleaseContext(m_hCryptProv, 0);
}
void CTestDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDialogDlg)
DDX_Text(pDX, IDC_EDIT1, m_strEncryptingData);
DDX_Text(pDX, IDC_EDIT2, m_strDecryptingData);
DDX_Text(pDX, IDC_EDIT_TEMP, m_strPassword);
DDX_Text(pDX, IDC_EDIT3, m_strSignatingData);
DDX_Text(pDX, IDC_EDIT6, m_strSignatedData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDialogDlg, CDialog)
//{{AFX_MSG_MAP(CTestDialogDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDialogDlg message handlers
BOOL CTestDialogDlg::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
UINT pControls[] = {IDC_EDIT1, IDC_EDIT_TEMP, IDC_EDIT2,
IDC_EDIT3, IDC_EDIT6};
EnableControls(pControls, sizeof(pControls)/sizeof(UINT), FALSE);
EnableScreen(FALSE, TRUE);
EnableScreen(FALSE, FALSE);
m_hCryptProv = InitCryptProv("");
if(m_hCryptProv == NULL) InitCryptProv("", 0, CRYPT_NEWKEYSET);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDialogDlg::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 CTestDialogDlg::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 CTestDialogDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDialogDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
HCRYPTKEY hKey = NULL;
hKey = CreateSessionKey(m_strPassword, ENCRYPT_ALGORITHM, CRYPT_CREATE_SALT);
BYTE* pbBuffer = EncryptData(hKey, m_strEncryptingData);
m_strDecryptingData.Format("%s", pbBuffer);
if(hKey) CryptDestroyKey(hKey);
m_strEncryptingData = _T("");
UpdateData(FALSE);
}
void CTestDialogDlg::OnButton2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
HCRYPTKEY hKey = NULL;
hKey = CreateSessionKey(m_strPassword, ENCRYPT_ALGORITHM, CRYPT_CREATE_SALT);
BYTE* pbBuffer = DecryptData(hKey, m_strDecryptingData);
m_strEncryptingData.Format("%s", pbBuffer);
if(hKey) CryptDestroyKey(hKey);
m_strDecryptingData = _T("");
UpdateData(FALSE);
}
void CTestDialogDlg::EnableScreen(BOOL blEncrypt, BOOL blIsSymmetry)
{
UINT pControlFalse[] = {IDC_EDIT1};
UINT pControlTrue[] = {IDC_EDIT1, IDC_EDIT_TEMP, IDC_EDIT2};
UINT pControlF[] = {IDC_EDIT3};
UINT pControlT[] = {IDC_EDIT6};
int i = 0;
if(blIsSymmetry)
{
if(blEncrypt)
{
EnableControls(pControlTrue, sizeof(pControlTrue)/sizeof(UINT), TRUE);
}
else
{
EnableControls(pControlFalse, sizeof(pControlFalse)/sizeof(UINT), TRUE);
}
}
else
{
if(blEncrypt)
{
EnableControls(pControlT, sizeof(pControlT)/sizeof(UINT), TRUE);
}
else
{
EnableControls(pControlF, sizeof(pControlF)/sizeof(UINT), TRUE);
}
}
}
void CTestDialogDlg::EnableControls(UINT uArray[], UINT uSize, BOOL blEnable)
{
for(UINT uIndex = 0; uIndex < uSize; uIndex++)
{
CWnd* pWnd = GetDlgItem(uArray[uIndex]);
if(pWnd) pWnd->EnableWindow(blEnable);
}
}
void CTestDialogDlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
EnableScreen(TRUE, TRUE);
}
HCRYPTPROV CTestDialogDlg::InitCryptProv(CString strCSPName, DWORD dwCSPType/*=PROV_RSA_FULL*/, DWORD dwFlags/*=0*/)
{
HCRYPTPROV hCryptProv = NULL;
TRY
{
CryptAcquireContext(
&hCryptProv,
NULL,
strCSPName,
dwCSPType,
dwFlags);//创建密钥容器
}
CATCH(CException, e) {
e->ReportError();
return NULL;
} END_CATCH
return hCryptProv;
}
HCRYPTKEY CTestDialogDlg::CreateSessionKey(CString strPasswords, UINT AlgID, DWORD dwFlag)
{
HCRYPTKEY hKey = NULL;
HCRYPTHASH hHash;
char* pzPasswords = (LPTSTR)(LPCTSTR)strPasswords;
// Create a hash object.
if(!CryptCreateHash(
m_hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
MessageBox("Cannot create a Hash table.");
return NULL;
}
//--------------------------------------------------------------------
// create a hash deque with the input passwords
if(!CryptHashData(
hHash,
(BYTE *)pzPasswords,
strlen(pzPasswords),
0))
{
MessageBox("Cannot create hash deque with the passwords.");
return NULL;
}
//--------------------------------------------------------------------
// Gernerate a session key with the hash deque
if(!CryptDeriveKey(
m_hCryptProv,
AlgID,//ENCRYPT_ALGORITHM,
hHash,
dwFlag,//KEYLENGTH,
&hKey))
{
MessageBox("Cannot Gernerate a session key.");
return NULL;
}
//--------------------------------------------------------------------
// Destroy the hash object.
if(hHash)
CryptDestroyHash(hHash);
hHash = NULL;
return hKey;
}
BYTE* CTestDialogDlg::EncryptData(HCRYPTKEY hKey, CString strEncrypting)
{
//Create the session key
// char* pzPasswords = (LPTSTR)(LPCTSTR)m_strPasswords;
// HCRYPTKEY hKey = NULL;
// hKey = CreateSessionKey(pzPasswords, ENCRYPT_ALGORITHM, CRYPT_CREATE_SALT);
BYTE* pbBuffer;
DWORD dwBufferLen = 0;
DWORD dwCount;
DWORD dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
//--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block.
if(ENCRYPT_BLOCK_SIZE > 1)
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
else
dwBufferLen = dwBlockLen;
// Allocate memory.
pbBuffer = (BYTE *)malloc(dwBufferLen);
if(NULL == pbBuffer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -