The main development environment for this article is that the Visual Studio 2008,visual Studio series has always provided powerful control capabilities, but we can use these controls to write powerful applications that visual Studio2008 has also made a number of improvements in some features. This article mainly uses the latest. NET development tools of Microsoft to show the application methods of text encryption and decryption, and introduces some techniques for creating basic encryption and decryption programs. Very suitable for the beginners of the. NET development tools, has certain practical value.
Open Visual Studio 2008 on the File menu, click New Project. In the Templates pane of the New Project dialog box, click Windows Application (Windows application). Click OK (OK) as shown in Figure 1.
Select the Form1 form and add the following controls to the Form1 form: Label1, Label2, Label3 controls, TextBox1, TextBox2, TextBox3 controls, Button1, and Button2 controls.
The related properties are set as follows:
We adjust the interface appropriately, the interface setting effect is as follows: Figure 2
After the program interface is set up, we need to create the class, in the class to enter the code related to this program
Select < Project >---< add Class >---file named: Csmode Figure 3
Before programming, first we need to know the System.Security.Cryptography namespace
System.Security.Cryptography namespaces provide cryptographic services, including secure data encoding and decoding
Introduction to some commonly used enumerations
Introduction to some commonly used enumerations to enter the Csmode.vb class code event
Input code:
Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Csmode
Public Function Encrypt (ByVal strinput As String) as String
Dim byteinput as [Byte] () = Encoding.Default.GetBytes (strinput)
Dim MS as New System.IO.MemoryStream
Dim DESC as New DESCryptoServiceProvider
Dim Cstream as New CryptoStream (MS, DESC. CreateEncryptor (Encoding.Default.GetBytes ("20000000"), Encoding.Default.GetBytes ("number of bytes"), CryptoStreamMode.Write)
Cstream.write (byteinput, 0, Byteinput.length)
Cstream.flushfinalblock ()
Return convert.tobase64string (Ms. ToArray ())
End Function
Public Function Decrypt (ByVal strinput As String) as String
Dim byteinput as [Byte] () = convert.frombase64string (strinput)
Dim MS as New System.IO.MemoryStream
Dim DESC as New DESCryptoServiceProvider
Dim Cstream as New CryptoStream (MS, DESC. CreateDecryptor (Encoding.Default.GetBytes ("20000000"), Encoding.Default.GetBytes ("number of bytes"), CryptoStreamMode.Write)
Cstream.write (byteinput, 0, Byteinput.length)
Cstream.flushfinalblock ()
Return Encoding.Default.GetString (Ms. ToArray ())
End Function
End Class
Select Form1 form to enter the Code Editor
Make a declaration first
Imports System
Imports System.IO
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
Enter Button1_Click event
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim Encode as New Csmode () to encrypt text
TextBox2.Text = Encode.encrypt (TextBox1.Text)
End Sub
Enter Button2_Click Event
Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click
Dim Decode as New Csmode () to decrypt text
TextBox3.Text = Decode.decrypt (TextBox2.Text)
End Sub
After the code is entered, run the program to test. As shown in Figure 4
The success of the program, the above is a simple text encryption and decryption process, I hope you can extrapolate, using this idea can be compiled to write more powerful encryption and decryption procedures.