VB.net中的加密 - 解密文件比源文件大?

use*_*286 5 vb.net encryption cryptography

您好我一直在尝试使用System.Security.Cryptography加密和解密文件,但它不适合我

这段代码

Private 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.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
    Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Close()
End Sub
Run Code Online (Sandbox Code Playgroud)

打电话给

EncryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]
Run Code Online (Sandbox Code Playgroud)

似乎工作正常,我得到一个与源文件大小相同的文件

继承人哪里出错了

这段代码

Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
    Dim DES As New DESCryptoServiceProvider()
    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
    Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
    Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
    Dim fsDecrypted As New StreamWriter(sOutputFilename)
    fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
    fsDecrypted.Flush()
    fsDecrypted.Close()
End Sub
Run Code Online (Sandbox Code Playgroud)

打电话给

DecryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]
Run Code Online (Sandbox Code Playgroud)

输出的文件几乎是加密源文件的2倍.

什么事情发生,我肯定这几周前工作正常,我看不出任何明显的错误.

有什么想法吗?

bri*_*ums 3

主要问题是 EncryptFile 使用字节数组读取数据,而 DecryptFile 使用流读取数据。EncryptFile 和 DecryptFile 方法之间的唯一区别应该是您的ICryptoTransform分配。在 1 个过程中使用通用代码会更容易:

Private Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
    Crypto(sInputFilename, sOutputFilename, sKey, True)
End Sub

Private Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
    Crypto(sInputFilename, sOutputFilename, sKey, False)
End Sub

Private Sub Crypto(ByVal sInputFileName As String, ByVal sOutputFileName As String, ByVal sKey As String, ByVal bEncrypt As Boolean)
    'Define the service provider
    Dim DES As New DESCryptoServiceProvider()
    DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)


    'Read the input file into array
    Dim fsInput As New FileStream(sInputFileName, FileMode.Open, FileAccess.Read)
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)


    'Define the crypto transformer
    Dim cryptoTransform As ICryptoTransform

    If bEncrypt Then
        cryptoTransform = DES.CreateEncryptor()
    Else
        cryptoTransform = DES.CreateDecryptor
    End If


    'Create the encrypting streams
    Dim fsEncrypted As New FileStream(sOutputFileName, FileMode.Create, FileAccess.Write)
    Dim cryptostream As New CryptoStream(fsEncrypted, cryptoTransform, CryptoStreamMode.Write)

    'Write the output file
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Close()
End Sub
Run Code Online (Sandbox Code Playgroud)

Crypto 过程与以前的 EncryptFile 几乎相同。不同之处在于我ICryptoTransform根据您是加密还是解密来更改分配。