473,385 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Unhadelded Exception Occurs When using RSACryptoServiceProvider

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.CryptographicExc eption

message=Keyset does not exist
stack=
at
System.Security.Cryptography.CryptographicExceptio n.ThrowCryptogaphicException(Int32
hr)
at System.Security.Cryptography.SafeProvHandle._FreeC SP(IntPtr
pProvCtx)
at System.Security.Cryptography.SafeProvHandle.Releas eHandle()
at System.Runtime.InteropServices.SafeHandle.Internal Finalize()
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 '06 #1
0 1533

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is ...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
0
by: Val | last post by:
From: "Val Mazur" <group51a@hotmail.com> Subject: Exception has been thrown by the target of an invocation Date: Monday, June 06, 2005 2:01 PM Hi guys, For some reason when application tries...
7
by: Søren Dreijer | last post by:
Hi, I have a mixed C#, managed C++ and unmanaged C++ project. The managed class calls a method which exists in an unmanaged singleton class. During the entire lifetime of the application, this...
0
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
2
by: =?Utf-8?B?R2FicmllbCBNw6luZGV6?= | last post by:
Hello everyone. I have a small class that encapsulates some functionallity to work with the RSACryptoServiceProvider. Here is the code of the class i'm using: public class dsRSA { private...
0
by: dfa_geko | last post by:
Hi All, Just had a question about the RSACryptoServiceProvider class. I'm kind of a newbie at this. In the following code at the end of the message, does the key get stored in the User...
0
by: Olli Goessler | last post by:
Hi Guys, (sorry for my bad english) i have a question for the following problem: With the RSACryptoServiceProvider object... Application A: // Generate a public/private key pair....
3
by: John Wright | last post by:
I have a x509Certificate that I exported and I am using for testing called wsTest.cer.pfx. I want to use this cert to send a public key to anyone who requests it, and then use the private key to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.