Results 1 to 3 of 3

Thread: Des Encryption Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Des Encryption Problem

    OK, I have a program that successfully encrypts files using the des encryption, but when I try to encrypt rather large files, I cannot. I believe this is because all the data is loaded into the memory, which, when loading a large file, makes sense that I have a problem.

    Code:
            ' Create input and output file streams.
    
            If in_file.Length = 0 Then Call Errors(0) : Exit Sub
            If out_file.Length = 0 Then Call Errors(1) : Exit Sub
    
            Dim in_stream As New FileStream(in_file, FileMode.Open, FileAccess.Read)
            Dim out_stream As New FileStream(out_file, FileMode.Create, FileAccess.Write)
    
            ' Make a triple DES service provider.
            Dim des_provider As New TripleDESCryptoServiceProvider
    
            ' Find a valid key size for this provider.
            Dim key_size_bits As Integer = 0
            For i As Integer = 1024 To 1 Step -1
                If des_provider.ValidKeySize(i) Then
                    key_size_bits = i
                    Exit For
                End If
            Next i
    
            Debug.Assert(key_size_bits > 0)
    
            ' Get the block size for this provider.
            Dim block_size_bits As Integer = des_provider.BlockSize
    
            frmProgress.ProgressBar1.Step = block_size_bits
    
            ' Generate the key and initialization vector.
            Dim key As Byte() = Nothing
            Dim iv As Byte() = Nothing
    
            MakeKeyAndIV(password, key_size_bits, block_size_bits, key, iv)
    
            ' Make the encryptor or decryptor.
            Dim crypto_transform As ICryptoTransform
            If encrypt Then
                crypto_transform = des_provider.CreateEncryptor(key, iv)
            Else
                crypto_transform = des_provider.CreateDecryptor(key, iv)
            End If
    
            ' Attach a crypto stream to the output stream.
            Dim crypto_stream As New CryptoStream(out_stream, crypto_transform, CryptoStreamMode.Write)
    
            ' Encrypt or decrypt the file.
            Const BLOCK_SIZE As Integer = 1024
            Dim buffer(BLOCK_SIZE) As Byte
            Dim bytes_read As Integer
    
            Me.cmdEncrypt.Enabled = False
    
            Do
                ' Read some bytes.
                bytes_read = in_stream.Read(buffer, 0, BLOCK_SIZE)
                If bytes_read = 0 Then Exit Do
    
                System.Windows.Forms.Application.DoEvents()
    
                ' Write the bytes into the CryptoStream.
                crypto_stream.Write(buffer, 0, bytes_read)
            Loop
    
            ' Close the streams.
            crypto_stream.Close()
            in_stream.Close()
            out_stream.Close()
            FileClose()
    
            Me.cmdEncrypt.Enabled = True
    How could I, if possible, encrypt one byte, then write it to a file and take the next byte, etc...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Des Encryption Problem

    I think you're mistaken about the issue. You are only reading 1 kilobyte of data into memory at a time so, from that point of view, the size of the file should be irrelevant. I think you need to look again.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Des Encryption Problem

    See, and that's what I thought, and I opened TaskManager and watched as my program went from using 201kb of memory to 201,000kb of memory.

    What can I do about this? It works great when I try smaller files, but the larger the file, the more memory it eats...

    I could upload the program source if that would make it easier...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width