Click here to Skip to main content
15,892,005 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Sir,can you help me Pin
momer20-Sep-05 15:24
momer20-Sep-05 15:24 
QuestionAccessing Error-Checking of local hard drives in XP (in c++ code) Pin
jai200219-Sep-05 19:59
jai200219-Sep-05 19:59 
AnswerRe: Accessing Error-Checking of local hard drives in XP (in c++ code) Pin
Branislav20-Sep-05 1:00
Branislav20-Sep-05 1:00 
GeneralRe: Accessing Error-Checking of local hard drives in XP (in c++ code) Pin
jai200220-Sep-05 13:53
jai200220-Sep-05 13:53 
Questionopening default mail client Pin
rohit.dhamija19-Sep-05 19:02
rohit.dhamija19-Sep-05 19:02 
Questionhttp mime part Pin
ppp00119-Sep-05 17:36
ppp00119-Sep-05 17:36 
AnswerRe: http mime part Pin
Jose Lamas Rios19-Sep-05 18:11
Jose Lamas Rios19-Sep-05 18:11 
QuestionPublic/private keys w/CryptoAPI Pin
Michael Dunn19-Sep-05 16:17
sitebuilderMichael Dunn19-Sep-05 16:17 
So this is the first time I'm doing anything more complex with CryptoAPI than a MD5 hash, and in typical MS fashion the API is over-complex and under-documented. Unsure | :~ Anyhoo, I'm trying to generate a public/private key pair so I can ship the public key in an app and write an encrypted log file (the thinking being that only my log viewer will have the private key so no one else could read sensitive data in the log). Problem is, I'm able to encrypt and decrypt using just the public key. WTF?

Here's how I created the keys. Error handling has been omitted here but all calls are succeeding in my test app.
HCRYPTPROV hcp;
HCRYPTKEY hcc;
 
    if ( !CryptAcquireContext ( &hcp, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0 ) &&
         GetLastError() == NTE_BAD_KEYSET )
        {
        CryptAcquireContext ( &hcp, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET ) )
        }
 
    <font color=red>// This should generate a public/private key pair, right?</font>
    CryptGenKey ( hcp, CALG_RSA_KEYX, (4096<<16)|CRYPT_EXPORTABLE, &hcc );
 
DWORD cbyBlob = 0;
BYTE* pbyPublicBlob = NULL, *pbyPrivateBlob = NULL;
 
    CryptExportKey ( hcc, NULL, PUBLICKEYBLOB, 0, NULL, &cbyBlob );
 
    pbyPublicBlob = new BYTE[cbyBlob];
 
    CryptExportKey ( hcc, NULL, PUBLICKEYBLOB, 0, pbyPublicBlob, &cbyBlob );
    // OMITTED: Write pbyPublicBlob to a C struct.
 
    CryptExportKey ( hcc, NULL, PRIVATEKEYBLOB, 0, NULL, &cbyBlob );
 
    pbyPrivateBlob = new BYTE[cbyBlob];
 
    CryptExportKey ( hcc, NULL, PRIVATEKEYBLOB, 0, pbyPrivateBlob, &cbyBlob );
    // OMITTED: Write pbyPrivateBlob to a C struct.
That's all fine and dandy and I end up with two byte arrays:
BYTE abyPublicKey[] = { ... };
BYTE abyPrivateKey[] = { ... };
So now when I test importing just the public key, I can do this:
HCRYPTKEY hpubkey;
 
    // OMITTED: Same CryptAcquireContext() call as above
    CryptImportKey ( hcp, abyPublicKey, sizeof(abyPublicKey), NULL, 0, &hpubkey );
 
    // Encrypt some text
BYTE thetext[] = "The Code Project", *pbyBuffer = NULL;
DWORD cbyPlainText = sizeof(thetext), cbyCipherText = 0;
 
    CryptEncrypt ( hpubkey, NULL, TRUE, 0, NULL, &cbyCipherText, cbyPlainText );
 
    pbyBuffer = new BYTE[cbyCipherText];
    memcpy ( pbyBuffer, thetext, cbyPlainText );
 
    CryptEncrypt ( hpubkey, NULL, TRUE, 0, pbyBuffer, &cbyPlainText, cbyCipherText );
 
    // Decrypt it back
BYTE* pbyDecrypted = new BYTE[cbyCipherText];
 
    memcpy ( pbyDecrypted, pbyBuffer, cbyCipherText );
 
    <font color=red>// THIS WORKS:</font>
    CryptDecrypt ( hpubkey, NULL, TRUE, 0, pbyDecrypted, &cbyCipherText );
The same public key results in successful calls to CryptEncrypt() and CryptDecrypt(). So either I'm doing something dumb (which is possible, I'm not used to this API yet) or I didn't really get a pub/private key pair from CryptGenKey(). Any ideas?

--Mike--
Visual C++ MVP Cool | :cool:
LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ
Pinky, are you pondering what I'm pondering?
I think so Brain, but how will we fit the hamster inside the accordion?


-- modified at 22:22 Monday 19th September, 2005
AnswerRe: Public/private keys w/CryptoAPI Pin
oustar19-Sep-05 21:00
oustar19-Sep-05 21:00 
GeneralRe: Public/private keys w/CryptoAPI Pin
Michael Dunn20-Sep-05 5:53
sitebuilderMichael Dunn20-Sep-05 5:53 
AnswerRe: Public/private keys w/CryptoAPI Pin
Michael Dunn20-Sep-05 7:19
sitebuilderMichael Dunn20-Sep-05 7:19 
Questionint to CString Pin
benjnp19-Sep-05 15:44
benjnp19-Sep-05 15:44 
AnswerRe: int to CString Pin
Michael Dunn19-Sep-05 15:56
sitebuilderMichael Dunn19-Sep-05 15:56 
AnswerRe: int to CString Pin
kakan19-Sep-05 18:58
professionalkakan19-Sep-05 18:58 
QuestionDeviceIoControl() returns zero Pin
momer19-Sep-05 15:44
momer19-Sep-05 15:44 
QuestionMultithread Pin
benjnp19-Sep-05 14:02
benjnp19-Sep-05 14:02 
AnswerRe: Multithread Pin
User 58385219-Sep-05 14:23
User 58385219-Sep-05 14:23 
GeneralRe: Multithread Pin
benjnp19-Sep-05 14:34
benjnp19-Sep-05 14:34 
GeneralRe: Multithread Pin
User 58385219-Sep-05 14:38
User 58385219-Sep-05 14:38 
AnswerRe: Multithread Pin
benjnp19-Sep-05 14:41
benjnp19-Sep-05 14:41 
GeneralRe: Multithread Pin
Cedric Moonen19-Sep-05 20:12
Cedric Moonen19-Sep-05 20:12 
AnswerRe: Multithread Pin
RichardS19-Sep-05 14:53
RichardS19-Sep-05 14:53 
GeneralRe: Multithread Pin
benjnp19-Sep-05 15:33
benjnp19-Sep-05 15:33 
GeneralRe: Multithread Pin
RichardS20-Sep-05 4:21
RichardS20-Sep-05 4:21 
AnswerRe: Multithread Pin
Tim Smith19-Sep-05 18:14
Tim Smith19-Sep-05 18:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.