Advertisement

[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

04/26/2007 at 07:35AM PDT, ID: 22535926
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.4

Can't access private keys by using RSACryptoServiceProvider

Asked by aahmed19 in Encryption for Network Security, Visual Studio, .NET Framework 2.x

Tags: find, cannot, file

Hi,
I am trying to use RSACryptoServiceProvider in my ASP.NET application to access keys from a MachineKeyStore on my computer, running windows xp and IIS 5.
I created machinekeystore like following in Visual Studio 2005 Command Prompt:

aspnet_regiis -pc "CustomKeys" -exp (command was successful)

Then I executed following command because I am impersonating my web application with a non-default user:

aspnet_regiis -pa "CustomKeys" "domain\auserforapplication" (command was successful)

Then I worte the following code:

public partial class Examples_EncryptionExample : System.Web.UI.Page
{
    CspParameters CspParam;
    string publicXmlString = string.Empty;
    string privateXmlString = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            byte []  encrypted;
            string decrypted;

            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            encrypted = EncrptData("data to encrypt");
            Response.Write(System.Text.Encoding.Unicode.GetString(encrypted));
            decrypted = DecryptData(encrypted);
            Response.Write(decrypted);
        }
        catch (Exception ex)
        {
        }
    }

    public string DecryptData(byte [] data)
    {
        RSACryptoServiceProvider RsaCsp;
        byte[] decryptedData;
        RsaCsp = new RSACryptoServiceProvider();
        RsaCsp.FromXmlString(privateXmlString);
        decryptedData = RsaCsp.Decrypt(data, false);
        return System.Text.Encoding.Unicode.GetString(decryptedData);
    }

    public byte [] EncrptData(string data)
    {
        RSACryptoServiceProvider RsaCsp;
        RSACryptoServiceProvider RsaCsp2;
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        CspParam = new CspParameters();
        CspParam.KeyContainerName = "CustomKeys";
        CspParam.Flags = CspProviderFlags.UseMachineKeyStore;

        byte[] encryptedData = ByteConverter.GetBytes(data);

        RsaCsp = new RSACryptoServiceProvider(CspParam);

        //Getting public key
        publicXmlString = RsaCsp.ToXmlString(false);
        //Getting private key
        privateXmlString = RsaCsp.ToXmlString(true);

        RsaCsp2 = new RSACryptoServiceProvider();
        RsaCsp2.FromXmlString(publicXmlString);
        encryptedData = RsaCsp2.Encrypt(System.Text.Encoding.Unicode.GetBytes(data), false);

        return encryptedData;
    }
}

The problem over here is that when ever I try to execute the above mentioned code. Code encrypts the data fine
but when it comes at decrypting the data, throws following exception:

Exception Details: System.Security.Cryptography.CryptographicException: The system cannot find the file specified.


Source Error:


Line 35:         byte[] decryptedData;
Line 36:         RsaCsp = new RSACryptoServiceProvider();
Line 37:         RsaCsp.FromXmlString(privateXmlString);
Line 38:         decryptedData = RsaCsp.Decrypt(data, false);
Line 39:         return System.Text.Encoding.Unicode.GetString(decryptedData);
 

Source File: c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs    Line: 37

Stack Trace:


[CryptographicException: The system cannot find the file specified.
]
   System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) +33
   System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv) +0
   System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +201
   System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) +262
   System.Security.Cryptography.RSA.FromXmlString(String xmlString) +465
   Examples_EncryptionExample.DecryptData(Byte[] data) in c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs:37
   Examples_EncryptionExample.Page_Load(Object sender, EventArgs e) in c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs:28
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

I could control the above mentioned error by doing a nasty trick which is. The account "domain\auserforapplication" which I am impersonating my application with. I used a utility in windows xp accessible from "All Programs/Accessories/System Tool/Schedule Tasks" to create a process e.g. executed calc.exe application under the account "domain\auserforapplication". Everything started working fine. No error nothing.

A million dollar question is why did I get the above mentioned error at the first place? Why did I had to start a new process under the indentity of my application on my machine.

If somebody could answer my question. I will highly appreciate that because then I have another question regarding exporting the keys to Windows 2003 Server and using keys over there. That problem is even nasty.

For now I will highly appriciate if somebody could answer my current question.
Thanks

View the Solution FREE for 30 Days
[+][-]05/17/07 02:59 AM, ID: 19106946

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/17/07 07:50 AM, ID: 19108640

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05/17/07 09:12 PM, ID: 19113531

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/24/07 03:15 AM, ID: 19350478

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]06/29/07 08:49 AM, ID: 19390901

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Encryption for Network Security, Visual Studio, .NET Framework 2.x
Tags: find, cannot, file
Sign Up Now!
Solution Provided By: AnnieMod
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20090429-EE-VQP-58