Skip to main content
gamedev.net
🔒 Locked

Encryption problem (using CryptAPI)

Started by UriKiller 6 replies 1.7k views
Original Post
UriKiller
UriKiller
Hello, I'm new to the whole encrypting system.. I started to read in MSDN about how to use CryptAPI and after about a week I finished a program that generates a public/private key pair, a program that encrypts a file using the public key, and another that decrypts using the private key. Everything worked fine until: I accidently tried to decrypt the file with the public key and it succeeded. This shocked me.. I am using the Microsoft basic cryptographic provider. I use the algorithm CALG_RSA_KEYX with length 1024, and provider type PROV_RSA_FULL. Please help me to figure this out, I cannot use these programs now that I found this horrible mistake. Thank you if you solve it! Uri.
Washu
Washu
you have given us no details as to how you implemented your solution, nor anything in between.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Scuppy
Scuppy
MS CryptoAPI uses FIPS approved alogirthms. If you think the alogrithm is broken; first re-read the doco (because it sounds like you just don't understand how it works) and then give details.
UriKiller
UriKiller
Ok sorry for not giving the details, I just assumed that this is a common mistake so you would immediately guess why this happens.

1) In the Key Generator I call:
CryptAcquireContext (&g_hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)CryptGenKey (g_hCryptProv, ENCRYPT_ALGORITHM, (0x0400 << 16) | CRYPT_EXPORTABLE, &hKey)CryptExportKey (hKey, NULL, PUBLICKEYBLOB, 0, pudata, &pukeylen)CryptExportKey (hKey, NULL, PRIVATEKEYBLOB, 0, prdata, &prkeylen)

And then save the files.

2) In the encrypter I call:
CryptAcquireContext (&g_hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)//Public key is file loaded into keydata, keylen.CryptImportKey (g_hCryptProv, keydata, keylen, 0, 0, &hKey)//It's for encrypting just one block. so nbytes = 117, outblokclen = 128.CryptEncrypt (hKey, NULL, true, 0, data, &nbytes, outblocklen);

3) In the decrypter I call:
CryptAcquireContext (&g_hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)//Private key is loaded into keydata, keylen. If public key is loaded instead,//everything will still work. That's the problem...CryptImportKey (g_hCryptProv, keydata, keylen, 0, 0, &hKey)//It's for decrypting just one block. So dec so nbytes = 128, and becomes 117 after the function returns.CryptDecrypt (hKey, NULL, true, 0, data, &nbytes)


*There are no errors returned in any of the calls to the Crypt functions in either of the programs. But when I import the public key blob, the decryption works perfectly.
Thanks again for any help.
Naurava kulkuri
Naurava kulkuri
Hmm... I've been under the impression that the key-container is persistent in a way. That is, if it isn't explicitly deleted, then the container in which the key is placed is still there and if you try to decrypt using a public key, the system automatically finds the stored private key.

Try deleting the container between the calls. It's been a while I've used CryptoAPI (in a .NET CF 1.0 environment) so I'm not 100 % sure about this.
---Sudet ulvovat - karavaani kulkee
UriKiller
UriKiller
Wow, Nice guess! thank you!! I'm almost sure that it is what you said,
because on other computers I cannot decrypt using the public key. I still haven't tried to explicitly delete the keys, what function does that?
Naurava kulkuri
Naurava kulkuri
Quote:
Original post by UriKiller
Wow, Nice guess! thank you!! I'm almost sure that it is what you said,
because on other computers I cannot decrypt using the public key. I still haven't tried to explicitly delete the keys, what function does that?
Hmm... The CryptoAPI itself is a bit cryptic in places. There is rather good, but hard to find documentation on these issues in MSDN. Please see The Cryptography API, or How to Keep a Secret and look for the string In order to delete an existing key container from the default CSP from the page for more instructions.

A quote from the documentation:
The CryptAcquireContext function performs two operations. It first attempts to find a CSP with the characteristics described by various parameters. If the CSP is found, the function attempts to find a key container within the CSP that matches the specified container name. This function can also be used to create and destroy key containers, depending on the value of the parameters.
My best guess is that this is the way to destroy the container if that is what you want to do.

[EDIT]... And now the link is visible also. [smile][/EDIT].
---Sudet ulvovat - karavaani kulkee
UriKiller
UriKiller
Thanks, everything works now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.