.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

encrypt querystring value

Posted By: Ramesh     Posted Date: October 05, 2009    Points:2   Category :ASP.Net
Hello Friends,

How to encrypt querystring value in asp.net and decrypt in form code.

I am passing Value using Querysting and i need to encrypt that value so that user can not able to see the value passed.

I am using ASP.NET and C#

Regards
Ramesh

Responses
Author: Lakhan Pal Garg             
Posted Date: October 06, 2009     Points: 5   

Hi Ramesh-
Check this link for Encryption:
http://lakhangarg.blogspot.com/2009/10/encrypt-string-data.html

and for decryption:
http://lakhangarg.blogspot.com/2009/10/decrypt-string-data.html

Thanks & Regards
Lakhan Pal Garg
http://lakhangarg.blogspot.com

Author: Nabin             
Posted Date: October 13, 2009     Points: 5   

Hi Ramesh
Please check the steps and code given bellow:-

==============================================================================================================
Program for incyption and Decryption Implimantation
==============================================================================================================
steps:
1. Create two pages as Default.aspx and Default2.aspx
2. redirect the page default.aspx page to default2.aspx with the help of a button click event.
3.During redirection send some value with the page address .
4.To encrypt and Decrypt the value during trassmission write the code as below:

==============================================================================================================
Page Default.aspx code(using encryption method)
==============================================================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
string name = "Raj";
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
public static string Encrypt(string TextToBeEncrypted)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric encryptor object.
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
//Defines a stream that links data streams to cryptographic transformations
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(PlainText, 0, PlainText.Length);
//Writes the final state and clears the buffer
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string EncryptedData = Convert.ToBase64String(CipherBytes);
return EncryptedData;

}
protected void btnGoTo_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?id="+Encrypt(name));
}
}

==============================================================================================================
Page Default2.aspx code(using decryption method)
==============================================================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
string Name = Decrypt(Request.QueryString["id"].ToString());
Response.Write(Name);
}
public static string Decrypt(string TextToBeDecrypted)
{

RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric Rijndael decryptor object.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);
//Defines the cryptographics stream for decryption.THe stream contains decrpted data
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] PlainText = new byte[EncryptedData.Length];
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();
//Converting to string
string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
return DecryptedData;


}
}


Thanks
Nabin

Author: Neet_Aarzoo             
Posted Date: October 15, 2009     Points: 5   


Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
Dim I As Integer
Dim Word1 As String, Word2 As String
'Read typed word - initialize encoded word
Word1 = txtWord.Text
Word2 = ""
'Find ASCII representation for each character and subtract 1
For I = 1 To Len(Word1)
Word2 = Word2 + Chr(Asc(Mid(Word1, I, 1)) - 1)
Next I
'Display encoded word
txtWord.Text = Word2
End Sub

Private Sub btnDecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecode.Click
Dim I As Integer
Dim Word1 As String, Word2 As String
'Read typed word - initialize decoded word
Word1 = txtWord.Text
Word2 = ""
'Find ASCII representation for each character and add 1
For I = 1 To Len(Word1)
Word2 = Word2 + Chr(Asc(Mid(Word1, I, 1)) + 1)
Next I
'Display encoded word
txtWord.Text = Word2
End Sub
End Class


regards
Neetu

Author: Neet_Aarzoo             
Posted Date: October 15, 2009     Points: 5   


Private Sub btnEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncode.Click
Dim I As Integer
Dim Word1 As String, Word2 As String
'Read typed word - initialize encoded word
Word1 = txtWord.Text
Word2 = ""
'Find ASCII representation for each character and subtract 1
For I = 1 To Len(Word1)
Word2 = Word2 + Chr(Asc(Mid(Word1, I, 1)) - 1)
Next I
'Display encoded word
txtWord.Text = Word2
End Sub

Private Sub btnDecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecode.Click
Dim I As Integer
Dim Word1 As String, Word2 As String
'Read typed word - initialize decoded word
Word1 = txtWord.Text
Word2 = ""
'Find ASCII representation for each character and add 1
For I = 1 To Len(Word1)
Word2 = Word2 + Chr(Asc(Mid(Word1, I, 1)) + 1)
Next I
'Display encoded word
txtWord.Text = Word2
End Sub
End Class


regards
Neetu



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend