Hi Alex,
And thanks for replying my email!, I was taken a look at the application that VFP70 has for encryption, and seems to be the start point, however the PKI methodology still is not very clear for me.
I was trying to create a public key derived from a password and export to a file to be used later on by another person, the code is below ( what's wrong?, why the key cannot be exported?
You can copy and paste and try, may be you can help me understand why or how the key can be saved in a file(exported)
Thanks in advance!-)
* There are two methods for generating session keys: using a random seed through CryptGenKey
* or using a hash method via CryptDeriveKey
lcPrivateKey = "JUAN"
lPasswordCount = LEN(lcPrivateKey)
nSuccess = CryptCreateHash(lHCryptprov, dnCALG_MD5, 0, 0, @lHHash)
nSuccess = CryptHashData(lHHash, @lcPrivateKey, lPasswordCount, 0)
&& THE CryptDeriveKey FUNCTION DOES NOT GENERATE A PUBLIC/PRIVATE KEY PAIR
* // Derive a session key from the hash object.
*nSuccess = CryptDeriveKey(lHCryptprov, dnCALG_RC4, lHHash, dnCRYPT_EXPORTABLE, @lHkey)
nSuccess = CryptDeriveKey(lHCryptprov, dnCALG_RC4, lHHash, 0, @lHkey)
=CryptDestroyHash(lHHash)
* AT THIS POINT THE HANDLER lHkey IS USED TO ENCRYPT DATA
** UP TILL HERE IS OK, BUT
** NOW EXPORT THE CREATED PUBLIC KEY FROM THE CSP
SET STEP ON
nSuccess = CryptGetUserKey(lHCryptprov, dnAT_KEYEXCHANGE, @lHExchgKey)
IF nSuccess = 0
?"CryptGetUserKey FAILED..."
RETURN .F.
ENDIF
lHkeyBlobLen = MqBinToStr(0,4)
lcEmptyString = ''
* Get the Len of the String First
? CryptExportKey(lHkey, lHExchgKey, dnPUBLICKEYBLOB, 0, lcEmptyString, @lHkeyBlobLen)
* Create the String and Stuff it with the BLOB
aKeyBlob = SPACE(MqStrToBin(lHkeyBlobLen))
* dnPRIVATEKEYBLOB - USED TO TRANSPORT PUBLIC/PRIVATE KEYS
*dnPUBLICKEYBLOB - USED TO TRANSPORT PUBLIC KEYS
? CryptExportKey(lHkey, lHExchgKey , dnPUBLICKEYBLOB, 0, @aKeyBlob, @lHkeyBlobLen)
CryptDestroyKey(lHExchgKey)
cPublicKey = aKeyBlob + CHR(0) && ENCRYPTED SESSION KEY
>
> I couldn't find any resources for the creation and use of a public key using CRYPTOAPI.
>> Not even books!!!!
>> If somebody has worked with CRYPTOAPI>
>VFP 7.0 has a class library and a set of examples in the FFC (_crypt.vcx)
>I wrote an article with examples on the use of this in CoDe Magazine two issues ago.
>
http://www.code-magazine.com>
>There are a few messages about it here in the UT.
>
>MSDN has the wole documentation for the CAPI (albeit with examples mostly in C). You can get that in an MSDN subscription or on MSDN online.
>
>There is also an article from FoxTalk in MSDN:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfoxtk00/html/ft00f10.asp>
>HTH