【问题标题】:des file encryption corrupts filedes 文件加密损坏文件
【发布时间】:2013-02-03 11:49:34
【问题描述】:

我在 vb.net 中编写了一个类来加密/解密文件,但是当我解密图像或 zip 或办公文件等文件时,它们看起来已损坏。但是,如果我在记事本中打开解密文件和原始文件,它们是完全相同的。我能做些什么来阻止这种情况?

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

Public Class Encrytion
Shared 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

Shared 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

End Class

【问题讨论】:

  • But if i open the decrypted and encrypted file in notepad they are exactly the same. - 这怎么可能?
  • 第一个想法:记得将文件后缀设置回它之前的后缀。即 .txt .zip .whatever.
  • @WozzeC 我总是将文件后缀设置回原来的后缀
  • 代码看起来不错,你是怎么调用这些方法的?您是否传入相同的文件名?
  • @Ameen 这是我的测试项目:dropbox.com/sh/qke1pplvia4gudb/AO5VhizAAf

标签: vb.net encryption des


【解决方案1】:

我找到this answer 来解决这个问题。简而言之,在 CreateEncryptor/CreateDecryptor 调用之外,Encrypt 和 Decrypt 函数应该完全相同:

Shared 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(DES.Key, DES.IV)

    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.Flush()

    cryptostream.Close()
End Sub

Shared Sub DecryptFile(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.CreateDecryptor(DES.Key, DES.IV)

    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.Flush()

    cryptostream.Close()
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多