Example of a reversible encryption

Source: Internet
Author: User
Tags decrypt functions generator modify net visual studio
Encryption

The following code implements a method of reversible encryption. Can be used for encryption processing such as cookie,querystring.

View examples

vb.net code

<%@ Page language= "vb" autoeventwireup= "false" codebehind= "Encstring.<a href=" http://dev.21tx.com/web/asp/" target= "_blank" >asp</a>x.vb "
inherits= "Aspx<a href=" http://dev.21tx.com/web/target= "_blank" &GT;WEB&LT;/A&GT;. Encstring "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> An example of reversible encryption </title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name= "vs_defaultClientScript" content= "<a href=" http://dev.21tx.com/web/javascript/"target=" >JavaScript</a> ">
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<asp:label id= "Label1" runat= "Server" ></asp:Label>
<p align= "center" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<asp:textbox id= "TextBox1" runat= "Server" width= "96%" ></asp:TextBox>
<asp:radiobuttonlist id= "RadioButtonList1" runat= "Server" font-bold= "True"
repeatdirection= "Horizontal" autopostback= "True" onselectedindexchanged= "Showres" >
</asp:RadioButtonList>
<asp:textbox id= "TextBox2" runat= "Server" width= "96%" ></asp:TextBox>
</FONT>
</form>
</p>
</body>
</HTML>

Back-end Code EncString.aspx.vb:
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
Public Class encstring
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 as System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 as System.Web.UI.WebControls.TextBox
Protected WithEvents Form1 as System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents Label1 as System.Web.UI.WebControls.Label
Protected WithEvents RadioButtonList1 as System.Web.UI.WebControls.RadioButtonList

#Region "Web Form Designer generated Code"

' This are required by the Web Form Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()

End Sub

Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles

Mybase.init
' Codegen:this Method-required by the Web Form Designer
' Do not modify it using the ' Code Editor.
InitializeComponent ()
End Sub

#End Region

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles

MyBase.Load
' Put user code to initialize the page
Label1.Text = "

If not IsPostBack Then
Dim MyList as New ArrayList ()
Mylist.add ("Encryption")
Mylist.add ("decryption")
Radiobuttonlist1.datasource = MyList
Radiobuttonlist1.databind ()
End If
End Sub

' Encrypt
Public Shared Function Encrypttext (ByVal StrText As String) as String
Return Encrypt (StrText, "&%#@?,:*")
End Function

' Decryption
Public Shared Function Decrypttext (ByVal StrText As String) as String
Return Decrypt (StrText, "&%#@?,:*")
End Function

' Cryptographic functions
Private Shared Function Encrypt (ByVal strText As String, ByVal Strencrkey as String) as String
Dim Bykey () as Byte = {}
Dim IV () as Byte = {&h12, &h34, &h56, &h78, &AMP;H90, &hab, &AMP;HCD, &hef}
Try
Bykey = System.Text.Encoding.UTF8.GetBytes (Left (Strencrkey, 8))
Dim des as New DESCryptoServiceProvider ()
Dim Inputbytearray () as Byte = Encoding.UTF8.GetBytes (StrText)
Dim MS as New MemoryStream ()
Dim CS as New CryptoStream (MS, Des. CreateEncryptor (Bykey, IV), CryptoStreamMode.Write)
Cs. Write (Inputbytearray, 0, Inputbytearray.length)
Cs. FlushFinalBlock ()
Return convert.tobase64string (Ms. ToArray ())
Catch ex as Exception
Return ex. Message
End Try
End Function

