Click to See Complete Forum and Search --> : passing keys between pages - cryptography


Banupriya
March 17th, 2005, 11:11 PM
Hello,

I am developing an application in which I am using the RSA algo. to encrypt and the userID when I am loging in the first page....

Then I have to pass the encrypted value to the second page...

In the second page..I have to decrypt the value & use it...

For this I need the combined key that is generated during encription ...

Please say how I get the key from 1st to 2nd page....

Here is the code that I used..

Here is a sample that I created for cryptography...RSA algorithm....ASP.NET (VB.NET)

Please help me with the code if possible....

The Front End HTML Code Used in first page (WebForm1.aspx) is:
<form name="Form1" action="./WebForm2.aspx" method="post">
<table width="500" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>User ID</td>
<td><input type="text" name="txtUID"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="txtPwd"></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btnSubmit" value="Login"></td>
</tr>
</table>
</form>

And the Code behind vb.net of second page (WebForm2.aspx) is:
Protected DecryptedStrAsString As String
Protected EncryptedStrAsString As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim cspParam As CspParameters
cspParam = New CspParameters
cspParam.Flags = CspProviderFlags.UseMachineKeyStore
Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider
Dim publicKey As String = RSA.ToXmlString(False) ' gets the public key
Dim privateKey As String = RSA.ToXmlString(True) ' gets the private key
Response.Write(privateKey)
'Encrypt the UserID
Dim strUserID As String = Request.Form("txtUID")
Dim RSA2 As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParam)
RSA2.FromXmlString(privateKey)
Dim EncryptedStrAsByt() As Byte = RSA2.Encrypt(System.Text.Encoding.Unicode.GetBytes(strUserID), False)
EncryptedStrAsString = System.Text.Encoding.Unicode.GetString(EncryptedStrAsByt)
'Decrypt the UserID
Dim RSA3 As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParam)
RSA3.FromXmlString(publicKey)
Dim DecryptedStrAsByt() As Byte = RSA3.Decrypt(System.Text.Encoding.Unicode.GetBytes(EncryptedStrAsString), False)
DecryptedStrAsString = System.Text.Encoding.Unicode.GetString(DecryptedStrAsByt)
End Sub

Note:
We can also use the following instead of ToXMLString(...) and FromXMLString(...)

Dim completeParams As RSAParameters = RSA.ExportParameters(True)
Dim publicParams As RSAParameters = RSA.ExportParameters(False)

RSA3.ImportParameters(publicParams)
RSA3.ImportParameters(completeParams)

Here I did both the encryption and decryption on the same page.

But I need to export the publicKey generated in WebForm2.aspx to WebForm3.aspx and then wants to decrypt the data.

Is it possible to do so?

If so...Please help me providing some sample codes...

Thanks & Regards
Banu