Different (wrong?) RSA keys from X509 certyficate stored in CSP in -
06-18-2004
, 07:00 AM
I need to encrypt/decrypt data using RSA algorithm. The private/public keys which I am using are from X509 certificate. This certificate is stored in CSP. MMC shows, that certificate exists in machine key store (in "MY" store).
I am encrypting data in Windows console application. The following code I have used to construct valid RSACryptoServiceProvider object with private/public keys stored in CSP (all parameters like KeyContainerName, KeyNumber, etc. I obtained using Win32 API functions: CertOpenStore, CertFindCertificateInStore, CertGetCertificateContextProperty - see examples in article "EncryptTo/DecryptTo: Encryption in .NET with CryptoAPI Certificate Stores" by Michel I. Gallant, Ph.D). The same code I have used for decpypting data.
CspParameters cp = new CspParameters();
cp.KeyContainerName = "29a69d75e3b626afcc0f46dccfab5172_d0b4628f-e8d6-4c55-88b1-2345024a21c7";
cp.KeyNumber = 1;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
cp.ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";
cp.ProviderType = 1;
RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(cp);
string s_xml = oRSA.ToXmlString(true);
(...)
plainBytes = oRSA.Decrypt(cipherBytes, false);
But this code run as console Windows application and as ASP.NET application (on the same computer) results two DIFFERENT RSA parameters (public/private keys)!
In console application s_xml variable allways has value:
<RSAKeyValue><Modulus>rRn6WTKcmkPYrWKx52nkSJpZSBnX +bUwu9GvAAnJa4mgh9dyF(...)
In ASP.NET application s_xml variable allways has DIFFERENT value:
<RSAKeyValue><Modulus>ttQe8Q7TsOiz7aZ0r5MLk5HhpLJC 39JgF9zMMagfX//axEjS4y(...)
When I decrypt data in console application, it works ok, but when I decrypt data in ASP.NET I have got exception in Decrypt method which shows that private key is invalid (it is true because the public/private keys are DIFFERENT in ASP.NET).
My question is: why these keys are DIFFERENT in console and ASP.NET (I use MachineKeyStore)? What shall I do to solve this problem?
I would appreciate any help.
Best regards,
Marek Jazwinski |