📄 form1.vb
字号:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.Label3 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(200, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "指定需要加密或解密的文件"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(248, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(184, 64)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 2
Me.Button1.Text = "浏览"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 96)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(200, 23)
Me.Label2.TabIndex = 3
Me.Label2.Text = "输入密码(8个字符)"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(8, 120)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(248, 21)
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(184, 184)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 5
Me.Button2.Text = "开始"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(8, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(200, 23)
Me.Label3.TabIndex = 6
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(272, 221)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Button2, Me.TextBox2, Me.Label2, Me.Button1, Me.TextBox1, Me.Label1})
Me.Name = "Form1"
Me.Text = "加密/解密工具"
Me.ResumeLayout(False)
End Sub
#End Region
' 保存密钥的8字节的数组
Public TheKey(7) As Byte
' 在向量中放入一些随机数据
Private Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
'/////加密
Sub Encrypt(ByVal inName As String, ByVal outName As String)
Try
' 创建缓冲区
Dim storage(4096) As Byte
' 已经写入的字节数量
Dim totalBytesWritten As Long = 8
' 每次写入的字节数量
Dim packageSize As Integer
' 声明文件流
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
' 源文件的大小
Dim totalFileLength As Long = fin.Length
' 创建加密对象
Dim des As New DESCryptoServiceProvider()
Dim crStream As New CryptoStream(fout, _
des.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write)
' 输出加密后的文件
While totalBytesWritten < totalFileLength
packageSize = fin.Read(storage, 0, 4096)
crStream.Write(storage, 0, packageSize)
totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
End While
crStream.Close()
Catch e As Exception
MsgBox(e.Message)
End Try
End Sub
'//////// 解密
Sub Decrypt(ByVal inName As String, ByVal outName As String)
Try
Dim storage(4096) As Byte
Dim totalBytesWritten As Long = 8
Dim packageSize As Integer
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
Dim totalFileLength As Long = fin.Length
Dim des As New DESCryptoServiceProvider()
Dim crStream As New CryptoStream(fout, _
des.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write)
Dim ex As Exception
While totalBytesWritten < totalFileLength
packageSize = fin.Read(storage, 0, 4096)
crStream.Write(storage, 0, packageSize)
totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
Console.WriteLine("已处理 {0} 字节, 总 {1} 字节", packageSize, _
totalBytesWritten)
End While
crStream.Close()
Catch e As Exception
MsgBox(e.Message & "请确认你的密码是否正确!")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As OpenFileDialog
OpenFileDialog1 = New OpenFileDialog()
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim targetfile, sourcefile As String
If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("必须输入文件名字和密码!.") : Exit Sub
Dim ext As String
Dim mainpath As String
Dim n As Integer
n = TextBox1.Text.IndexOf(".")
If n <> -1 Then
ext = TextBox1.Text.Substring(n + 1)
mainpath = TextBox1.Text.Substring(0, TextBox1.Text.Length - ext.Length - 1)
Else
mainpath = TextBox1.Text
End If
If mainpath.Substring(mainpath.Length - 2) = "xx" Then 'this file will be decrypted
sourcefile = TextBox1.Text
mainpath = mainpath.Substring(0, mainpath.Length - 2)
If ext <> "" Then mainpath &= "." & ext
targetfile = mainpath
CreateKey(TextBox2.Text)
Decrypt(sourcefile, targetfile)
Label3.Text = "解密完成..."
Exit Sub
End If
sourcefile = TextBox1.Text
mainpath &= "xx"
If ext <> "" Then mainpath &= "." & ext
targetfile = mainpath
CreateKey(TextBox2.Text)
Encrypt(sourcefile, targetfile)
Label3.Text = "加密完成..."
End Sub
Sub CreateKey(ByVal strKey As String)
' 保存密钥的字节数组
Dim arrByte(7) As Byte
Dim AscEncod As New ASCIIEncoding()
Dim i As Integer = 0
AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)
' 获得密码的Hash值
Dim hashSha As New SHA1CryptoServiceProvider()
Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)
' 将Hash值保存到密钥
For i = 0 To 7
TheKey(i) = arrHash(i)
Next i
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -