Статус: Участник
Группы: Участники
Зарегистрирован: 02.08.2013(UTC) Сообщений: 11
|
Sorry, but I'm new in this, and it just doesn't work. I think that CryptAcquireCertificatePrivateKey(pCert,0,0, hProv, @dwKeySpec,0) doesn't acquire key, and when I come to cryptsigmmessage, I get Access violation.... in module crypt32... Код:
var
signature : PBYTE ;
count : pDWORD;
hSystemStore : HCERTSTORE;
pCert : PCCERT_CONTEXT;
dwKeySpec : DWORD ;
hProv : HCRYPTPROV ;
SignPara : CRYPT_SIGN_MESSAGE_PARA ;
cString:string;
DataArray : PBYTE ;
SizeArray : DWORD ;
Size: Cardinal;
p: Pointer;
const
CERT_STORE_NAME = WideString('ROOT');
begin
p:=CertOpenSystemStore(0, 'MY');
if not assigned(p) then
begin
exit;
end;
if( not(hSystemStore = CertOpenSystemStore(hProv, CERT_STORE_NAME))) then
if not assigned(hSystemStore) then
showmessage(syserrormessage (getlasterror));
pCert:=nil;
hSystemStore:=p;
pCert:=CertEnumCertificatesInStore(hSystemStore,pCert);
if(CryptAcquireCertificatePrivateKey(pCert,0,0, hProv, @dwKeySpec,0)) then
begin
CryptReleaseContext(hProv,0);
end;
ZeroMemory(@SignPara,sizeof(SignPara));
SignPara.cbSize:=sizeof(SignPara);
SignPara.dwMsgEncodingType:=X509_ASN_ENCODING or PKCS_7_ASN_ENCODING;
SignPara.HashAlgorithm.pszObjId:=CertAlgIdToOID(CALG_MD5);
SignPara.pSigningCert:=pCert;
SignPara.cMsgCert:=1;
SignPara.rgpMsgCert:=pCert;
cString:=pwideChar('Test');
DataArray:=PByte(cString);
SizeArray:=strlen(PAnsiChar(cString));
count:=0;
if( not CryptSignMessage(@SignPara,true,1,DataArray,@SizeArray,nil,count)) then
showmessage(syserrormessage (getlasterror));
GetMem(signature, Size);
FillChar(signature^, Size, 0);
if( not CryptSignMessage( @SignPara,true,1,DataArray,@SizeArray,@signature,count)) then
showmessage(syserrormessage (getlasterror));
CertFreeCertificateContext(pCert);
CertCloseStore(hSystemStore,0);
|