日期:2008-08-03  浏览次数:20483 次

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography

Imports System.Text

Module Encrypt
    Private Const sSecretKey As String = "password"

    Public Sub Main()
        EncryptFile("c:\temp\test.txt", _
                        "c:\temp\Encrypted.txt", _
                        sSecretKey)
        DecryptFile("c:\temp\Encrypted.txt", _
                    "c:\temp\Decrypted.txt", _
                    sSecretKey)
    End Sub

    Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)

        Dim fsInput As New FileStream(sInputFilename, _
                                    FileMode.Open, FileAccess.Read)
        Dim fsEncrypted As New FileStream(sOutputFilename, _
                                    FileMode.Create, FileAccess.Write)

        Dim DES As New DESCryptoServiceProvider()

        '为DES算法设置安全码.
        '必须是64位,8个字节
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)


        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

        '创建DES加密码实例
        Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
        '
        Dim cryptostream As New CryptoStream(fsEncrypted, _
                                            desencrypt, _
                                            CryptoStreamMode.Write)

        '读取文件到数组
        Dim bytearrayinput(fsInput.Length - 1)