Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, November 18, 2007 3:09 PM
I've got a problem with using any of the RSACryptoServiceProvider methods, the problem is that the method didn't wotk properly when i uploaded my applicaion on a hosting account which is hosted on a windows server. but the methods worked fine on the local host !!
when any method is called i get the following error:
Server Error in '/' Application.
The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
Source Error:
Line 70:
Line 71: // importing public key
Line 72: RSA.FromXmlString(PublicKey);
Line 73:
Line 74:
Source File: d:\hosting\ksucciscom\App_Code\Encryption.cs Line: 72
Stack Trace:
[CryptographicException: The system cannot find the file specified.
]
System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +1425404
System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) +262
System.Security.Cryptography.RSA.FromXmlString(String xmlString) +465
Encryption.RSAencryption(String SecretKey, String PublicKey) in d:\hosting\ksucciscom\App_Code\Encryption.cs:72
Message.encryptMessage(String& message, String receiver_PublicKey) in d:\hosting\ksucciscom\App_Code\Message.cs:28
NewMessage.Send_Click(Object sender, EventArgs e) in d:\hosting\ksucciscom\NewMessage.aspx.cs:66
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
this is the same error which occured on another place of the code where i used one of the methods:
Server Error in '/' Application.
The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
Source Error:
Line 167: RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024);
Line 168:
Line 169: String privateKey = RSA.ToXmlString(false); // public key only
Line 170: String publicKey = RSA.ToXmlString(true); //public & private key
Line 171:
Source File: d:\hosting\ksucciscom\Register.aspx.cs Line: 169
Stack Trace:
[CryptographicException: The system cannot find the file specified.
]
System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +1425404
System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +55
System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +79
System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(Boolean includePrivateParameters) +38
System.Security.Cryptography.RSA.ToXmlString(Boolean includePrivateParameters) +41
Register.Submit_Click(Object sender, ImageClickEventArgs e) in d:\hosting\ksucciscom\Register.aspx.cs:169
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +105
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
***********************************************************************************************
This is the code where the exception occured:
// a simple method to generate prive & public keys
public void generateKeys(String PublicKey, String PrivateKey)
{
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data of 1024 key size.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024);
// Converting to strings
PublicKey = RSA.ToXmlString(false); // public key only
PrivateKey = RSA.ToXmlString(true); //public & private key
}
***********************************************************************************************
Note: the same problem occures also when i use the other
RSA.ExportCspBlob(true) method which is an alternative for ToXmlString but it returns byte[] instead of string.
Please guide me the solution or the reason as soon as possible because my graduation project depends on it.
thanx all
All replies (5)
Sunday, November 18, 2007 4:13 PM ✅Answered
This is probably because the ASP.NET worker process identity does not have read permission to the machine key store. This is a bit complicated, and I can't say exactly why but it appears you can create keys in the machine key store, but not read them.
My suggestion is that you check the Win32 API documentation from the Crypto API - because I think you'll find more information there. You should also get reflector (google for .NET reflector) so you can check the actual .NET code that fails, and follow the trail from there.
The 'File not found' is a typical misleading message from the Crypto API with it has trouble finding or reading key containers.
Most probably, you're running as administrator in your development machine, but ASP.NET is not in your deployment server. You might see the same result if you run you app on your development machine via IIS rather than the built-in VS web server (but then again, you might not - if you're running XP on the development machine and 2003 on the server you'll have other differences in the IIS 5 vs. IIS 6 to contend with).
Tuesday, November 27, 2007 1:43 PM
I noticed that when initialaizing an RSAcryptoProvider object it is creating files that contains the public/private keys on %system root%\machine-name\.. . I guess that since my user doesn’t have permissions on hosting provider machine, it generates the error. I think that I need to find a solution to create these files within my folder in the hosting server. Do you know how can I do that ? any help
Wednesday, November 28, 2007 2:37 AM
I think that I need to find a solution to create these files within my folder in the hosting server. Do you know how can I do that ?
No, not without investigating quite a bit, but you might want to try to specify a key-container using the constructor overload that takes a CspParameter, and setting KeyContainerName there. That should create a key container associated with the current user that the worker process is running under. Howerver, I'm not quite sure if this works since the worker process identity typically does not load a windows user profile. Might be worth a try though.
Wednesday, July 22, 2009 8:29 PM
Hi, I have the same issue now with my hosting ...
Did you solve this problem and how?
Thank's
Wednesday, December 1, 2010 8:52 AM
We are seeing the same exception occur on one of two web servers setup as a small web farm with ISA Server in front.
We didn't see this issue until recently when the farm was moved to a new (remote) domain. Before that, things were running fine for 2+ years.
I can reproduce the issue in my dev environment, by deleting the Application Pool user profile for the web site, and
can correct it by re-creating the user profile. (Decryption requires access within the User Profile to do it's work?)
We cannot determine why one machine works fine and the other doesn't. The bad machine has been rebuilt. This is in a test region. We also have the same setup going in production, which is working perfectly.
This is an intermittent issue. After we rebuild the bad machine, the decryption works for a while, then fails. It's almost like the user profile is disappearing. We've done some compares between the two machines but cannot see any significant difference. They are manually setup.
We've exhausted our diagnostics. My opinion is that it is a configuration or environmental issue. Our code base hasn't changed in 2+ years.
Any suggestions?