2

ファイルを暗号化/復号化するクラスを 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
4

1 に答える 1

1

この問題を解決するためにこの答えを見つけました。要するに、Encrypt 関数と Decrypt 関数は、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
于 2013-06-10T22:36:16.777 に答える