【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,CryptoSystemDlg.cpp,希望对大家有所帮助!
// CryptoSystemDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CryptoSystem.h"
#include "CryptoSystemDlg.h"
#include <wincrypt.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//确定使用RC2块编码或是RC4流式编码
#ifdef USE_BLOCK_CIPHER
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif
/////////////////////////////////////////////////////////////////////////////
// CCryptoSystemDlg dialog
CCryptoSystemDlg::CCryptoSystemDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCryptoSystemDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCryptoSystemDlg)
m_Password = _T("");
m_Path1 = _T("");
m_Path2 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCryptoSystemDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCryptoSystemDlg)
DDX_Text(pDX, IDC_EDIT1, m_Password);
DDV_MaxChars(pDX, m_Password, 24);
DDX_Text(pDX, IDC_EDIT2, m_Path1);
DDX_Text(pDX, IDC_EDIT3, m_Path2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCryptoSystemDlg, CDialog)
//{{AFX_MSG_MAP(CCryptoSystemDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DATAFILE, OnDatafile)
ON_BN_CLICKED(IDC_CRYPTOFILE, OnCryptofile)
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCryptoSystemDlg message handlers
BOOL CCryptoSystemDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
HCRYPTPROV hProv;
HCRYPTKEY hKey;
unsigned char szUserName[100];
DWORD dwUserNameLen = 100;
//试图获取缺省密钥容器句柄
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0))
{
//创建缺省密钥容器
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
AfxMessageBox("创建缺省密钥容器错误!");
PostQuitMessage(1);
}
//获取缺省密钥容器名
if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0))
szUserName[0] = 0;
CString str;
str.Format("创建密钥容器:%s",szUserName);
AfxMessageBox(str);
}
//视图得到签名密钥句柄
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
{
if(GetLastError() == NTE_NO_KEY)
{
//创建签名密钥对
AfxMessageBox("创建签名密钥对");
if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey))
{
AfxMessageBox("执行CryptGenKey()函数时错误!");
PostQuitMessage(1);
}
else
{
CryptDestroyKey(hKey);
}
}
else
{
AfxMessageBox("执行CryptGetUserKey()函数时错误!");
PostQuitMessage(1);
}
}
//试图获取交换密钥句柄
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey))
{
if(GetLastError()==NTE_NO_KEY)
{
//创建交换密钥对
AfxMessageBox("创建交换密钥对");
if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey))
{
AfxMessageBox("执行CryptGenKey()函数时错误!");
PostQuitMessage(1);
}
else
{
CryptDestroyKey(hKey);
}
}
else
{
AfxMessageBox("执行CryptGetUserKey()函数时错误!");
PostQuitMessage(1);
}
}
CryptReleaseContext(hProv,0);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CCryptoSystemDlg::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 CCryptoSystemDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCryptoSystemDlg::OnDatafile()
{
CFileDialog dlg(TRUE,"*","*.*",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"明文文件(*.*)|*.*||",NULL);
if(dlg.DoModal()==IDOK)
{
UpdateData(TRUE);
m_Path1=dlg.GetPathName();
UpdateData(FALSE);
}
}
void CCryptoSystemDlg::OnCryptofile()
{
CFileDialog dlg(TRUE,"cry","*.cry",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"密文文件(*.cry)|*.cry||",NULL);
if(dlg.DoModal()==IDOK)
{
UpdateData(TRUE);
m_Path2=dlg.GetPathName();
UpdateData(FALSE);
}
}
void CCryptoSystemDlg::OnEncrypt()
{
UpdateData(TRUE);
CFile Source,Destination;
if(!Source.Open(m_Path1,CFile::modeReadWrite))
{
AfxMessageBox("明文文件无法打开");
return;
}
if(!Destination.Open(m_Path2,CFile::modeCreate|CFile::modeReadWrite))
{
AfxMessageBox("密文文件无法打开");
return;
}
int eof=0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
HCRYPTHASH hHash = 0;
unsigned char* pbKeyBlob;
DWORD dwKeyBlobLen;
unsigned char* pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
// 连接缺省的CSP
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0);
if(m_Password.GetLength()==0) //口令为空,使用随机产生的会话密钥加密
{
// 产生随机会话密钥。
CryptGenKey(hProv, ENCRYPT_ALGORITHM, CRYPT_EXPORTABLE, &hKey);
// 取得密钥交换对的公共密钥
CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey);
// 计算隐码长度并分配缓冲区
CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen);
pbKeyBlob=new unsigned char[dwKeyBlobLen];
// 将会话密钥输出至隐码
CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen);
// 释放密钥交换对的句柄
CryptDestroyKey(hXchgKey);
hXchgKey = 0;
// 将隐码长度写入目标文件
Destination.Write(&dwKeyBlobLen,sizeof(DWORD));
//将隐码长度写入目标文件
Destination.Write(pbKeyBlob,dwKeyBlobLen);
delete pbKeyBlob;
}
else //口令不为空, 使用从口令派生出的密钥加密文件
{
CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);// 建立散列表
unsigned char temp[24];
for(int i=0;i<m_Password.GetLength();i++)
temp[i]=m_P
上一篇:
Batnball.frm
下一篇:
如何当一个轻松的班主任