News  [SoftwareSite

Latest News
Older News
RSS Feed
 
Complete Projects
Useful Classes
Top Downloads
Message Board
AllAPI.net
 
Send Comments
Software License
Mentalis.org Buttons
Donate
 
Forums -> Security Library Forum
 
sample: AsymmetricCryptography - CryptographicException: Bad Key  
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!

by Lars
posted on 2004/10/21

Try using the option

-sky Exchange

when creating your own key...

or look here
http://groups.google.com/groups?hl=en&lr=&threadm=a000d88c.0311170215.2cb38af9%40posting.google.com&rnum=1&prev=/groups%3Fq%3DMakeCert.exe%2B%2Bbad%2Bkey%26hl%3Den%26lr%3D%26selm%3Da000d88c.0311170215.2cb38af9%2540posting.google.com%26rnum%3D1

 

Copyright © 2002-2007, The Mentalis.org Team. All rights reserved.
This site is located at http://www.mentalis.org/
Send comments to the webmaster.