Hey Peoples!
I imported a public key for the RSACryptoServiceProvider using the FromXMLString() method, which imports without drama, but throws an unhandled CryptographicException when I use the Encrypt() method, with the Error Message of "Key not valid for use in specified state."
Does anyone understand this problem or how to get around it? I've looked at other forum posts but they all concern ASP, my application is a Windows Application built with C#, but out of desparation I have already tried the ASP fixes suggested but I still get the exception.
Ok, and now some new observations of this annoying behaviour...
I have just tried encrypting something using the same RSA Key Values XML string in the C# application that originally output the XML string and it works fine, but when I use the RSA Key Values XML in the application that did not generate the RSA Key Values XML it continues to throw the exception when I call the Encrypt method.
So the question is, why bother allowing users to export RSA Key Values to XML when it doesn't work with anything but the application that originally created the XML??? ...and why is this not documented anywhere? I have spent hours on this problem. Why would there be security measures in place that limit the usefulness of the output XML to a single application?
Microsoft people, please help me understand why I get this problem!!! There's a stack trace for the exception at the bottom. Is my current understanding correct, being "the values in the XML file are somehow tainted by the application that generated them, so that they can only be used with that particular application and no other" ?
Keenly awaiting some clarity,
Voss
System.Security.Cryptography.CryptographicException was unhandled
Message="Key not valid for use in specified state.\r\n"
Source="mscorlib"
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
at System.Security.Cryptography.Utils._EncryptKey(SafeKeyHandle hPubKey, Byte[] key)
at System.Security.Cryptography.RSACryptoServiceProvider.Encrypt(Byte[] rgb, Boolean fOAEP)
at Project.Library.LicenseKey.EncryptAll_SPub(Byte[] data) in C:\Documents and Settings\Voss\My Documents\Visual Studio 2005\Projects\Project.Library\Project.Library\LicenseKey.cs:line 356
at Project.Library.LicenseKey.Save(String fileName) in C:\Documents and Settings\Voss\My Documents\Visual Studio 2005\Projects\Project.Library\Project.Library\LicenseKey.cs:line 200
at PWin.ActivateProduct.GenerateUnsignedKey() in C:\Documents and Settings\Voss\My Documents\Visual Studio 2005\Projects\Project.Library\PWin\ActivateProduct.cs:line 143
at PWin.ActivateProduct.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Voss\My Documents\Visual Studio 2005\Projects\Project.Library\PWin\ActivateProduct.cs:line 70
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at PWin.Program.Main() in C:\Documents and Settings\Voss\My Documents\Visual Studio 2005\Projects\Project.Library\PWin\Program.cs:line 47
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()