Click here to Skip to main content
16,008,183 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Hyperlink in Gridview Pin
Chetan Ranpariya25-Apr-07 21:27
Chetan Ranpariya25-Apr-07 21:27 
Questionhow to bind the data from dropdownlist when dropdown is in datagrid or gridview Pin
praveenanand25-Apr-07 5:16
praveenanand25-Apr-07 5:16 
AnswerRe: how to bind the data from dropdownlist when dropdown is in datagrid or gridview Pin
xibeifeijian25-Apr-07 6:06
xibeifeijian25-Apr-07 6:06 
GeneralRe: how to bind the data from dropdownlist when dropdown is in datagrid or gridview Pin
xibeifeijian25-Apr-07 6:17
xibeifeijian25-Apr-07 6:17 
Questiononline quiz Pin
saravanan0525-Apr-07 4:49
saravanan0525-Apr-07 4:49 
AnswerRe: online quiz Pin
enjoycrack25-Apr-07 4:57
enjoycrack25-Apr-07 4:57 
AnswerRe: online quiz Pin
Colin Angus Mackay25-Apr-07 5:16
Colin Angus Mackay25-Apr-07 5:16 
QuestionRSACryptoServiceProvider Pin
guroo1325-Apr-07 4:38
guroo1325-Apr-07 4:38 
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<br />
{<br />
    CspParameters CspParam;<br />
    string publicXmlString = string.Empty;<br />
    string privateXmlString = string.Empty;<br />
    protected void Page_Load(object sender, EventArgs e)<br />
    {<br />
        try<br />
        {<br />
            byte []  encrypted;<br />
            string decrypted;<br />
<br />
            UnicodeEncoding ByteConverter = new UnicodeEncoding();<br />
            encrypted = EncrptData("data to encrypt");<br />
            Response.Write(System.Text.Encoding.Unicode.GetString(encrypted));<br />
            decrypted = DecryptData(encrypted);<br />
            Response.Write(decrypted);<br />
        }<br />
        catch (Exception ex)<br />
        {<br />
        }<br />
    }<br />
<br />
    public string DecryptData(byte [] data)<br />
    {<br />
        RSACryptoServiceProvider RsaCsp;<br />
        byte[] decryptedData;<br />
        RsaCsp = new RSACryptoServiceProvider();<br />
        RsaCsp.FromXmlString(privateXmlString);<br />
        decryptedData = RsaCsp.Decrypt(data, false);<br />
        return System.Text.Encoding.Unicode.GetString(decryptedData);<br />
    }<br />
<br />
    public byte [] EncrptData(string data)<br />
    {<br />
        RSACryptoServiceProvider RsaCsp;<br />
        RSACryptoServiceProvider RsaCsp2;<br />
        UnicodeEncoding ByteConverter = new UnicodeEncoding();<br />
        CspParam = new CspParameters();<br />
        CspParam.KeyContainerName = "CustomKeys";<br />
        CspParam.Flags = CspProviderFlags.UseMachineKeyStore;<br />
<br />
        byte[] encryptedData = ByteConverter.GetBytes(data);<br />
<br />
        RsaCsp = new RSACryptoServiceProvider(CspParam);<br />
<br />
        //Getting public key<br />
        publicXmlString = RsaCsp.ToXmlString(false);<br />
        //Getting private key<br />
        privateXmlString = RsaCsp.ToXmlString(true);<br />
<br />
        RsaCsp2 = new RSACryptoServiceProvider();<br />
        RsaCsp2.FromXmlString(publicXmlString);<br />
        encryptedData = RsaCsp2.Encrypt(System.Text.Encoding.Unicode.GetBytes(data), false);<br />
<br />
        return encryptedData;<br />
    }<br />
}


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:

<br />
Exception Details: System.Security.Cryptography.CryptographicException: The system cannot find the file specified.<br />
<br />
<br />
Source Error: <br />
<br />
<br />
Line 35:         byte[] decryptedData;<br />
Line 36:         RsaCsp = new RSACryptoServiceProvider();<br />
Line 37:         RsaCsp.FromXmlString(privateXmlString);<br />
Line 38:         decryptedData = RsaCsp.Decrypt(data, false);<br />
Line 39:         return System.Text.Encoding.Unicode.GetString(decryptedData);<br />
 <br />
<br />
Source File: c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs    Line: 37 <br />
<br />
Stack Trace: <br />
<br />
<br />
[CryptographicException: The system cannot find the file specified.<br />
]<br />
   System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr) +33<br />
   System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv) +0<br />
   System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +201<br />
   System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) +262<br />
   System.Security.Cryptography.RSA.FromXmlString(String xmlString) +465<br />
   Examples_EncryptionExample.DecryptData(Byte[] data) in c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs:37<br />
   Examples_EncryptionExample.Page_Load(Object sender, EventArgs e) in c:\Data\iis\www\DefaultWeb\Phoenix\Admin\Examples\EncryptionExample.aspx.cs:28<br />
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15<br />
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34<br />
   System.Web.UI.Control.OnLoad(EventArgs e) +99<br />
   System.Web.UI.Control.LoadRecursive() +47<br />
   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 more nasty.

For now I will highly appriciate if somebody could answer my current question.
Thanks
QuestionCombo box Event catching Pin
alok_2k325-Apr-07 4:18
alok_2k325-Apr-07 4:18 
AnswerRe: Combo box Event catching Pin
guroo1325-Apr-07 4:51
guroo1325-Apr-07 4:51 
AnswerRe: Combo box Event catching Pin
Arun.Immanuel25-Apr-07 5:02
Arun.Immanuel25-Apr-07 5:02 
QuestionRAD Menu Pin
Malayil alex25-Apr-07 3:38
Malayil alex25-Apr-07 3:38 
QuestionCan you help me Pin
mghiassi25-Apr-07 3:21
mghiassi25-Apr-07 3:21 
AnswerRe: Can you help me Pin
_mubashir25-Apr-07 4:07
_mubashir25-Apr-07 4:07 
Questioncant access other classes in namespace Pin
iuliuz25-Apr-07 3:10
iuliuz25-Apr-07 3:10 
AnswerRe: cant access other classes in namespace Pin
enjoycrack25-Apr-07 4:52
enjoycrack25-Apr-07 4:52 
GeneralRe: cant access other classes in namespace Pin
iuliuz25-Apr-07 21:28
iuliuz25-Apr-07 21:28 
QuestionMembership Provider Help Needed Pin
Brendan Vogt25-Apr-07 3:04
Brendan Vogt25-Apr-07 3:04 
AnswerRe: Membership Provider Help Needed Pin
enjoycrack25-Apr-07 4:51
enjoycrack25-Apr-07 4:51 
QuestionDatabase Connection Error Pin
chaddolan25-Apr-07 3:01
chaddolan25-Apr-07 3:01 
AnswerRe: Database Connection Error Pin
enjoycrack25-Apr-07 4:48
enjoycrack25-Apr-07 4:48 
GeneralRe: Database Connection Error Pin
chaddolan26-Apr-07 2:10
chaddolan26-Apr-07 2:10 
QuestionQueryString Problems Pin
gus_br25-Apr-07 2:41
gus_br25-Apr-07 2:41 
AnswerRe: QueryString Problems Pin
Guffa25-Apr-07 7:07
Guffa25-Apr-07 7:07 
GeneralRe: QueryString Problems Pin
gus_br25-Apr-07 7:57
gus_br25-Apr-07 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.