' Decryption function
Private Shared Function Decrypt (ByVal strText As String, ByVal Sdecrkey as String) as String
Dim Bykey () as Byte = {}
Dim IV () as Byte = {&h12, &h34, &h56, &h78, &AMP;H90, &hab, &AMP;HCD, &hef}
Dim Inputbytearray (strtext.length) as Byte
Try
Bykey = System.Text.Encoding.UTF8.GetBytes (Left (Sdecrkey, 8))
Dim des as New DESCryptoServiceProvider ()
Inputbytearray = convert.frombase64string (StrText)
Dim MS as New MemoryStream ()
Dim CS as New CryptoStream (MS, Des. CreateDecryptor (Bykey, IV), CryptoStreamMode.Write)
Cs. Write (Inputbytearray, 0, Inputbytearray.length)
Cs. FlushFinalBlock ()
Dim encoding as System.Text.Encoding = System.Text.Encoding.UTF8
return encoding. GetString (Ms. ToArray ())
Catch ex as Exception
Return ex. Message
End Try
End Function

Public Sub Showres (ByVal sender as Object, ByVal e as System.EventArgs) _
Handles radiobuttonlist1.selectedindexchanged
If radiobuttonlist1.selectedindex = 0 Then
TextBox2.Text = Encrypttext (TextBox1.Text)
Else
TextBox2.Text = Decrypttext (TextBox1.Text)
End If
End Sub
End Class

C # code

Encryptstring.aspx

<%@ Page language= "C #" EnableViewState = "true" codebehind= "EncryptString.aspx.cs" autoeventwireup= "false" Inherits = "EMeng.Exam.EncryptString"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> An example of reversible encryption </title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body>
<form id= "Form1" method= "POST" runat= "Server" >
<p align= "center" > An example of a reversible encryption
<asp:textbox id= "TextBox1" runat= "Server" width= "96%" >http://dotnet.aspx.cc/</asp:TextBox>
<asp:radiobuttonlist id= "RadioButtonList1" runat= "Server" font-bold= "True" repeatdirection= "Horizontal"
autopostback= "True" ></asp:RadioButtonList>
<asp:textbox id= "TextBox2" runat= "Server" width= "96%" ></asp:TextBox>
<asp:textbox id= "Textbox3" runat= "Server" width= "96%" ></asp:TextBox>
</p>
</form>
</body>
</HTML>

EncryptString.aspx.cs

Using System;
Using System.Collections;
Using System.ComponentModel;
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.IO;
Using System.Text;
Using System.Security.Cryptography;


Namespace Emeng.exam
{
<summary>
Summary description of the encryptstring.
</summary>
public class EncryptString:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox Textbox3;
protected System.Web.UI.HtmlControls.HtmlForm Form1;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (!this. IsPostBack)
{
ArrayList MyList = new ArrayList ();
Mylist.add ("Encryption");
Mylist.add ("decryption");
Radiobuttonlist1.datasource = MyList;
Radiobuttonlist1.databind ();
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Radiobuttonlist1.selectedindexchanged + = new System.EventHandler (this. radiobuttonlist1_selectedindexchanged);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Radiobuttonlist1_selectedindexchanged (object sender, System.EventArgs e)
{
if (radiobuttonlist1.selectedindex = 0)
TextBox2.Text = Encrypttext (TextBox1.Text);
Else
TextBox3.Text = Decrypttext (TextBox2.Text);
}
Encryption
public string Encrypttext (string strText)
{
Return Encrypt (StrText, "&%#@,:*");
}

' Decryption
public string Decrypttext (string strText)
{
Return Decrypt (StrText, "&%#@,:*");
}
' Cryptographic functions
private string Encrypt (String strText, String strencrkey)
{
Byte[] Bykey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Try
{
Bykey = System.Text.Encoding.UTF8.GetBytes (strencrkey.substring (0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = Encoding.UTF8.GetBytes (StrText);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (Bykey, IV), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
Return convert.tobase64string (Ms. ToArray ());
}
catch (Exception ex)
{
Return ex. message;
}
}

' Decryption function
private string Decrypt (String strText, String sdecrkey)
{
Byte[] Bykey = {};
Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] Inputbytearray = new Byte[strtext.length];
Try
{
Bykey = System.Text.Encoding.UTF8.GetBytes (sdecrkey.substring (0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Inputbytearray = convert.frombase64string (StrText);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (Bykey, IV), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
return encoding. GetString (Ms. ToArray ());
}
catch (Exception ex)
{
Return ex. message;
}
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.