22 Ocak 2007 Pazartesi

Asp.Net Encryption/Decryption Query String with DES Base64 encoded.

Example:


class Sifreleme.cs
--------------------------------------------------------------------------
using System.Text;
using System.Security.Cryptography;
using System;
using System.IO;
using System.Xml;

namespace Sifreleme2
{
///


/// Summary description for Class1.
///




public class Encryption64
{
//private byte[] key = {};
//private byte[] IV = {10, 20, 30, 40, 50, 60, 70, 80}; // it can be any byte value

public string Decrypt( string stringToDecrypt,
string sEncryptionKey)
{

byte[] key = {};
byte[] IV = {10, 20, 30, 40, 50, 60, 70, 80};
byte[] inputByteArray = new byte[stringToDecrypt.Length];

try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

Encoding encoding = Encoding.UTF8 ;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
throw ex;
}
}

public string Encrypt( string stringToEncrypt,
string sEncryptionKey)
{

byte[] key = {};
byte[] IV = {10, 20, 30, 40, 50, 60, 70, 80};
byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)

try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception ex)
{
throw ex;
}
}
}

}
----------------------------------------------------------------------------------------------------
WebForm1.aspx
-------------------------------------------------------------------------------------------------------
Controls:TextBox1 and Button1


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;

namespace Sifreleme2
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Encryption64 asd=new Encryption64 ();
string iso=asd.Encrypt(TextBox1.Text,"!#$a54?3");
Response.Redirect("WebForm2.aspx?search="+iso);

}
}
}
-------------------------------------------------------------------------------------------------------------------------------------
WebForm2.aspx
----------------------------------------------------------------------------------------------------------------------------------
Controls: TextBox1

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;

namespace Sifreleme2
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Encryption64 asd=new Encryption64 ();
string iso=asd.Encrypt(TextBox1.Text,"!#$a54?3");
Response.Redirect("WebForm2.aspx?search="+ iso);

}
}
}
--------------------------------------------------------------------------------------------------------------------------------------

İyi Günler,
İsmail tutumluer

The name 'Session' does not exist in the class or namespace

The name 'Session' does not exist in the class or namespace

Solution:c#
System.Web.HttpContext.Current.Session["name"]