首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >des文件加密损坏文件

des文件加密损坏文件
EN

Stack Overflow用户
提问于 2013-02-03 19:49:35
回答 1查看 797关注 0票数 2

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-11 06:36:17

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14672146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档