by Norm [norman_lm_fung at hotmail dot com] posted on 2004/09/19 |
|
AsymmetricCryptography - CryptographicException: Bad Key
I am having a bit of trouble with C# sample "AsymmetricCryptography". The sample works fine with the default certificate and key (client.cer and client.pvk). No trouble at all, the sample encrypt "Hello World" and decrypt it as expected.
I went on, generated my own certificate and keys with "MakeCert.exe":
C:\keys>MakeCert -sv privatekey.pvk -ss TestCertStore testcert.cer -n "CN=Testing,OU=Development, O=Testing, E=dev@testing.com"
I ended up getting "CryptographicException: Bad Key". Here's screendump:
This example shows how to use the public and private key from a certificate to e
ncrypt and decrypt data.
System.Security.Cryptography.CryptographicException: Bad Key.
at System.Security.Cryptography.RSACryptoServiceProvider._DecryptPKWin2KEnh(I
ntPtr hPubKey, Byte[] rgbKey, Boolean fOAEP)
at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb,
Boolean fOAEP)
at System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter.DecryptKeyExch
ange(Byte[] rgbIn)
at AsymmetricCryptography.Main(String[] args) in d:\vss local folder\visual s
tudio projects\seclib-1.0 download from mentalis.org on 19 sept 2004\samples\cry
ptography\asymmetriccryptography\c#\asymmetriccryptography.cs:line 58
The line where it failed:
RSA private_key = cert.PrivateKey;
Seems like it's having some trouble with the private key... btw, I'm running the sample on Windows 2003, with Administrator account.
Thanks in advance! |