Using System;
Using System.IO;
Using System.Text;
Using System.Diagnostics;
Using System.Web.Security;
Using System.Security;
Using System.Security.Cryptography;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace Aspnet3dbbook
{
public class EncryptString
{///define 64-bit and 192-bit Kev and IV
private static byte[] Key64 = {42, 16, 93, 156, 78, 4, 218, 32};
private static byte[] IV64 = {55, 103, 246, 79, 36, 99, 167, 3};
private static byte[] Key192 = {42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 250, 155, 112, 2, 94, 11, 204, 119, 35 , 184, 197};
private static byte[] IV192 = {55, 103, 246, 79, 36, 99, 167, 3, 42, 5, 62, 83, 184, 7, 209, 13,145, 23, 200, 58, 173, 10, 121, 222};
///<summary>
///encrypted string
///</summary>
// /<param name= "valuestring" ></PARAM>
///<returns></returns>
public static string Encrypt (String valuestring)
{
if (valuestring! = "")
{ //Create an encrypted provider
descryptoserviceprovider Desprovider = new DESCryptoServiceProvider ();
///Create an encrypted stream
memorystream MemoryStream = new MemoryStream ();
cryptostream CryptoStream = new CryptoStream (MemoryStream, Desprovider. CreateEncryptor
(key64,iv64), CryptoStreamMode.Write);
streamwriter writerstream = new StreamWriter (CryptoStream);
///encrypts the given string
writerstream.write (valuestring);
writerstream.flush ();
cryptostream.flushfinalblock ();
///Returns the encrypted string
memorystream.flush ();
return (Convert.tobase64string (Memorystream.getbuffer (), 0, (int) memorystream.length));
}
return (NULL);
}
///<summary>
///Decrypt string
///</summary>
// /<param name= "valuestring" ></PARAM>
///<returns></returns>
public static string Decrypt (String valuestring)
{
if (valuestring! = "")
{ //Create decrypted provider
descryptoserviceprovider Desprovider = new DESCryptoServiceProvider ();
byte[] buffer = convert.frombase64string (valuestring);
memorystream MemoryStream = new MemoryStream ();
///decrypts the given string
cryptostream CryptoStream = new CryptoStream ( Memorystream,desprovider. CreateEncryptor
(key64,iv64), cryptostreammode.read);
StreamReader Readerstream = new StreamReader (CryptoStream);
Return (Readerstream.readtoend ());
}
return (NULL);
}
<summary>
Des encryption method
</summary>
<param name= "valuestring" ></param>
<returns></returns>
public static string Encrypttripledes (String valuestring)
{
if (valuestring! = "")
{
//Create encrypted provider
tripledescryptoserviceprovider Triprovider = new TripleDESCryptoServiceProvider ();
///Create an encrypted stream
memorystream MemoryStream = new MemoryStream ();
cryptostream CryptoStream = new CryptoStream (Memorystream,triprovider. CreateEncryptor
(key192,iv192), CryptoStreamMode.Write);
streamwriter writerstream = new StreamWriter (CryptoStream);
///encrypts the given string
writerstream.write (valuestring);
writerstream.flush ();
cryptostream.flushfinalblock ();
///Returns the encrypted string
memorystream.flush ();
return (Convert.tobase64string (Memorystream.getbuffer (), 0, (int) memorystream.length));
}
return (NULL);
}
///<summary>
///des decryption method
///</summary>
<param name= "valuestring" ></PARAM>
///<returns></returns>
public static string Decrypttripledes (String valuestring)
{
if ( valuestring = "")
{ //Create encrypted provider
TripleDESCryptoServiceProvider Triprovider = new TripleDESCryptoServiceProvider ();
///Create an encrypted stream
byte[] buffer = convert.frombase64string ( valuestring);
memorystream MemoryStream = new MemoryStream ();
cryptostream CryptoStream = new CryptoStream (Memorystream,triprovider. CreateEncryptor
(key192,iv192), cryptostreammode.read);
StreamReader Readerstream = new StreamReader (CryptoStream);
Return (Readerstream.readtoend ());
}
return (NULL);
}
}
public class Cookieencrypt
{
<summary>
Set cookies
</summary>
<param name= "Cookies" ></param>
public static void Setcookie (HttpCookie cookie)
{
HttpContext.Current.Response.Cookies.Set (cookie);
}
///<summary>
///Set cookie
///</summary>
///<param name= "key" ></PARAM>
///<param name= "valuestring" ></param>
public static void Setcookie (String key,string valuestring)
{ //Get keywords and values
key = HttpContext.Current.Server.UrlEncode (key);
valuestring = HttpContext.Current.Server.UrlEncode (valuestring);
///Set Cookie
httpcookie cookie = new HttpCookie (key,valuestring);
setcookie (cookie);
}
///<summary>
///Set cookie
///</summary>
///<param name= "key" ></PARAM>
///<param name= "valuestring" ></param>
///<param name= "Expires" ></PARAM>
public static void Setcookie (String Key,string valuestring,datetime expires)
{ //Get keywords and values
key = HttpContext.Current.Server.UrlEncode (key);
valuestring = HttpContext.Current.Server.UrlEncode (valuestring);
///Set Cookie
httpcookie cookie = new HttpCookie (key,valuestring);
cookie. Expires = Expires;
setcookie (cookie);
}
<summary>
Set cookies after using the Des method for encryption
</summary>
<param name= "Key" ></param>
<param name= "valuestring" ></param>
public static void Settripledesencryptedcookie (String key,string valuestring)
{
Get keywords and values
Key = Encryptstring.encrypttripledes (key);
valuestring = Encryptstring.encrypttripledes (valuestring);
Set cookies
Setcookie (key,valuestring);
}
<summary>
Set cookies after using the Des method for encryption
</summary>
<param name= "Key" ></param>
<param name= "valuestring" ></param>
<param name= "Expires" ></param>
public static void Settripledesencryptedcookie (String key,string valuestring,datetime expires)
{
Get keywords and values
Key = Encryptstring.encrypttripledes (key);
valuestring = Encryptstring.encrypttripledes (valuestring);
Set cookies
Setcookie (Key,valuestring,expires);
}
///<summary>
///set the cookie after encryption
///</summary>
///<param name= "key" ></PARAM>
///<param name= "valuestring" ></param>
public static void Setencryptedcookie (String key,string valuestring)
{ // /Get keyword and value
key = Encryptstring.encrypt (key);
valuestring = Encryptstring.encrypt (valuestring);
///Set Cookie
setcookie (key,valuestring);
<summary>
Set the cookie after encryption
</summary>
<param name= "Key" ></param>
<param name= "valuestring" ></param>
<param name= "Expires" ></param>
public static void Setencryptedcookie (String key,string valuestring,datetime expires)
{
Get keywords and values
Key = Encryptstring.encrypt (key);
valuestring = Encryptstring.encrypt (valuestring);
Set cookies
Setcookie (Key,valuestring,expires);
}
<summary>
Gets the cookie after the Des method is encrypted
</summary>
<param name= "Key" ></param>
<returns></returns>
public static string Gettripledesencryptedcookievalue (String key)
{
Get keywords and values
Key = Encryptstring.encrypttripledes (key);
String valuestring = Getcookievalue (key);
Get cookies
valuestring = Encryptstring.decrypttripledes (valuestring);
return (valuestring);
}
<summary>
Gets the cookie after the Des method is encrypted
</summary>
<param name= "Key" ></param>
<returns></returns>
public static string Getencryptedcookievalue (String key)
{
Get keywords and values
Key = Encryptstring.encrypt (key);
String valuestring = Getcookievalue (key);
Get cookies
valuestring = Encryptstring.decrypt (valuestring);
return (valuestring);
}
<summary>
Get cookies
</summary>
<param name= "Key" ></param>
<returns></returns>
public static HttpCookie GetCookie (String key)
{
Get keywords and values
Key = HttpContext.Current.Server.UrlEncode (key);
Get cookies
Return (HttpContext.Current.Request.Cookies.Get (key));
}
<summary>
Get cookies
</summary>
<param name= "Key" ></param>
<returns></returns>
public static string Getcookievalue (String key)
{
//Get keywords and values
string valuestring = GetCookie (key). Value;
///Get cookie
valuestring = HttpContext.Current.Server.UrlDecode ( valuestring);
return (valuestring);
}
}
}