⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbcertpage.cpp

📁 MiniCA减肥修正版 大部分源码来自王雪松的原作以及OpenSSL。(文件名基本保持原样
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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;
	n_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);
	DDX_Radio(pDX, IDC_R_TODER, n_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_R_CHANGEP2, OnRChangep2)
	ON_BN_CLICKED(IDC_R_TODER, OnTODER)
	ON_BN_CLICKED(IDC_R_TOPEM, OnTOPEM)
	ON_BN_CLICKED(IDC_R_TOPVK, OnTOPVK)
	ON_BN_CLICKED(IDC_R_VERIFY, OnVERIFY)
	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();
	SendDlgItemMessage(IDC_COMBO_CRYPT1,CB_SETCURSEL,25,0);//"des-ede3-cbc"
	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
	CFileDialog dlgOpen(false,NULL,NULL,OFN_HIDEREADONLY,
	"输出文件(*.*)|*.*||",NULL);
	dlgOpen.m_ofn.lpstrTitle="选择输出文件";//标题条
	if(dlgOpen.DoModal()!=IDOK) return;
	m_Path=dlgOpen.GetPathName();
	int Len=m_Path.ReverseFind('\\'); Len++;
	m_Path=m_Path.Left(Len)+dlgOpen.GetFileTitle();
	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_FILEMUSTEXIST,
			"公钥证书文件(*.crt,*.cer,*.der,*.pem)|*.crt;*.cer;*.der;*.pem||",NULL);
		dlgOpen.m_ofn.lpstrTitle="选择公钥证书文件";//标题条
		if(dlgOpen.DoModal()!=IDOK) return;
//		m_cert=dlgOpen.GetPathName();
		SetDlgItemText(IDC_EDIT_P12,dlgOpen.GetPathName());
		
	}
	else//选择p12
	{
		CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
			"个人信息交换(*.pfx,*.p12)|*.pfx;*.p12||",NULL);
		dlgOpen.m_ofn.lpstrTitle="选择个人信息交换文件";//标题条
		if(dlgOpen.DoModal()!=IDOK) return;
//		m_p12=dlgOpen.GetPathName();
		SetDlgItemText(IDC_EDIT_P12,dlgOpen.GetPathName());
	
	}
}

void CUsbCertPage::OnBKey() //选择私钥
{
	// TODO: Add your control notification handler code here
	CFileDialog dlgOpen(true,NULL,NULL,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST,
	"私钥证书文件(*.prk,*.pvk,*.pem)|*.prk;*.pvk;*.pem||",NULL);
	dlgOpen.m_ofn.lpstrTitle="选择私钥证书文件";//标题条
	if(dlgOpen.DoModal()!=IDOK) return;
//	m_key=dlgOpen.GetPathName();
	SetDlgItemText(IDC_EDIT_KEY,dlgOpen.GetPathName());
	
}

void CUsbCertPage::OnBOk() //执行操作
{
	// TODO: Add your control notification handler code here
	if(m_Path=="") 
	{
		m_List.AddMsg("请选择输出文件!",M_WARING);
		return;
	}
	
	if(m_Type==0)//提取证书操作
	{
		//读取p12文件
		CString pwd,m_p12;
		GetDlgItemText(IDC_EDIT_P12,m_p12);
		if(m_p12=="")
		{
			m_List.AddMsg("请选择P12文件!",M_WARING);
			return;
		}
		GetDlgItemText(IDC_EDIT_PASSWORD,pwd);
		CString filein,outCert,outKey;
		outCert=m_Path+".cer";
		outKey=m_Path+".prk";
//		GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);

		char *out,buf1[4096],buf2[4096];
		int len1=4096,len2=4096;
//		if(!ParseDB(m_p12.GetBuffer(0),pwd.GetBuffer(0),certout.GetBuffer(0),keyout.GetBuffer(0),PEM,out))
//	int P12Pick(char *P12file,char *passwd,char *certdata,int *certlen,char *keydata,int *keylen,int otype)
		if(P12Pick(m_p12.GetBuffer(0),pwd.GetBuffer(0),buf1,&len1,buf2,&len2,PEM))
		{
			FILE * pfc=fopen(outCert,"wb");
			fwrite(buf1,sizeof(char),len1,pfc);
			fclose(pfc);
			pfc=fopen(outKey,"wb");
			fwrite(buf2,sizeof(char),len2,pfc);
			fclose(pfc);
			m_List.AddMsg("分拆P12文件成功");	
		}
		else
		{
			out=(char *)Get_Func_Err(0);
			m_List.AddMsg(out,M_ERROR);
		}
	}

	else if(m_Type==1)//合并证书操作
	{
		CString fileout;
		fileout=m_Path+".pfx";
		CString pwd3,pwd2,certIn,keyIn;

		GetDlgItemText(IDC_EDIT_P12,certIn);
		GetDlgItemText(IDC_EDIT_KEY,keyIn);
		if(certIn=="")
		{
			m_List.AddMsg("请选择公钥文件!",M_WARING);
			return;
		}
		if(keyIn=="")
		{
			m_List.AddMsg("请选择私钥文件!",M_WARING);
			return;
		}
		GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);
		GetDlgItemText(IDC_EDIT_PASSWORD3,pwd3);
		if(pwd2!=pwd3)
		{
			m_List.AddMsg("新设密码和重复密码不一致!",M_WARING);
			return;
		}
		
		char *out;
//		if(CreateDB(fileout.GetBuffer(0),pwd2.GetBuffer(0),m_cert.GetBuffer(0),
//			m_key.GetBuffer(0),out))
//	int P12Pack(char * strP12,char * strPwd,char * NiceName,char * strCert,int plen,char * strkey,int klen)
		if(P12Pack(fileout.GetBuffer(0),pwd2.GetBuffer(0),"OurCA",certIn.GetBuffer(0),0,keyIn.GetBuffer(0),0))
		{
			m_List.AddMsg("合并P12文件成功");
		}
		else
		{
			out=(char *)Get_Func_Err(0);
			m_List.AddMsg(out,M_ERROR);
		}
	}
	else if(m_Type==2)	//修改P12密码
	{
		CString pwd,pwd2,pwd3,filein,fileOut;
		GetDlgItemText(IDC_EDIT_P12,filein);
		if(filein=="")
		{
			m_List.AddMsg("请选择P12文件!",M_WARING);
			return;
		}
		GetDlgItemText(IDC_EDIT_PASSWORD,pwd);
		GetDlgItemText(IDC_EDIT_PASSWORD2,pwd2);
		GetDlgItemText(IDC_EDIT_PASSWORD3,pwd3);
		if(pwd2!=pwd3)
		{
			m_List.AddMsg("新设密码和重复密码不一致!",M_WARING);
			return;
		}

		char *out;
		fileOut=m_Path+".pfx";
//		if(ChangePB(m_p12.GetBuffer(0),pwd.GetBuffer(0),pwd2.GetBuffer(0),m_p12.GetBuffer(0),out))
//	int ChangeP12PW(char * strP12,char * strPwd,char * strPwd2,char * NiceName,char * strOutP12)
		if(ChangeP12PW(filein.GetBuffer(0),pwd.GetBuffer(0),pwd2.GetBuffer(0),"OurCA",fileOut.GetBuffer(0)))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -