问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

客户机安装证书 C++

发布网友 发布时间:2022-04-28 13:44

我来回答

1个回答

热心网友 时间:2023-10-11 03:00

#include "stdafx.h"
#include "CertImportx.h"

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void CCertImport::PrintfError(DWORD err , LPCTSTR szError)
{
if( err == 0 )
{
MessageBox(NULL,_T("安装成功!"),_T("证书安装"),MB_OK);
}
else
{//
MessageBox(NULL,_T("安装失败!"),_T("证书安装"),MB_OK);
}
}

// Global function for free handles...
void CCertImport::FreeHandles(HCERTSTORE hFileStore, PCCERT_CONTEXT pctx, HCERTSTORE pfxStore, HCERTSTORE myStore )
{

if (myStore)
CertCloseStore(myStore, 0);

if (pfxStore)
CertCloseStore(pfxStore, CERT_CLOSE_STORE_FORCE_FLAG);

if(pctx)
CertFreeCertificateContext(pctx);

if (hFileStore)
CertCloseStore(hFileStore, 0);
}

int CCertImport::ImportCACert()
{
const char* pCert= "-----BEGIN CERTIFICATE-----\
MIIEXDCCA0SgAwIBAgIEOGO5ZjANBgkqhkiG9w0BAQUFADCBtDEUMBIGA1UEChML\
RW50cnVzdC5uZXQxQDA+BgNVBAsUN3d3dy5lbnRydXN0Lm5ldC9DUFNfMjA0OCBp\
bmNvcnAuIGJ5IHJlZi4gKGxpbWl0cyBsaWFiLikxJTAjBgNVBAsTHChjKSAxOTk5\
IEVudHJ1c3QubmV0IExpbWl0ZWQxMzAxBgNVBAMTKkVudHJ1c3QubmV0IENlcnRp\
ZmljYXRpb24gQXV0aG9yaXR5ICgyMDQ4KTAeFw05OTEyMjQxNzUwNTFaFw0xOTEy\
MjQxODIwNTFaMIG0MRQwEgYDVQQKEwtFbnRydXN0Lm5ldDFAMD4GA1UECxQ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tm/Kq+4h4YhPATKt5Rof8886ZjXOP/swNlQ8C5LWK5Gb9Auw2DaclVy\
vUxFnmG6v4SBkgPR0ml8xQ==\
-----END CERTIFICATE-----";

BYTE pBinByte[8192]={0};
unsigned long binBytes = 4096;

CryptStringToBinaryA( pCert , strlen(pCert) ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);

return ImportCACert(pBinByte , binBytes );
}

// This function imports a CA certificate...
int CCertImport::ImportCACert(LPCTSTR szFileName)
{
HANDLE hfile = INVALID_HANDLE_VALUE;

BYTE pByte[4096] = {0} , pBinByte[8192]={0};
unsigned long bytesRead = 0;
unsigned long binBytes = 4096;

// Open it...
hfile = CreateFile(szFileName, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == hfile)
return -1;

ReadFile( hfile , pByte, 4096, &bytesRead ,NULL );
CloseHandle(hfile);

CryptStringToBinaryA( (LPCSTR)pByte , bytesRead ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);

return ImportCACert(pBinByte , binBytes );
}

int CCertImport::ImportCACert(BYTE* pBinByte , unsigned long binBytes)
{
HCERTSTORE pfxStore = 0;
HCERTSTORE myStore = 0;
HCERTSTORE hFileStore = 0;
PCCERT_CONTEXT pctx = NULL;
DWORD err = 0;

pctx = CertCreateCertificateContext(MY_ENCODING_TYPE, (BYTE*)pBinByte , binBytes );
if(pctx == NULL)
{
DWORD err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertCreateCertificateContext'") );
return err;
}

// we open the store for the CA
hFileStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root" );
if (!hFileStore)
{
DWORD err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertOpenStore'") );
return err;
}

if( !CertAddCertificateContextToStore(hFileStore, pctx, CERT_STORE_ADD_NEW, 0) )
{
err = GetLastError();
if( CRYPT_E_EXISTS == err )
{
// if( AfxMessageBox("An equivalent previous personal certificate already exists. Overwrite ? (Yes/No)", MB_YESNO) == IDYES)
{
if( !CertAddCertificateContextToStore(hFileStore, pctx , CERT_STORE_ADD_REPLACE_EXISTING, 0))
{
err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertAddCertificateContextToStore'") );
return err;
}
}
}
else
{
FreeHandles(hFileStore, pctx , pfxStore , myStore);
PrintfError( err , _T("Error in 'CertAddCertificateContextToStore'") );
return err;
}
}
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError(0 , NULL) ;
return 0;
}
复制的别人的,我也不懂C++。

