Cryptography in VB.net - decrypted file larger than source file?

Hi, I am trying to use System.Security.Cryptography to encrypt and decrypt a file, but it does not work for me

this code

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

called by

EncryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]

seems to work fine and i get the file the same size as the source file

heres where he goes wrong though

this code

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

called by

DecryptFile(OpenFileDialog1.FileName, SaveFileDialog1.FileName, "12345678")[/CODE]

displays a file whose size is almost 2 times the size of the original file that was encrypted.

What happens, I'm sure it worked fine a few weeks ago, and I don't see anything obvious about it.

any ideas please?

+5
source share
1 answer

, 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

Crypto EncryptFile. , ICryptoTransform , .

+3

All Articles