|
I Can not Encode any file with software on Console or wtl application.
|
|
|
|
|
OK, so this question might be slightly off topic, but I am desperate.
I'm new to crypto in general, and CryptoAPI in particular, so I think I am hitting some sort of newbie problem.
My VC6/COM/ATL app needs to be able to verify a digital signature using an imported public key blob via CryptImportKey. It is currently able to import the key and verify the signature, but I have also found that I am able to actually sign data using the public key. Agh! I know I am doing something wrong, but I am not sure what.
Just for fun, I threw together an equivalent app using the .NET framework (which is very nice!), and this app behaves as expected. The .NET app will not allow an attempt to sign data using a public key, throwing a system exception that says 'Object contains only the public half of a key pair. A private key must also be provided.' Very nice, but how do I use CryptoAPI to do the same thing?
Here are the CryptoAPI calling sequences my app is making.
1) Create keys
CryptAcquireContext( &hProv, szContainer=NULL, szProvider=MS_STRONG_PROV, dwProviderType=PROV_RSA_FULL, dwFlags=CRYPT_SILENT );
CryptGenKey( hProv, algid=CALG_RSA_SIGN, dwFlags=CRYPT_EXPORTABLE, &hKey );
CryptExportKey( hKey, hExpKey=NULL, dwBlobType=PRIVATEKEYBLOB, dwFlags=0, pbData=pPrivateBlob, pcbDataLen=&nPrivateBlobLen );
CryptExportKey( hKey, hExpKey=NULL, dwBlobType=PUBLICKEYBLOB, dwFlags=0, pbData=pPrivateBlob, pcbDataLen=&nPublicBlobLen );
2) Sign data from imported private key
CryptAcquireContext( &hProv, szContainer=NULL, szProvider=MS_STRONG_PROV, dwProviderType=PROV_RSA_FULL, dwFlags=CRYPT_SILENT );
CryptImportKey( hProv, pbData=&privateBlob, dwDataLen=nPrivateBlobLen, hPubKey=NULL, dwFlags=0, &hKey );
CryptCreateHash( hProv, algid=CALG_MD5, hKey=NULL, dwFlags=0, &hHash );
CryptHashData( hHash, pbData=pMyData, cbData=nMyDataLen, dwFlags=0 );
CryptSignHash( hHash, dwKeySpec=AT_SIGNATURE, sDescription=NULL, dwFlags=0, pbSignature=pMySignature, pdwSigLen=&nMySignatureLen );
3) Verify data from imported public key
CryptAcquireContext( &hProv, szContainer=NULL, szProvider=MS_STRONG_PROV, dwProviderType=PROV_RSA_FULL, dwFlags=CRYPT_SILENT );
CryptImportKey( hProv, pbData=&publicBlob, dwDataLen=nPublicBlobLen, hPubKey=NULL, dwFlags=0, &hKey );
CryptCreateHash( hProv, algid=CALG_MD5, hKey=NULL, dwFlags=0, &hHash );
CryptHashData( hHash, pbData=pMyData, cbData=nMyDataLen, dwFlags=0 );
CryptVerifySignature( hHash, pbSignature=pMySignature, dwSigLen=nMySignatureLen, hPubKey=hKey, sDescription=NULL, dwFlags=0 );
In all sequences, all error checking is being performed, and nothing is erroring out.
Any ideas what is wrong? If I had to guess, I'd say that something is wrong with sequence #2, since the hKey generated by importing the private key does not seem to be used anywhere else in the signing process. Perhaps I need to call CryptCreateHash with the hKey from the preceeding CryptImportKey call?
|
|
|
|
|
Install WTL
http://www.microsoft.com/downloads/details.aspx?FamilyID=1be1eb52-aa96-4685-99a5-4256737781c5&displaylang=en
|
|
|
|