473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unhadelded Exception Occurs When using RSACryptoServic eProvider

Simply I am trying to use RSACryptoServic eProvider 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.Curre ntDomain.Unhand ledException event and this is the
exception I get

type=System.Sec urity.Cryptogra phy.Cryptograph icException

message=Keyset does not exist
stack=
at
System.Security .Cryptography.C ryptographicExc eption.ThrowCry ptogaphicExcept ion(Int32
hr)
at System.Security .Cryptography.S afeProvHandle._ FreeCSP(IntPtr
pProvCtx)
at System.Security .Cryptography.S afeProvHandle.R eleaseHandle()
at System.Runtime. InteropServices .SafeHandle.Int ernalFinalize()
at System.Runtime. InteropServices .SafeHandle.Dis pose(Boolean
disposing)
at System.Runtime. InteropServices .SafeHandle.Fin alize()

..

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
RSACryptoServic eProvider by invoking the
RSACryptoServic eProvider.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 RSACryptoServic eProvider? 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.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

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

public partial class _Default : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
}
protected void btnRefresh_Serv erClick(object sender, EventArgs e)
{
try
{
CspParameters cspParam = new CspParameters() ;
cspParam.Flags = CspProviderFlag s.UseMachineKey Store;
RSACryptoServic eProvider pair = new
RSACryptoServic eProvider(cspPa ram);
string keyInfo = pair.ToXmlStrin g(false);
string encryptedData = GetSecureData(k eyInfo);
byte[] encrptedBytes =
Convert.FromBas e64String(encry ptedData);
byte[] decryptedBytes = pair.Decrypt(en crptedBytes, true);

string decrypedData =
Encoding.ASCII. GetString(decry ptedBytes);
//pair.Clear();
txtEnctptedData .Value = encryptedData;
txtDecryptedDat a.Value = decrypedData;
}
catch (Exception ex)
{
txtDecryptedDat a.Value = ex.Message;
}
}

private string GetSecureData(s tring publicKey)
{
CspParameters cspParam = new CspParameters() ;
cspParam.Flags = CspProviderFlag s.UseMachineKey Store;
RSACryptoServic eProvider pair = new
RSACryptoServic eProvider(cspPa ram);
pair.FromXmlStr ing(publicKey);

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

return Convert.ToBase6 4String(dataByt es);
}
}

Jul 4 '06 #1
0 1558

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

Similar topics

5
2705
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 return ::memcmp(_First1, _First2, _Count); On inspecting these variables, the strings are in fact equal when the exception occurs and _Count is the right size. As a test I replaced this code in the system include file with a for loop to do the...
5
3437
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
0
1523
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 to load signed XML into the SignedXml using LoadXml method, then it generates next exception despite the fact that signed XML is valid. But it happens only in ASP.NET environement. Same code runs fine on a same PC if it is a Windows application....
7
9538
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 gives no problems whatsoever. However, upon shutdown an exception pops up: "The string binding is invalid" If I call the singleton method from inside a purely unmanaged class, I
0
5686
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 encryption command (line : encryptedData2 = RSAE...) i get a "Key not valid for use in specified state." exception error even though i provide a valid second key to encrypt it. How can i overcome this error and get double encryption to work ? The...
2
6058
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 RSACryptoServiceProvider objRSA = null; public dsRSA(string keyContainerName)
0
1421
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 Profile key store in the OS? I'm assuming that if this is the case then if I were to encrypt the file in one machine, it will not decrypt on another as the key is stored in the OS. Am I right about that?
0
2491
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. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
3
4212
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 decrypt any incoming messages. In the code below you can see I can grab the publickey string from this cert and pass it back to the client. I can also create a RSACryptoServiceProvider from the cert.private key. This is all good and fine. What...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.