参考资料:http://blog.sina.com.cn/s/blog_4e6c32b30100i3iq.html

热心网友 时间:2023-10-11 03:00

#include "stdafx.h"
#include "CertImportx.h"

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void CCertImport::PrintfError(DWORD err , LPCTSTR szError)
{
if( err == 0 )
{
MessageBox(NULL,_T("安装成功!"),_T("证书安装"),MB_OK);
}
else
{//
MessageBox(NULL,_T("安装失败!"),_T("证书安装"),MB_OK);
}
}

// Global function for free handles...
void CCertImport::FreeHandles(HCERTSTORE hFileStore, PCCERT_CONTEXT pctx, HCERTSTORE pfxStore, HCERTSTORE myStore )
{

if (myStore)
CertCloseStore(myStore, 0);

if (pfxStore)
CertCloseStore(pfxStore, CERT_CLOSE_STORE_FORCE_FLAG);

if(pctx)
CertFreeCertificateContext(pctx);

if (hFileStore)
CertCloseStore(hFileStore, 0);
}

int CCertImport::ImportCACert()
{
const char* pCert= "-----BEGIN CERTIFICATE-----\
MIIEXDCCA0SgAwIBAgIEOGO5ZjANBgkqhkiG9w0BAQUFADCBtDEUMBIGA1UEChML\
RW50cnVzdC5uZXQxQDA+BgNVBAsUN3d3dy5l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G9w0BAQUFAAOCAQEA\
WUesIYSKF8mciVMeuoCFGsY8Tj6xnLZ8xpJdGGQC49MGCBFhfGPjK50xA3B20qMo\
oPS7mmNz7W3lKtvtFKkrxjYR0CvrB4ul2p5cGZ1WEvVUKcgF7bISKo30Axv/55IQ\
h7A6tcOdBTcSo8f0FbnVpDkWm1M6I5HxqIKiaohowXkCIryqptau37AUX7iH0N18\
f3v/rxzP5tsHrV7bhZ3QKw0z2wTR5klAEyt2+z7pnIkPFc4YsIV4IU9rTw76NmfN\
B/L/CNDi3tm/Kq+4h4YhPATKt5Rof8886ZjXOP/swNlQ8C5LWK5Gb9Auw2DaclVy\
vUxFnmG6v4SBkgPR0ml8xQ==\
-----END CERTIFICATE-----";

BYTE pBinByte[8192]={0};
unsigned long binBytes = 4096;

CryptStringToBinaryA( pCert , strlen(pCert) ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);

return ImportCACert(pBinByte , binBytes );
}

// This function imports a CA certificate...
int CCertImport::ImportCACert(LPCTSTR szFileName)
{
HANDLE hfile = INVALID_HANDLE_VALUE;

BYTE pByte[4096] = {0} , pBinByte[8192]={0};
unsigned long bytesRead = 0;
unsigned long binBytes = 4096;

// Open it...
hfile = CreateFile(szFileName, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == hfile)
return -1;

ReadFile( hfile , pByte, 4096, &bytesRead ,NULL );
CloseHandle(hfile);

CryptStringToBinaryA( (LPCSTR)pByte , bytesRead ,CRYPT_STRING_BASE64HEADER , pBinByte , &binBytes ,NULL,NULL);

return ImportCACert(pBinByte , binBytes );
}

