?? usbcertpage.cpp
字號:
// UsbCertPage.cpp : implementation file
//
#include "stdafx.h"
#include "UsbKey.h"
#include "UsbCertPage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "..\ca.h"
/////////////////////////////////////////////////////////////////////////////
// CUsbCertPage property page
IMPLEMENT_DYNCREATE(CUsbCertPage, CPropertyPage)
CUsbCertPage::CUsbCertPage() : CPropertyPage(CUsbCertPage::IDD)
{
//{{AFX_DATA_INIT(CUsbCertPage)
m_Type = 0;
//}}AFX_DATA_INIT
}
CUsbCertPage::~CUsbCertPage()
{
}
void CUsbCertPage::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUsbCertPage)
DDX_Control(pDX, IDC_LIST_CERT, m_List);
DDX_Radio(pDX, IDC_R_PARSE, m_Type);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUsbCertPage, CPropertyPage)
//{{AFX_MSG_MAP(CUsbCertPage)
ON_BN_CLICKED(IDC_BPTOD, OnBptod)
ON_BN_CLICKED(IDC_B_PATH, OnBPath)
ON_BN_CLICKED(IDC_B_PUB, OnBPub)
ON_BN_CLICKED(IDC_B_KEY, OnBKey)
ON_BN_CLICKED(IDC_B_OK, OnBOk)
ON_BN_CLICKED(IDC_R_PARSE, OnRParse)
ON_BN_CLICKED(IDC_R_CREATE, OnRCreate)
ON_BN_CLICKED(IDC_R_CHANGEP, OnRChangep)
ON_BN_CLICKED(IDC_BDTOP, OnBdtop)
ON_BN_CLICKED(IDC_BSOUT, OnBsout)
ON_BN_CLICKED(IDC_BSIN, OnBsin)
ON_BN_CLICKED(IDC_B_CHECK, OnBCheck)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsbCertPage message handlers
BOOL CUsbCertPage::OnInitDialog()
{
CPropertyPage::OnInitDialog();
SetCheck();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/*void CUsbCertPage::OnBPri() //選擇私鑰
{
// TODO: Add your control notification handler code here
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"私鑰證書文件(*.crt;*.cer)|*.crt;*.cer|所有文件(*.*)|*.*||",NULL );
dlgOpen.m_ofn.lpstrTitle="選擇私鑰證書";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
m_PriKey=dlgOpen.GetPathName();
SetDlgItemText(IDC_EDIT_PRI,m_PriKey);
}
void CUsbCertPage::OnBChange()
{
// TODO: Add your control notification handler code here
CString msg,m_Password,m_Newword;
GetDlgItemText(IDC_EDIT_OLD,m_Password);
GetDlgItemText(IDC_EDIT_NEW,m_Newword);
//從私鑰文件獲得私鑰
UCHAR certbuff[3000];
asn__data *kk=(asn__data*)(new char[sizeof(asn__data)]);//私鑰
CFile f(m_PriKey,CFile::modeRead);
kk->length=f.Read(certbuff,3000);
kk->value=(UCHAR*)certbuff;
f.Close();
//用私鑰口令生成解密私鑰的對稱密鑰
UCHAR Dec_key[32]={0};
UINT4 Dec_keylen=32;
if(RTN_OK!=CreateKeyFromPassword(ATTRIB_SDBI_KEY,(UCHAR *)m_Password.GetBuffer(32),
m_Password.GetLength(),Dec_key,&Dec_keylen))
{
m_List.AddMsg("生成解密密鑰失敗。",M_ERROR);
delete kk;
return;
}
//用對稱密鑰解密私鑰
UCHAR data[1024]={0}; //解密后的私鑰文件
UINT4 datalen=1024; //解密后的私鑰文件長度
if(RTN_OK!=CryptionProc(ATTRIB_SDBI_KEY,Dec_key,Dec_keylen,DECRYPT,kk->value,kk->length,data,&datalen))
{
m_List.AddMsg("解密私鑰失敗",M_ERROR);
delete kk;
return;
}
UCHAR Enc_key[32]={0};
UINT4 Enc_keylen=32;
//生成加密密要
if(RTN_OK!=CreateKeyFromPassword(ATTRIB_SDBI_KEY,(UCHAR *)m_Newword.GetBuffer(32),
m_Newword.GetLength(),Enc_key,&Enc_keylen))
{
m_List.AddMsg("生成加密密鑰失敗。",M_ERROR);
delete kk;
return;
}
//用對稱密鑰加密私鑰
UCHAR outbuf[1024]; //加密后的私鑰文件
UINT4 outlen=1024; //加密后的私鑰文件長度
if(RTN_OK!=CryptionProc(ATTRIB_SDBI_KEY,Enc_key,Enc_keylen,ENCRYPT,data,datalen,outbuf,&outlen))
{
m_List.AddMsg("加密私鑰失敗",M_ERROR);
delete kk;
return;
}
else
{
f.Open(m_PriKey,CFile::modeCreate|CFile::modeWrite);
f.Write(outbuf,outlen);
f.Close();
m_List.AddMsg("修改私鑰密碼成功");
}
//用私鑰解密口令
delete kk;
}
*/
void CUsbCertPage::OnBPath() //設置輸出路徑,如果路徑下有文件則覆蓋
{
// TODO: Add your control notification handler code here
LPITEMIDLIST pidlRoot=NULL;
SHGetSpecialFolderLocation(m_hWnd,CSIDL_DRIVES ,&pidlRoot);
BROWSEINFO bi; //必須傳入的參數,下面就是這個結構的參數的初始化
CString strDisplayName; //用來得到,你選擇的活頁夾路徑,相當于提供一個緩沖區
bi.hwndOwner=GetSafeHwnd(); //得到父窗口Handle值
bi.pidlRoot=0; //這個變量就是我們在上面得到的.
bi.pszDisplayName=strDisplayName.GetBuffer(MAX_PATH+1); //得到緩沖區指針,
bi.lpszTitle="選擇文件輸出路徑,路徑下已有的文件將被覆蓋!!"; //設置標題
bi.ulFlags=BIF_RETURNONLYFSDIRS ; //設置標志
bi.lpfn=NULL;
bi.lParam=0;
bi.iImage=0; //上面這個是一些無關的參數的設置,最好設置起來,
ITEMIDLIST * pidl;
pidl=SHBrowseForFolder(&bi); //打開對話框
if(!pidl)
return;
SHGetPathFromIDList(pidl,bi.pszDisplayName);
strDisplayName.ReleaseBuffer(); //和上面的GetBuffer()相對應
m_Path=strDisplayName;
SetDlgItemText(IDC_EDIT_PATH,m_Path);
}
void CUsbCertPage::OnBPub() //選擇公鑰,選擇p12
{
// TODO: Add your control notification handler code here
if(m_Type==1)//選擇共鑰
{
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"公鑰證書文件(*.crt;*.cer)|*.crt;*.cer|所有文件(*.*)|*.*||",NULL);
dlgOpen.m_ofn.lpstrTitle="選擇公鑰證書文件";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
m_cert=dlgOpen.GetPathName();
SetDlgItemText(IDC_EDIT_P12,m_cert);
}
else//選擇p12
{
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"個人信息交換(*.db;*.pfx;*.p12)|*.db;*.pfx;*.p12|所有文件(*.*)|*.*||",NULL);
dlgOpen.m_ofn.lpstrTitle="選擇個人信息交換文件";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
m_p12=dlgOpen.GetPathName();
SetDlgItemText(IDC_EDIT_P12,m_p12);
}
}
void CUsbCertPage::OnBKey() //選擇私鑰
{
// TODO: Add your control notification handler code here
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"私鑰證書文件(*.crt;*.cer)|*.crt;*.cer|所有文件(*.*)|*.*||",NULL);
dlgOpen.m_ofn.lpstrTitle="選擇私鑰證書文件";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
m_key=dlgOpen.GetPathName();
SetDlgItemText(IDC_EDIT_KEY,m_key);
}
void CUsbCertPage::OnBOk() //執行操作
{
// TODO: Add your control notification handler code here
if(m_Type!=2&&m_Path=="")
{
m_List.AddMsg("請選擇輸出路徑!",M_WARING);
return;
}
if(m_Type==0)//提取證書操作
{
//讀取p12文件
CString pwd,pwd2;
GetDlgItemText(IDC_EDIT_PASSWORD,pwd);
/*if(pwd=="")
{
m_List.AddMsg("請給出解密口令!",M_WARING);
return;
}
*/
CString filein,certout,keyout;
certout=m_Path+"\\UserCert.crt";
keyout=m_Path+"\\UserPriKey.crt";
GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);
char out[100]={0};
if(!ParseDB(m_p12.GetBuffer(0),pwd.GetBuffer(0),certout.GetBuffer(0),keyout.GetBuffer(0),PEM,out))
m_List.AddMsg(out,M_ERROR);
else//ok
m_List.AddMsg("分解P12文件操作成功");
}
else if(m_Type==1)//合并證書操作
{
CString fileout;
fileout=m_Path+"\\Pkcs12.pfx";
CString pwd,pwd2;
GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);
// if(pwd2=="")
// {
// m_List.AddMsg("請給出加密口令!",M_WARING);
// return;
// }
GetDlgItemText(IDC_EDIT_PASSWORD,pwd);
char out[100]={0};
if(CreateDB(fileout.GetBuffer(0),pwd2.GetBuffer(0),m_cert.GetBuffer(0),
m_key.GetBuffer(0),out))
m_List.AddMsg(out);
else
m_List.AddMsg(out,M_ERROR);
}
else//修改密碼
{
CString pwd,pwd2,fileOut;
GetDlgItemText(IDC_EDIT_PASSWORD,pwd);
/* if(pwd=="")
{
m_List.AddMsg("請給出解密口令!",M_WARING);
return;
}
*/ GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);
/* if(pwd2=="")
{
m_List.AddMsg("請給出加密口令!",M_WARING);
return;
}
*/ char out[100]={0};
if(ChangePB(m_p12.GetBuffer(0),pwd.GetBuffer(0),pwd2.GetBuffer(0),m_p12.GetBuffer(0),out))
m_List.AddMsg(out);
else
m_List.AddMsg(out,M_ERROR);
}
}
void CUsbCertPage::SetCheck()
{
SetDlgItemText(IDC_EDIT_P12,"");
SetDlgItemText(IDC_EDIT_PASSWORD,"");
SetDlgItemText(IDC_EDIT_KEY,"");
SetDlgItemText(IDC_EDIT_PASSWORD2,"");
if(m_Type==0)
{
GetDlgItem(IDC_EDIT_P12)->EnableWindow(1);
SetDlgItemText(IDC_B_PUB,"選擇P12");
GetDlgItem(IDC_B_PUB)->EnableWindow(1);
GetDlgItem(IDC_EDIT_PASSWORD)->EnableWindow(1);
GetDlgItem(IDC_EDIT_KEY)->EnableWindow(0);
GetDlgItem(IDC_B_KEY)->EnableWindow(0);
GetDlgItem(IDC_EDIT_PASSWORD2)->EnableWindow(1);
}
else if(m_Type==1)
{
GetDlgItem(IDC_EDIT_P12)->EnableWindow(1);
SetDlgItemText(IDC_B_PUB,"選擇公鑰");
GetDlgItem(IDC_B_PUB)->EnableWindow(1);
GetDlgItem(IDC_EDIT_PASSWORD)->EnableWindow(1);
GetDlgItem(IDC_EDIT_KEY)->EnableWindow(1);
GetDlgItem(IDC_B_KEY)->EnableWindow(1);
GetDlgItem(IDC_EDIT_PASSWORD2)->EnableWindow(1);
}
else
{
GetDlgItem(IDC_EDIT_P12)->EnableWindow(1);
SetDlgItemText(IDC_B_PUB,"選擇P12");
GetDlgItem(IDC_B_PUB)->EnableWindow(1);
GetDlgItem(IDC_EDIT_PASSWORD)->EnableWindow(1);
GetDlgItem(IDC_EDIT_KEY)->EnableWindow(0);
GetDlgItem(IDC_B_KEY)->EnableWindow(0);
GetDlgItem(IDC_EDIT_PASSWORD2)->EnableWindow(1);
}
}
void CUsbCertPage::OnRParse() //提取證書操作
{
// TODO: Add your control notification handler code here
m_Type=0;
SetCheck();
}
void CUsbCertPage::OnRCreate() //合并證書操作
{
// TODO: Add your control notification handler code here
m_Type=1;
SetCheck();
}
void CUsbCertPage::OnRChangep() //修改密碼操作
{
// TODO: Add your control notification handler code here
m_Type=2;
SetCheck();
}
void CUsbCertPage::OnBsout() //選擇輸入文件
{
// TODO: Add your control notification handler code here
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"證書文件(*.crt;*.cer;*.pem;*.der;*.p12;*.pfx)|*.crt;*.cer;*.pem;*.der;*.p12;*.pfx|所有文件(*.*)|*.*||",NULL);
dlgOpen.m_ofn.lpstrTitle="選擇證書文件";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
SetDlgItemText(IDC_EDIT_INFILE,dlgOpen.GetPathName());
}
void CUsbCertPage::OnBsin() //選擇輸出文件
{
// TODO: Add your control notification handler code here
CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"證書文件(*.crt;*.cer;*.pem;*.der;*.p12;*.pfx)|*.crt;*.cer;*.pem;*.der;*.p12;*.pfx|所有文件(*.*)|*.*||",NULL);
dlgOpen.m_ofn.lpstrTitle="保存證書文件";//標題條
if(dlgOpen.DoModal()!=IDOK) return;
SetDlgItemText(IDC_EDIT_OUTFILE,dlgOpen.GetPathName());
}
void CUsbCertPage::OnBptod() //PEM-->DER
{
// TODO: Add your control notification handler code here
char out[100]={0};
//獲得輸入文件和輸出文件
CString infile,outfile;
GetDlgItemText(IDC_EDIT_INFILE,infile);
GetDlgItemText(IDC_EDIT_OUTFILE,outfile);
if(infile=="")
{
m_List.AddMsg("沒有選擇輸入文件",M_WARING);
return;
}
GetDlgItemText(IDC_EDIT_OUTFILE,outfile);
if(outfile=="")
{
m_List.AddMsg("沒有指定輸出文件",M_WARING);
return;
}
if(CertFormatConver(infile.GetBuffer(0),0,NULL,outfile.GetBuffer(0),DER,out))
m_List.AddMsg(out);
else
m_List.AddMsg(out,M_ERROR);
}
void CUsbCertPage::OnBdtop() //DER-->PEM
{
// TODO: Add your control notification handler code here
char out[100]={0};
CString infile,outfile;
GetDlgItemText(IDC_EDIT_INFILE,infile);
if(infile=="")
{
m_List.AddMsg("沒有選擇輸入文件",M_WARING);
return;
}
GetDlgItemText(IDC_EDIT_OUTFILE,outfile);
if(outfile=="")
{
m_List.AddMsg("沒有指定輸出文件",M_WARING);
return;
}
if(CertFormatConver(infile.GetBuffer(0),0,NULL,outfile.GetBuffer(0),PEM,out))
m_List.AddMsg(out);
else
m_List.AddMsg(out,M_ERROR);
}
void CUsbCertPage::OnBCheck()
{
// TODO: Add your control notification handler code here
char out[100]={0};
CString infile,outfile;
GetDlgItemText(IDC_EDIT_INFILE,infile);
if(infile=="")
{
m_List.AddMsg("沒有選擇公鑰文件",M_WARING);
return;
}
GetDlgItemText(IDC_EDIT_OUTFILE,outfile);
if(outfile=="")
{
m_List.AddMsg("沒有選擇私鑰文件",M_WARING);
return;
}
if(CertPairCheck(infile.GetBuffer(0),outfile.GetBuffer(0),out))
m_List.AddMsg("公私鑰對匹配");
else
{
m_List.AddMsg(out,M_ERROR);
}
// GeneratePair(out);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -