Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8103293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:30:15+00:00 2026-06-05T23:30:15+00:00

Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but

  • 0

Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but its not working 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 with

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

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

heres where it 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 with

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

outputs a file that is almost 2x as large as the source file that was encrypted.

whats going on im sure this was working fine a few weeks ago and i cant see anything obviously wrong with it.

any ideas please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:30:16+00:00Added an answer on June 5, 2026 at 11:30 pm

    The main problem is that EncryptFile reads in the data using a byte array and DecryptFile is reading in the data using streams. The only difference between the EncryptFile and DecryptFile methods should be your ICryptoTransform assignment. It would be easier to have the common code in 1 procedure:

    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
    

    The Crypto procedure is nearly identical to what EncryptFile used to be. The difference is I change the ICryptoTransform assignment based on whether you’re encrypting or decrypting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use the new jquery-ui, and all but the resizable function
I've been trying to use Grapher on my GIN project. But trying to create
I've been trying to use SimpleXML , but it doesn't seem to like XML
Using LINQ , I've been trying to use the System.Linq.Dynamic library in order to
I've been trying to get codeigniter working on .Net platform with the use of
I've been trying to get WCF security working for my project, and have had
I've been trying to use the system call ptrace (using the PTRACE_SINGLESTEP macro) to
I'm trying to use a SelectList one of my views, and its just not
I've been trying to use the pipe() system call to create a shell that
I've been trying to use the Java SAX parser to parse an XML file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.