使用 Visual Basic 2008 的基本 .NET 加密服务






2.60/5 (2投票s)
了解如何使用两种简单的 .NET 方法成为唯一访问您文件的人。
引言
从 Microsoft .NET Framework 的 2.0 版本开始,System.IO.File
类公开了两个非常有趣的方法,它们允许对任何文件应用(或从中删除)一种保护,以便只有应用加密的用户才能访问该文件本身。
例如,如果多个人可以使用不同的用户帐户访问同一系统,则所有用户都可以访问某些文件夹,因此,同样适用于位于这些文件夹内的文件。
使用这些方法,我们可以将这种保护应用于我们的用户帐户创建的所有文件,从而防止其他人访问它们。我们正在谈论的方法是 System.IO.File.Encrypt
和 System.IO.File.Decrypt
。本文的完整 VB 9.0 源代码可从上面的链接获得。请记住,这种加密只能在 NTFS 文件系统上使用。
使用代码
经过简短的介绍后,就该编写代码了。我在下面的代码中添加了注释,以便阅读本文应该更流畅一些。我们将创建一个 Windows 窗体应用程序,该应用程序应如图所示。首先,需要一个 Imports
指令
Imports System.IO
以下代码分配给“浏览”按钮,以便用户可以选择要加密的文件,并分配给“确定”按钮
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Sfoglia.Click
With OFD1
.Filter = "All files|*.*"
.Title = "Select a file to encrypt"
If .ShowDialog = Windows.Forms.DialogResult.OK And _
String.IsNullOrEmpty(.FileName) = False Then
TextBox1.Text = .FileName
Else
Exit Sub
End If
End With
End Sub
以下方法在加密终止时调用,并打开包含文件本身的文件夹。
Private Sub OpenFolder()
Process.Start("Explorer.exe", Path.GetDirectoryName(TextBox1.Text))
End Sub
以下代码验证是否已指定文件名,然后调用相应的方法(Encrypt
或 Decrypt
);否则,将显示一条错误消息。
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
'If the TextBox contains a valid string...
If String.IsNullOrEmpty(TextBox1.Text) = False Then
'...encrypts the related file
Encrypt(TextBox1.Text)
Else
MessageBox.Show("No file specified yet!")
Exit Sub
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button5.Click
'If the TextBox contains a valid string...
If String.IsNullOrEmpty(TextBox1.Text) = False Then
'...decrypts the related file
Decrypt(TextBox1.Text)
Else
MessageBox.Show("No file specified yet!")
Exit Sub
End If
End Sub
以下方法将加密应用于指定的文件,并管理此场景的所有可能异常
Private Sub Encrypt(ByVal Filename As String)
Try
File.Encrypt(Filename)
Catch ex As ArgumentNullException
MessageBox.Show("A null argument was specified")
Catch ex As ArgumentException
MessageBox.Show("Specified pathname contains invalid characters")
Catch ex As DriveNotFoundException
MessageBox.Show("Invalid drive")
Catch ex As FileNotFoundException
MessageBox.Show("File not found")
Catch ex As PathTooLongException
MessageBox.Show("Pathname too long")
Catch ex As IOException
MessageBox.Show("I/O error")
Catch ex As PlatformNotSupportedException
MessageBox.Show("You're trying to execute this " & _
"action onto a non Windows NT operating system")
Catch ex As NotSupportedException
MessageBox.Show("Current file system is not NTFS")
Catch ex As UnauthorizedAccessException
MessageBox.Show("You're not authorized to execute the selected action")
End Try
OpenFolder()
End Sub
正如您可以轻松注意到的那样,我们编写的关于管理异常的代码比其他任何内容都多!以下代码实现了 Decrypt
方法
Private Sub Decrypt(ByVal Filename As String)
Try
File.Decrypt(Filename)
Catch ex As ArgumentNullException
MessageBox.Show("A null argument was specified")
Catch ex As ArgumentException
MessageBox.Show("Specified pathname contains invalid characters")
Catch ex As DriveNotFoundException
MessageBox.Show("Invalid drive")
Catch ex As FileNotFoundException
MessageBox.Show("File not found")
Catch ex As PathTooLongException
MessageBox.Show("Pathname too long")
Catch ex As IOException
MessageBox.Show("I/O error")
Catch ex As PlatformNotSupportedException
MessageBox.Show("You're trying to execute this action " & _
"onto a non Windows NT operating system")
Catch ex As NotSupportedException
MessageBox.Show("Current file system is not NTFS")
Catch ex As UnauthorizedAccessException
MessageBox.Show("You're not authorized to execute the selected action")
End Try
OpenFolder()
End Sub
这两种方法的工作方式相同,并且管理的异常也相同。应用加密后,文件名在资源管理器中显示为绿色标记,而不是黑色。这只是为了让用户了解他们正在查看加密的文档。当文件被解密时,颜色会反转为黑色。
我们的文件发生了什么?
要检查加密如何影响文件,您应该使用另一个用户帐户(例如,Guest)访问系统。然后,尝试打开先前加密的文档。如果一切正常,Windows 将不允许您打开该文件。要恢复文件的原始状态,请使用第一个用户帐户访问它并解密该文件。
关注点
这可能是添加文件保护的最简单方法。但是 .NET Framework 中的加密是一项非常强大的任务,您可以通过阅读官方 MSDN 文档来更深入地研究它。但是,如果您确实需要快速而简单的东西,Encrypt
和 Decrypt
可能是一个不错的选择。