本篇内容主要讲解“VB.NET加密文件的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“VB.NET加密文件的实现方法”吧!
VB。网编程环境可以帮助开发人员打造一个性能稳定,安全可靠的编程平台,帮助开发人员大大提高编程效率。本文介绍使用XOR加密算法对数据进行加密,这是一种很简单的算法,使用了尽量简单的VB编程方法,通俗易懂。我们可以采用更安全的算法如DES算法,想法算法等。
在窗体中添加一RichTextBox,设置其名称属性为源文件,多行属性为真的,滚动条属性为3-both。添加一MainMenu,设置一菜单项“文件”,其下有“打开”,“保存”,“加密”,“解密”,“算子”等子菜单项。
以下是大致的VB。净加密文件的程序界面:
<李>
Public Class Form1 李,
> <李>Inherits System.Windows.Forms.Form 李,
> <李>Dim strNum As String 李,
> <李>Private Sub Form1_Load (ByVal eventSender
> <李>
, As System.Object,, ByVal eventArgs
As System.EventArgs), Handles MyBase.Load 李,strNum =,“password",李,
> <李>最终获得Sub 李,
> <李>Private Sub Form1_Resize (ByVal 事件
> <李>
Sender  As System.Object,, ByVal eventArgs
, As System.EventArgs), Handles MyBase.Resize 李,SourceFile.Width =, Form1.DefInstance.Width 李,
> <李>SourceFile.Height =, Form1.DefInstance.Height 李,
> <李>最终获得Sub ,
李>
解密过程,对xor算法而言,解密和VB。净加密文件的过程是完全一样的
<李>
Private Function XorOut (ByRef
> <李>
strNum  As 字符串,ByRef strText
As 字符串),As String 李,Dim 小姐:As Integer 李,
> <李>Dim XorValue1 As Short 李,
> <李>Dim XorValue2 As Short 李,
> <李>Dim strResult As String 李,
> <李>For 小姐:=,1,用Len (strText),李,
> <李>XorValue1 =, Asc(中期(strText,,我,,1)),李,
> <李>XorValue2 =, Asc(中期(strNum,,
> <李>
(小姐:Mod Len (strNum)), +, 1, 1)),李,strResultstrResult =, strResult ,,
> <李>
科(XorValue1 Xor  XorValue2),李,Next 李,
> <李>XorOut =, strResult 李,
> <李>最终获得Function 李,
> <李>,
李> <李>Private Sub MenuDeEncrypt_Click
> <李>
(ByVal sender  As System.Object,,
ByVal e As System.EventArgs),
Handles MenuDeEncrypt.Click 李,SourceFile.Text =, XorOut (strNum,,
> <李>
(SourceFile.Text)),李,最终获得Sub 李,
> <李>,
李> <李>Private Sub MenuEncrypt_Click
> <李>
(ByVal sender  As System.Object,
, ByVal e As System.EventArgs),
Handles MenuEncrypt.Click 李,SourceFile.Text =, XorOut (strNum,
> <李>
, (SourceFile.Text)),李,最终获得Sub 李,
> <李>,
李> <李>Private Sub MenuOpen_Click (ByVal
> <李>
sender  As System.Object,, ByVal e
, As System.EventArgs), Handles
MenuOpen.Click 李,Dim openFile1 As New OpenFileDialog 李,
> <李>& # 39;, Determine whether 从而user
> <李>
selected a file 得到,OpenFileDialog又是;李,If (openFile1.ShowDialog (),=,
> <李>
DialogResult.OK), _ 李,以及(openFile1.FileName.Length 祝辞,0),Then 李,
> <李>,
李> <李>& # 39;, Load 从而contents of 从而
> <李>
file into 从而RichTextBox又是;李,SourceFile.LoadFile (openFile1.FileName, _ 李,
> <李>RichTextBoxStreamType.PlainText),李,
> <李>最终获得If 李,
> <李>最终获得Sub 李,
> <李>,
李> <李>Private Sub MenuSave_Click (ByVal
> <李>
sender  As System.Object,, ByVal e 作为
, System.EventArgs), Handles MenuSave.Click 李,& # 39;, Create a SaveFileDialog 用request
VB.NET加密文件的实现方法