Problems with RSACryptoServiceProvider
I have some problems with RSACryptoServiceProvider on my machine. If a create a new 开发者_如何学Cinstance:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
I already get an exception, under CspKeyContainerInfo property :
Exportable 'rsa.CspKeyContainerInfo.Exportable' threw an exception of type 'System.Security.Cryptography.CryptographicException' bool {System.Security.Cryptography.CryptographicException}
base {"Key does not exist.\r\n"} System.SystemException {System.Security.Cryptography.CryptographicException}
If I try the same code on the another PC, everything works fine. Are there some settings on my PC that I can check to see if both of them are configured properly and in what the configuration defers? Any clue on what may be the problem?
Thanks
EDIT: It seem's that it only happens on framework 4.0. Any clue?
I managed to make it work.
CspParameters parms = new CspParameters();
parms.Flags = CspProviderFlags.NoFlags;
parms.KeyContainerName = Guid.NewGuid().ToString().ToUpperInvariant();
parms.ProviderType = ((Environment.OSVersion.Version.Major > 5) || ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor >= 1))) ? 0x18 : 1;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(parms);
I do not know why, but it is just like that. If anyone know why of this behavior, and comment is more then welcome.
Thanks
You can get the provider type in the windows registry: \local_machine\software\Microsoft\Cryptography\Defaults\Provider\
Look for the provider you want and check the Type value...
精彩评论