Unhadelded Exception Occurs When using RSACryptoServiceProvider

Discussion in 'ASP .Net' started by khubieb@gmail.com, Jul 4, 2006.

  1. Guest

    Simply I am trying to use RSACryptoServiceProvider to generate a key
    pair, send the public key to a service that will retrieve me data,
    encrypt it with my public key, send the encrypted data back for me to
    decrypt the data and use it. below is a code sample that simulates my
    task. It works just fine, however, when I turn impersonation to true in
    my web.config file and after a random number of attempts to invoke my
    page, an unhandled exception is fired somewhere in the Crypto Service
    Provider causing the aspnet_wp process to restart. I've caught the
    exception by registring an HTTP Module to listen to the
    AppDomain.CurrentDomain.UnhandledException event and this is the
    exception I get

    type=System.Security.Cryptography.CryptographicException

    message=Keyset does not exist


    stack=
    at
    System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32
    hr)
    at System.Security.Cryptography.SafeProvHandle._FreeCSP(IntPtr
    pProvCtx)
    at System.Security.Cryptography.SafeProvHandle.ReleaseHandle()
    at System.Runtime.InteropServices.SafeHandle.InternalFinalize()
    at System.Runtime.InteropServices.SafeHandle.Dispose(Boolean
    disposing)
    at System.Runtime.InteropServices.SafeHandle.Finalize()

    ..

    If I turn impersonation to false in web.config the exception doesn't
    fire. I don't know where does this exception occure.
    Also I've noticed that I missed to release the resources used by
    RSACryptoServiceProvider by invoking the
    RSACryptoServiceProvider.Clear() method. when calling this method the
    exception seems to cease to occure.

    I am just curious to know what is happening? what is the relation
    between impersonation and RSACryptoServiceProvider? why isn't it caught
    by the catch block? What is the thread that fires the exception? ...

    here is the code of my web form

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    using System.Security.Cryptography;
    using System.Text;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnRefresh_ServerClick(object sender, EventArgs e)
    {
    try
    {
    CspParameters cspParam = new CspParameters();
    cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
    RSACryptoServiceProvider pair = new
    RSACryptoServiceProvider(cspParam);
    string keyInfo = pair.ToXmlString(false);


    string encryptedData = GetSecureData(keyInfo);
    byte[] encrptedBytes =
    Convert.FromBase64String(encryptedData);
    byte[] decryptedBytes = pair.Decrypt(encrptedBytes, true);

    string decrypedData =
    Encoding.ASCII.GetString(decryptedBytes);


    //pair.Clear();
    txtEnctptedData.Value = encryptedData;
    txtDecryptedData.Value = decrypedData;
    }
    catch (Exception ex)
    {
    txtDecryptedData.Value = ex.Message;
    }
    }

    private string GetSecureData(string publicKey)
    {
    CspParameters cspParam = new CspParameters();
    cspParam.Flags = CspProviderFlags.UseMachineKeyStore;
    RSACryptoServiceProvider pair = new
    RSACryptoServiceProvider(cspParam);
    pair.FromXmlString(publicKey);

    byte[] dataBytes = Encoding.ASCII.GetBytes("Hello World!!");
    dataBytes = pair.Encrypt(dataBytes, true);

    return Convert.ToBase64String(dataBytes);
    }
    }
     
    , Jul 4, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dfa_geko

    RSACryptoServiceProvider class

    dfa_geko, Apr 11, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    486
    dfa_geko
    Apr 11, 2007
  2. Steven Licciardi

    RSACryptoServiceProvider

    Steven Licciardi, Sep 17, 2004, in forum: ASP .Net Security
    Replies:
    0
    Views:
    121
    Steven Licciardi
    Sep 17, 2004
  3. Glenn
    Replies:
    3
    Views:
    216
    Glenn
    Nov 26, 2004
  4. C

    .NET RSACryptoServiceProvider Class

    C, May 15, 2006, in forum: ASP .Net Security
    Replies:
    1
    Views:
    133
  5. anoop

    RSACryptoServiceProvider in ASP.Net 2.0

    anoop, Feb 28, 2007, in forum: ASP .Net Security
    Replies:
    4
    Views:
    208
    Jamieson
    Mar 30, 2007
Loading...

Share This Page