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\auserforapplicatio
n" (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.Ge
tString(en
crypted));
decrypted = DecryptData(encrypted);
Response.Write(decrypted);
}
catch (Exception ex)
{
}
}
public string DecryptData(byte [] data)
{
RSACryptoServiceProvider RsaCsp;
byte[] decryptedData;
RsaCsp = new RSACryptoServiceProvider()
;
RsaCsp.FromXmlString(priva
teXmlStrin
g);
decryptedData = RsaCsp.Decrypt(data, false);
return System.Text.Encoding.Unico
de.GetStri
ng(decrypt
edData);
}
public byte [] EncrptData(string data)
{
RSACryptoServiceProvider RsaCsp;
RSACryptoServiceProvider RsaCsp2;
UnicodeEncoding ByteConverter = new UnicodeEncoding();
CspParam = new CspParameters();
CspParam.KeyContainerName = "CustomKeys";
CspParam.Flags = CspProviderFlags.UseMachin
eKeyStore;
byte[] encryptedData = ByteConverter.GetBytes(dat
a);
RsaCsp = new RSACryptoServiceProvider(C
spParam);
//Getting public key
publicXmlString = RsaCsp.ToXmlString(false);
//Getting private key
privateXmlString = RsaCsp.ToXmlString(true);
RsaCsp2 = new RSACryptoServiceProvider()
;
RsaCsp2.FromXmlString(publ
icXmlStrin
g);
encryptedData = RsaCsp2.Encrypt(System.Tex
t.Encoding
.Unicode.G
etBytes(da
ta), 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.Cryptograp
hy.Cryptog
raphicExce
ption: The system cannot find the file specified.
Source Error:
Line 35: byte[] decryptedData;
Line 36: RsaCsp = new RSACryptoServiceProvider()
;
Line 37: RsaCsp.FromXmlString(priva
teXmlStrin
g);
Line 38: decryptedData = RsaCsp.Decrypt(data, false);
Line 39: return System.Text.Encoding.Unico
de.GetStri
ng(decrypt
edData);
Source File: c:\Data\iis\www\DefaultWeb
\Phoenix\A
dmin\Examp
les\Encryp
tionExampl
e.aspx.cs Line: 37
Stack Trace:
[CryptographicException: The system cannot find the file specified.
]
System.Security.Cryptograp
hy.Cryptog
raphicExce
ption.Thro
wCryptogap
hicExcepti
on(Int32 hr) +33
System.Security.Cryptograp
hy.Utils._
CreateCSP(
CspParamet
ers param, Boolean randomKeyContainer, SafeProvHandle& hProv) +0
System.Security.Cryptograp
hy.Utils.C
reateProvH
andle(CspP
arameters parameters, Boolean randomKeyContainer) +201
System.Security.Cryptograp
hy.RSACryp
toServiceP
rovider.Im
portParame
ters(RSAPa
rameters parameters) +262
System.Security.Cryptograp
hy.RSA.Fro
mXmlString
(String xmlString) +465
Examples_EncryptionExample
.DecryptDa
ta(Byte[] data) in c:\Data\iis\www\DefaultWeb
\Phoenix\A
dmin\Examp
les\Encryp
tionExampl
e.aspx.cs:
37
Examples_EncryptionExample
.Page_Load
(Object sender, EventArgs e) in c:\Data\iis\www\DefaultWeb
\Phoenix\A
dmin\Examp
les\Encryp
tionExampl
e.aspx.cs:
28
System.Web.Util.CalliHelpe
r.EventArg
FunctionCa
ller(IntPt
r fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEvent
HandlerDel
egateProxy
.Callback(
Object sender, EventArgs e) +34
System.Web.UI.Control.OnLo
ad(EventAr
gs e) +99
System.Web.UI.Control.Load
Recursive(
) +47
System.Web.UI.Page.Process
RequestMai
n(Boolean includeStagesBeforeAsyncPo
int, Boolean includeStagesAfterAsyncPoi
nt) +1061
I could control the above mentioned error by doing a nasty trick which is. The account "domain\auserforapplicatio
n" which I am impersonating my application with. I used a utility in windows xp accessible from "All Programs/Accessories/Syste
m Tool/Schedule Tasks" to create a process e.g. executed calc.exe application under the account "domain\auserforapplicatio
n". 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