2013-02-03 64 views
2

我已經在vb.net寫了一個類來加密/解密文件,但是當我解密文件如圖片或拉鍊或辦公文件,它們看起來損壞。但是,如果我打開記事本中的解密和原始文件,他們是完全一樣的。我能做些什麼來阻止呢?DES文件加密文件腐敗

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 
+3

'但是,如果我在記事本中打開解密和加密的文件,他們是完全same.' - 如何可能嗎? –

+0

首先想到的:記得設置文件後綴到其先前的後綴。即.txt .zip。無論如何。 – WozzeC

+0

@WozzeC我總是設置文件後綴回到原來的一個 – kasperB

回答

1

我發現this answer來解決這個問題。總之,加密和解密功能應該是完全一樣的,CreateEncryptor/CreateDecryptor通話之外:

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

謝謝!這最終修復了它 – kasperB