int CCertImport::ImportCACert(BYTE* pBinByte , unsigned long binBytes)
{
HCERTSTORE pfxStore = 0;
HCERTSTORE myStore = 0;
HCERTSTORE hFileStore = 0;
PCCERT_CONTEXT pctx = NULL;
DWORD err = 0;

pctx = CertCreateCertificateContext(MY_ENCODING_TYPE, (BYTE*)pBinByte , binBytes );
if(pctx == NULL)
{
DWORD err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertCreateCertificateContext'") );
return err;
}

// we open the store for the CA
hFileStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_STORE_OPEN_EXISTING_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root" );
if (!hFileStore)
{
DWORD err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertOpenStore'") );
return err;
}

if( !CertAddCertificateContextToStore(hFileStore, pctx, CERT_STORE_ADD_NEW, 0) )
{
err = GetLastError();
if( CRYPT_E_EXISTS == err )
{
// if( AfxMessageBox("An equivalent previous personal certificate already exists. Overwrite ? (Yes/No)", MB_YESNO) == IDYES)
{
if( !CertAddCertificateContextToStore(hFileStore, pctx , CERT_STORE_ADD_REPLACE_EXISTING, 0))
{
err = GetLastError();
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError( err , _T("Error in 'CertAddCertificateContextToStore'") );
return err;
}
}
}
else
{
FreeHandles(hFileStore, pctx , pfxStore , myStore);
PrintfError( err , _T("Error in 'CertAddCertificateContextToStore'") );
return err;
}
}
FreeHandles(hFileStore,pctx, pfxStore, myStore);
PrintfError(0 , NULL) ;
return 0;
}
复制的别人的,我也不懂C++。

参考资料:http://blog.sina.com.cn/s/blog_4e6c32b30100i3iq.html

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
!这叫什么号 百万医疗赔付后是否可以续保 前一年理赔过医疗险还能续保吗? 医疗住院险理赔后还能购买吗? 女生多大后可以不在长身高? 如何不用软件把手机投屏到电脑上手机屏幕怎样投放到电脑上 战时拒绝、故意延误军事订货罪既遂的处罚? 战时故意延误军事订货罪处罚标准 名师1+1导读方案:汤姆·索亚历险记目录 三星sm-g7200打开微信慢,无法正常收看,网速不慢。 网易云音乐如何查询开通流量使用情况? 黛西法罗和爱克佩特鞋码一样吗 DF是什么鞋子品牌 黛西法罗注册过商标吗?还有哪些分类可以注册? 黛西法罗鞋子有假的吗 黛西法罗是什么牌子? 如何设置按钮控件清除编辑框的内容? 唯美父女动漫图片一对两张 求图~要动漫的,可爱的,两个人的! 索尼相机和α5000相比较,哪个更好 SONY nex5t 5r ilce-5000(a5000)哪个好一些 索尼a5000拍有点距离的人像求推荐镜头 索尼a5000照虚化影像咋样 索尼A5000镜头为什么是泰国产的 能否推荐一两款配合a5100使用的比较优秀的中长焦镜头,最好是18-200这种,敬待您的回复。 索尼微单a5000是多少倍变焦 索尼a5000能拍清楚月亮和星星吗? 索尼微单a5000的镜头焦距是多少 a5000用什么镜头 女孩初潮在什么年龄 爱奇艺播放器安装不了是怎么回事? TCL电视怎么安装爱奇艺播放器 通联支付网络服务股份有限公司是什么意思莫名其妙扣钱 银行卡收到通联支付网络服务股份有限公司转的几百块钱,是怎么回事啊? 开通邮储银行通联支付网络服务是什么意思 有人了解通联支付网络服务股份有限公司吗? 今天下午突然收到通联支付网络股份有限公司的备付金是什么情况? 我的交易记录有一个被通联支付网络服务股份有限公司扣费,怎么回事,怎么办 有人知道通联支付网络服务股份有限公司吗? 微信重新注册了,怎么找回以前那个? 新注册时把原来的替换了如何找到原来的? “六一”儿童节快到了,小学的第一个“六一”儿童节,你准备怎样过? 另注册了,原来的微信就没有了,怎样才能找回原来的? 习家训 谈家风 写家书作文 争做新时代好队员主题队会 微信改绑号码原来的微信怎么找回 我姓刘祖辈分、习、爷爷、之字辈、父、福字辈、我是述字辈、下、劲、忠、传、请问:我的祖先何处 改了以后能不能找回来原来复原原来的微信账号怎么找回! 贵州女孩双脚烫伤植皮称后妈所为,爷爷发声说了些什么? 微信被覆盖了怎么才能找回以前的?