-
查看文章
PFX
证书导入到
< br>USBKEY(
代码
)
2008-10-24
17:29
5.
完整代码
#include
#include
#include
#include
// Global
Csp Handle
HCRYPTPROV hCryptProv =
NULL;
// Handle for a
cryptographic
void
ToSmartCard(HCERTSTORE hSystemStore)
{
PCCERT_CONTEXT pCertContext = NULL;
char
pszNameString[256];
DWORD
dwKeySpec;
HCRYPTKEY
UserKey = 0;
DWORD dwBlobLen;
BYTE
*pbKeyBlob;
HCRYPTKEY hPubKey = NULL;
int error;
//
CryptAcquireCertificatePrivateKey Exported Handle
HCRYPTPROV hCrypt = NULL;
//
Just for Test Provider Name
BYTE
pbData[1000];
// 1000
will hold the longest
// key container
name.
DWORD cbData;
cbData =
1000;
//-------------------------------
------------------------------------
//
Find the certificates in the system store.
// In fact, just have one.
while(pCertContext=
CertEnumCertificatesInStore(
hSystemStore,
pCertContext)) // on the first call to
the function,
// this
parameter is NULL
// on all subsequent calls,
// this parameter is the
last pointer
// returned by the function
{
//----------
--------------------------------------------------
----
// Do whatever is
needed for a current certificate.
// ...
//-----
--------------------------------------------------
-------------
// Find and
print the name of the subject of the certificate
// just
retrieved.
if(CertGetNameString(
pCertContext,
CERT_NAME_SIMPLE_DISPLAY_TYPE,
0,
NULL,
pszNameString,
128))
{
printf(
if(!(
CryptAcquireCertificatePrivateKey(
pCertContext,
0,
NULL,
&hCrypt, //&hCryptProv,
//
注意此处
hCrypt
输出不该是
hCryp
tProv
,如果没有猜错是
Microsoft
Base
那个
&dwKeySpec,
NULL)))
{
printf(
}
// Test
hCrypt, Try to get its name;
//
猜错了,不是
Microsoft
Base...,
而是下面这个
// Microsoft Enhanced Cryptographic
Provider v1.0
if(CryptGetProvParam(
hCrypt,
PP_NAME,
pbData,
&cbData,
0))
{
printf(
printf(
}
if( ! CryptGetUserKey(
hCrypt,
//not hCryptProv,
AT_KEYEXCHANGE, //dwKeySpec,
&UserKey
) )
{
printf(
}
//---------
--------------------------------------------------
---------
// Export the
public/private key-pair.
if(CryptExportKey(
UserKey,
NULL,
PRIVATEKEYBLOB,
0,
NULL,
&dwBlobLen))
{
printf(
}
else
{
printf(
exit(1);
}
//----------------------
----------------------------------------------
// Allocate memory for
the pbKeyBlob.
if(pbKeyBlob =
(BYTE*)malloc(dwBlobLen))
{
printf(
}
else
{
printf(
exit(1);
}
//----------------------
----------------------------------------------
// Do the actual
exporting into the key BLOB.
if(CryptExportKey(
UserKey,
NULL,
PRIVATEKEYBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
printf(
}
else
{
printf(
exit(1);
}
if(CryptImportKey(
hCryptProv,
pbKeyBlob,
dwBlobLen,
0,
0,
&hPubKey))
{
printf(
}
else
{
printf(
exit(1);
}
// Not this one
//if
(!CryptSetKeyParam(UserKey, KP_CERTIFICATE,
pCertContext->pbCertEncoded, 0))
if (!CryptSetKeyParam(hPubKey,
KP_CERTIFICATE, pCertContext->pbCertEncoded, 0))
{
error = GetLastError();
printf(
if (error != NTE_BAD_TYPE) {
// If error is
bad_type then we just can't set the property.
-
-
-
-
-
-
-
-
-
上一篇:全球主要城市三字代码
下一篇:【人教版】八年级上册英语知识点汇总