File Encryption/Decryption problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TG9yZW4=?=

    File Encryption/Decryption problem

    I’m trying to encrypt and decrypt a file in vb.net. I am using the
    TripleDESCrypto ServiceProvider encryption found in
    System.Security .Cryptography. Below is the code for my Encrypt and Decrypt
    functions.

    While my functions read and write files the encryption/decryption is not
    working properly. My test file has an original length of 66,048 bytes. My
    encrypted file ends up with 66,056 bytes … 8 bytes more than my original.
    When I decrypt the encrypted file the resultant file is also 66,056 bytes in
    length … obviously different than the original!

    I feel like I’m missing something obvious. Does anyone have any
    suggestions? Thanks for your help.

    Public Function EncryptFile(ByV al SourceFilename As String, ByVal
    DestinationFile name As String) As Boolean

    Dim fsSourceStream As System.IO.FileS tream = Nothing
    Dim encryptionStrea m As CryptoStream = Nothing
    Dim fsDestinationSt ream As System.IO.FileS tream = Nothing

    Try
    ' Open file streams for the file reading and writing
    fsSourceStream = New System.IO.FileS tream(SourceFil ename,
    FileMode.Open, IO.FileAccess.R ead)
    fsDestinationSt ream = New
    System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
    IO.FileAccess.W rite)

    'Bytes will be encrypted by an encryption stream
    encryptionStrea m = New CryptoStream(fs DestinationStre am, _
    New
    TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
    CryptoStreamMod e.Write)

    mvarBufferSize = 1024
    ' Create buffer
    Dim fileBuffer(mvar BufferSize) As Byte
    Dim accumulatedByte sRead As Integer = 0
    Dim bytesRead As Integer

    ' read the file
    ' Process bytes from fsSourceStream through the encryptionStrea m
    to the fsDestinationSt ream.
    Do
    bytesRead = fsSourceStream. Read(fileBuffer , 0, mvarBufferSize)
    If (bytesRead = 0) Then Exit Do
    encryptionStrea m.Write(fileBuf fer, 0, bytesRead)
    accumulatedByte sRead += bytesRead
    Loop

    encryptionStrea m.FlushFinalBlo ck()
    Return True

    Catch ex As Exception
    MsgBox("Excepti on error occurred in EncryptFile." & vbNewLine &
    vbNewLine _
    & ex.Message, MsgBoxStyle.OkO nly Or
    MsgBoxStyle.Inf ormation)
    Return False

    Finally

    'close streams
    If encryptionStrea m IsNot Nothing Then
    encryptionStrea m.Close()
    End If

    If fsSourceStream IsNot Nothing Then
    fsSourceStream. Close()
    End If

    If fsDestinationSt ream IsNot Nothing Then
    fsDestinationSt ream.Close()
    End If

    End Try

    End Function

    Public Function DecryptFile(ByV al SourceFilename As String, ByVal
    DestinationFile name As String) As Boolean

    Dim fsSourceStream As System.IO.FileS tream = Nothing
    Dim decryptionStrea m As CryptoStream = Nothing
    Dim fsDestinationSt ream As System.IO.FileS tream = Nothing

    Try
    ' Open file streams for the file reading and writing
    fsSourceStream = New System.IO.FileS tream(SourceFil ename,
    FileMode.Open, IO.FileAccess.R ead)
    fsDestinationSt ream = New
    System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
    IO.FileAccess.W rite)

    ' Process bytes through a CryptoStream.
    decryptionStrea m = New CryptoStream(fs SourceStream, _
    New
    TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
    CryptoStreamMod e.Read)

    ' Create buffer
    mvarBufferSize = 1024
    Dim fileBuffer(mvar BufferSize) As Byte
    Dim accumulatedByte sRead As Integer = 0
    Dim bytesRead As Integer

    ' read the file
    ' Process bytes from fsSourceStream to fsDestinationSt ream.
    Do
    bytesRead = decryptionStrea m.Read(fileBuff er, 0,
    mvarBufferSize)
    If (bytesRead = 0) Then Exit Do
    fsDestinationSt ream.Write(file Buffer, 0, bytesRead)
    accumulatedByte sRead += bytesRead
    Loop

    fsDestinationSt ream.Flush()
    Return True


    Catch ex As Exception
    MsgBox("Excepti on error occurred in DecryptFile." & vbNewLine &
    vbNewLine _
    & ex.Message, MsgBoxStyle.OkO nly Or
    MsgBoxStyle.Inf ormation)
    Return False

    Finally

    'close(streams)
    If decryptionStrea m IsNot Nothing Then
    decryptionStrea m.Close()
    End If

    If fsSourceStream IsNot Nothing Then
    fsSourceStream. Close()
    End If

    If fsDestinationSt ream IsNot Nothing Then
    fsDestinationSt ream.Close()
    End If

    End Try


    End Function

    --
    Loren Baker
  • Mike C#

    #2
    Re: File Encryption/Decryption problem

    DES-based encryption algorithms use an 8-byte block length. When you
    encrypt a file using a DES-based function it rounds up to the next nearest
    8-byte length. If you're sitting on an 8-byte boundary (like 66,048 bytes),
    it is probably padding it up to the next 8-byte boundary. I would guess
    that the length difference in the encrypted file is just an additional 8
    bytes of padding added to the output by the encryption algorithm. In fact,
    try it with a 66,041 to 66,047 byte file and I would expect the output to be
    66,048 bytes in every case.

    When you decrypt it's probably padding the end of the decrypted data with
    NUL characters (character 0, or some other padding character). You should
    be able to just strip off all character 0's from the end of the decrypted
    data. If NUL characters on the end of the file might be important (for
    instance if you're encrypting binary files) you might even consider adding
    the file length as the first 4 or 8 bytes of the data you're encrypting so
    you'll know exactly how much padding to strip off the end of the decrypted
    data. There are other padding options, such as padding with a length
    character to tell you exactly how many padding bytes were added to the end,
    like this:

    4e 5f 6a 12 9f 03 03 03

    In this example the padding character is "03" indicating that the last three
    bytes of the last 8-byte block are all padding characters. It's been a
    while for me, but I believe you can set these padding options in the
    TripleDESCrypto ServiceProvider or other service providers.

    "Loren" <Loren@discussi ons.microsoft.c omwrote in message
    news:4FC577B2-1198-45F2-9174-1A69B78B5F44@mi crosoft.com...
    I'm trying to encrypt and decrypt a file in vb.net. I am using the
    TripleDESCrypto ServiceProvider encryption found in
    System.Security .Cryptography. Below is the code for my Encrypt and
    Decrypt
    functions.
    >
    While my functions read and write files the encryption/decryption is not
    working properly. My test file has an original length of 66,048 bytes.
    My
    encrypted file ends up with 66,056 bytes . 8 bytes more than my original.
    When I decrypt the encrypted file the resultant file is also 66,056 bytes
    in
    length . obviously different than the original!
    >
    I feel like I'm missing something obvious. Does anyone have any
    suggestions? Thanks for your help.
    >
    Public Function EncryptFile(ByV al SourceFilename As String, ByVal
    DestinationFile name As String) As Boolean
    >
    Dim fsSourceStream As System.IO.FileS tream = Nothing
    Dim encryptionStrea m As CryptoStream = Nothing
    Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
    >
    Try
    ' Open file streams for the file reading and writing
    fsSourceStream = New System.IO.FileS tream(SourceFil ename,
    FileMode.Open, IO.FileAccess.R ead)
    fsDestinationSt ream = New
    System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
    IO.FileAccess.W rite)
    >
    'Bytes will be encrypted by an encryption stream
    encryptionStrea m = New CryptoStream(fs DestinationStre am, _
    New
    TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
    CryptoStreamMod e.Write)
    >
    mvarBufferSize = 1024
    ' Create buffer
    Dim fileBuffer(mvar BufferSize) As Byte
    Dim accumulatedByte sRead As Integer = 0
    Dim bytesRead As Integer
    >
    ' read the file
    ' Process bytes from fsSourceStream through the
    encryptionStrea m
    to the fsDestinationSt ream.
    Do
    bytesRead = fsSourceStream. Read(fileBuffer , 0,
    mvarBufferSize)
    If (bytesRead = 0) Then Exit Do
    encryptionStrea m.Write(fileBuf fer, 0, bytesRead)
    accumulatedByte sRead += bytesRead
    Loop
    >
    encryptionStrea m.FlushFinalBlo ck()
    Return True
    >
    Catch ex As Exception
    MsgBox("Excepti on error occurred in EncryptFile." & vbNewLine &
    vbNewLine _
    & ex.Message, MsgBoxStyle.OkO nly Or
    MsgBoxStyle.Inf ormation)
    Return False
    >
    Finally
    >
    'close streams
    If encryptionStrea m IsNot Nothing Then
    encryptionStrea m.Close()
    End If
    >
    If fsSourceStream IsNot Nothing Then
    fsSourceStream. Close()
    End If
    >
    If fsDestinationSt ream IsNot Nothing Then
    fsDestinationSt ream.Close()
    End If
    >
    End Try
    >
    End Function
    >
    Public Function DecryptFile(ByV al SourceFilename As String, ByVal
    DestinationFile name As String) As Boolean
    >
    Dim fsSourceStream As System.IO.FileS tream = Nothing
    Dim decryptionStrea m As CryptoStream = Nothing
    Dim fsDestinationSt ream As System.IO.FileS tream = Nothing
    >
    Try
    ' Open file streams for the file reading and writing
    fsSourceStream = New System.IO.FileS tream(SourceFil ename,
    FileMode.Open, IO.FileAccess.R ead)
    fsDestinationSt ream = New
    System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
    IO.FileAccess.W rite)
    >
    ' Process bytes through a CryptoStream.
    decryptionStrea m = New CryptoStream(fs SourceStream, _
    New
    TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
    CryptoStreamMod e.Read)
    >
    ' Create buffer
    mvarBufferSize = 1024
    Dim fileBuffer(mvar BufferSize) As Byte
    Dim accumulatedByte sRead As Integer = 0
    Dim bytesRead As Integer
    >
    ' read the file
    ' Process bytes from fsSourceStream to fsDestinationSt ream.
    Do
    bytesRead = decryptionStrea m.Read(fileBuff er, 0,
    mvarBufferSize)
    If (bytesRead = 0) Then Exit Do
    fsDestinationSt ream.Write(file Buffer, 0, bytesRead)
    accumulatedByte sRead += bytesRead
    Loop
    >
    fsDestinationSt ream.Flush()
    Return True
    >
    >
    Catch ex As Exception
    MsgBox("Excepti on error occurred in DecryptFile." & vbNewLine &
    vbNewLine _
    & ex.Message, MsgBoxStyle.OkO nly Or
    MsgBoxStyle.Inf ormation)
    Return False
    >
    Finally
    >
    'close(streams)
    If decryptionStrea m IsNot Nothing Then
    decryptionStrea m.Close()
    End If
    >
    If fsSourceStream IsNot Nothing Then
    fsSourceStream. Close()
    End If
    >
    If fsDestinationSt ream IsNot Nothing Then
    fsDestinationSt ream.Close()
    End If
    >
    End Try
    >
    >
    End Function
    >
    --
    Loren Baker

    Comment

    • =?Utf-8?B?TG9yZW4=?=

      #3
      Re: File Encryption/Decryption problem

      Thanks Mike. I think that you’re correct in DES-based encryption algorithms
      use an 8-byte block length. However, my problem was far more simply
      corrected (I knew I couldn’t see the forest from the trees!). My error was
      in my decryption function where I create my decryptionStrea m. The error was
      that I created and “Encryptor” instead of “Decryptor” in this function.
      After making that change both functions worked correctly.

      Thanks for your help.

      --
      Loren Baker


      "Mike C#" wrote:
      DES-based encryption algorithms use an 8-byte block length. When you
      encrypt a file using a DES-based function it rounds up to the next nearest
      8-byte length. If you're sitting on an 8-byte boundary (like 66,048 bytes),
      it is probably padding it up to the next 8-byte boundary. I would guess
      that the length difference in the encrypted file is just an additional 8
      bytes of padding added to the output by the encryption algorithm. In fact,
      try it with a 66,041 to 66,047 byte file and I would expect the output to be
      66,048 bytes in every case.
      >
      When you decrypt it's probably padding the end of the decrypted data with
      NUL characters (character 0, or some other padding character). You should
      be able to just strip off all character 0's from the end of the decrypted
      data. If NUL characters on the end of the file might be important (for
      instance if you're encrypting binary files) you might even consider adding
      the file length as the first 4 or 8 bytes of the data you're encrypting so
      you'll know exactly how much padding to strip off the end of the decrypted
      data. There are other padding options, such as padding with a length
      character to tell you exactly how many padding bytes were added to the end,
      like this:
      >
      4e 5f 6a 12 9f 03 03 03
      >
      In this example the padding character is "03" indicating that the last three
      bytes of the last 8-byte block are all padding characters. It's been a
      while for me, but I believe you can set these padding options in the
      TripleDESCrypto ServiceProvider or other service providers.
      >
      "Loren" <Loren@discussi ons.microsoft.c omwrote in message
      news:4FC577B2-1198-45F2-9174-1A69B78B5F44@mi crosoft.com...
      I'm trying to encrypt and decrypt a file in vb.net. I am using the
      TripleDESCrypto ServiceProvider encryption found in
      System.Security .Cryptography. Below is the code for my Encrypt and
      Decrypt
      functions.

      While my functions read and write files the encryption/decryption is not
      working properly. My test file has an original length of 66,048 bytes.
      My
      encrypted file ends up with 66,056 bytes . 8 bytes more than my original.
      When I decrypt the encrypted file the resultant file is also 66,056 bytes
      in
      length . obviously different than the original!

      I feel like I'm missing something obvious. Does anyone have any
      suggestions? Thanks for your help.

      Public Function EncryptFile(ByV al SourceFilename As String, ByVal
      DestinationFile name As String) As Boolean

      Dim fsSourceStream As System.IO.FileS tream = Nothing
      Dim encryptionStrea m As CryptoStream = Nothing
      Dim fsDestinationSt ream As System.IO.FileS tream = Nothing

      Try
      ' Open file streams for the file reading and writing
      fsSourceStream = New System.IO.FileS tream(SourceFil ename,
      FileMode.Open, IO.FileAccess.R ead)
      fsDestinationSt ream = New
      System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
      IO.FileAccess.W rite)

      'Bytes will be encrypted by an encryption stream
      encryptionStrea m = New CryptoStream(fs DestinationStre am, _
      New
      TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
      CryptoStreamMod e.Write)

      mvarBufferSize = 1024
      ' Create buffer
      Dim fileBuffer(mvar BufferSize) As Byte
      Dim accumulatedByte sRead As Integer = 0
      Dim bytesRead As Integer

      ' read the file
      ' Process bytes from fsSourceStream through the
      encryptionStrea m
      to the fsDestinationSt ream.
      Do
      bytesRead = fsSourceStream. Read(fileBuffer , 0,
      mvarBufferSize)
      If (bytesRead = 0) Then Exit Do
      encryptionStrea m.Write(fileBuf fer, 0, bytesRead)
      accumulatedByte sRead += bytesRead
      Loop

      encryptionStrea m.FlushFinalBlo ck()
      Return True

      Catch ex As Exception
      MsgBox("Excepti on error occurred in EncryptFile." & vbNewLine &
      vbNewLine _
      & ex.Message, MsgBoxStyle.OkO nly Or
      MsgBoxStyle.Inf ormation)
      Return False

      Finally

      'close streams
      If encryptionStrea m IsNot Nothing Then
      encryptionStrea m.Close()
      End If

      If fsSourceStream IsNot Nothing Then
      fsSourceStream. Close()
      End If

      If fsDestinationSt ream IsNot Nothing Then
      fsDestinationSt ream.Close()
      End If

      End Try

      End Function

      Public Function DecryptFile(ByV al SourceFilename As String, ByVal
      DestinationFile name As String) As Boolean

      Dim fsSourceStream As System.IO.FileS tream = Nothing
      Dim decryptionStrea m As CryptoStream = Nothing
      Dim fsDestinationSt ream As System.IO.FileS tream = Nothing

      Try
      ' Open file streams for the file reading and writing
      fsSourceStream = New System.IO.FileS tream(SourceFil ename,
      FileMode.Open, IO.FileAccess.R ead)
      fsDestinationSt ream = New
      System.IO.FileS tream(Destinati onFilename, FileMode.Create ,
      IO.FileAccess.W rite)

      ' Process bytes through a CryptoStream.
      decryptionStrea m = New CryptoStream(fs SourceStream, _
      New
      TripleDESCrypto ServiceProvider ().CreateEncryp tor(mvarKey, mvarIV),
      CryptoStreamMod e.Read)

      ' Create buffer
      mvarBufferSize = 1024
      Dim fileBuffer(mvar BufferSize) As Byte
      Dim accumulatedByte sRead As Integer = 0
      Dim bytesRead As Integer

      ' read the file
      ' Process bytes from fsSourceStream to fsDestinationSt ream.
      Do
      bytesRead = decryptionStrea m.Read(fileBuff er, 0,
      mvarBufferSize)
      If (bytesRead = 0) Then Exit Do
      fsDestinationSt ream.Write(file Buffer, 0, bytesRead)
      accumulatedByte sRead += bytesRead
      Loop

      fsDestinationSt ream.Flush()
      Return True


      Catch ex As Exception
      MsgBox("Excepti on error occurred in DecryptFile." & vbNewLine &
      vbNewLine _
      & ex.Message, MsgBoxStyle.OkO nly Or
      MsgBoxStyle.Inf ormation)
      Return False

      Finally

      'close(streams)
      If decryptionStrea m IsNot Nothing Then
      decryptionStrea m.Close()
      End If

      If fsSourceStream IsNot Nothing Then
      fsSourceStream. Close()
      End If

      If fsDestinationSt ream IsNot Nothing Then
      fsDestinationSt ream.Close()
      End If

      End Try


      End Function

      --
      Loren Baker
      >
      >
      >

      Comment

      • Mike C#

        #4
        Re: File Encryption/Decryption problem


        "Loren" <Loren@discussi ons.microsoft.c omwrote in message
        news:12DA5CF5-572C-46B8-817C-5D30D822E312@mi crosoft.com...
        Thanks Mike. I think that you're correct in DES-based encryption
        algorithms
        use an 8-byte block length. However, my problem was far more simply
        corrected (I knew I couldn't see the forest from the trees!). My error
        was
        in my decryption function where I create my decryptionStrea m. The error
        was
        that I created and "Encryptor" instead of "Decryptor" in this function.
        After making that change both functions worked correctly.
        >
        Thanks for your help.
        >
        --
        Loren Baker.
        Very cool, so I guess the decryption service provider automatically strips
        the extra padding when you decrypt it. Thanks.


        Comment

        Working...