Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

aidan_walsh

Problem Writing BlowFish in VB to a File

This topic is 5710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Encryption seems to be becoming quite the buzzword around here these days. I am currently writing an ActiveX control for a community I''m involved elsewhere on the web. What I want to do is read in a file, encrypt it and write the encrypted data to the source file. Heres what I have so far, my madness documented as comments:
Public Sub EncryptFile(ByVal file As String, ByVal KeyValue As Byte)

    ''Declaring an array to hold the encrypted lines of text as they come. That way, _
    we can reassemble them to the file afterward. To begin, we only declare to one line _
    we can always ReDim it if needs be.
    ReDim Encrypted(1) As String
    Dim i As Integer, j As Integer
    
    ''Load the file
    Call LoadFile(file)
    
    ''Return an error and dump the method if the file is already encrypted
    If strFileIndex(bytFileIndex, 2) = True Then
        intLastError = 2
        Exit Sub
    End If
    
    ''Initialise key. We will play it safe and assume that every value entered is a Hex _
    Probably better document that...
    aKey() = cv_BytesFromHex(KeyValue)
    Call blf_KeyInit(aKey())
    
    ''Encrypt the file
    Do While Not EOF(nFileNum)
        ''Next please
        i = i + 1
        ''Redeclare the size of the array to accommadate the next line
        ReDim Encrypted(i)
        ''Read in the line
        Line Input #nFileNum, sNextLine
        ''And add the encrypted line into the array
        Encrypted(i) = blf_StringEnc(sNextLine)
    Loop
    
    ''Close the old file before we output to the new one
    Close nFileNum

    ''Write to the file again
    nFileNum = FreeFile
    Open strFile For Output As nFileNum
    For j = 1 To i ''Must start at 1, there is no line 0 in a file
        Write #nFileNum, Encrypted(j)
    Next
    Close nFileNum
    
    ''Mark the file as encrypted so as to avoid embarasing re-encryption
    strFileIndex(bytFileIndex, 2) = True
    
End Sub

Public Sub DecryptFile(ByVal file As String, ByVal KeyValue As Byte)

    ReDim Decrypted(1) As String
    Dim i As Integer, j As Integer
    
    ''Load the file
    Call LoadFile(file)
    
    ''Return an error and dump the method if the file is already encrypted
    If strFileIndex(bytFileIndex, 2) = False Then
        intLastError = 3
        Exit Sub
    End If
    
    ''Initialise key. We will play it safe and assume that every value entered is a Hex _
    Probably better document that...
    aKey() = cv_BytesFromHex(KeyValue)
    Call blf_KeyInit(aKey())
    
    ''Encrypt the file
    Do While Not EOF(nFileNum)
        ''Next please
        i = i + 1
        ''Redeclare the size of the array to accommadate the next line
        ReDim Decrypted(i)
        ''Read in the line
        Line Input #nFileNum, sNextLine
        ''And add the encrypted line into the array
        Delcrypted(i) = blf_StringDec(sNextLine)
    Loop
    
    ''Close the old file before we output to the new one
    Close nFileNum

    ''Write to the file again
    nFileNum = FreeFile
    Open strFile For Output As nFileNum
    For j = 1 To i ''Must start at 1, there is no line 0 in a file
        Write #nFileNum, Decrypted(j)
    Next
    Close nFileNum
    
    ''Mark the file as encrypted so as to avoid embarasing re-encryption
    strFileIndex(bytFileIndex, 2) = False

End Sub
 
What I get are several blank lines and one line of encrypted data, but when I do a step by debug, I get encrypted data in each of the array positions. Any ideas how to fix this\ make it work? BTW: I''m using encryption modules written by David Ireland, available at pscode.com

Share this post


Link to post
Share on other sites
Advertisement
Well, I can now explain at least one of the blank lines anyhow...


ReDim(1)
...

Do While Not EOF(nFileNum)
i = i + 1 ''Redeclare the size of the array to accommadate the next line
ReDim Encrypted(i)


Dumb, dumb, dumb

Share this post


Link to post
Share on other sites
don't forget the "preserve" keyword:

ReDim Preserve Encrypted(i) <--like so

otherwise, it will empty the array everytime you change it's size.

hope it helps

"What is darkness? It doesn't exist where there's light, and without light, it's nothing..."

[edited by - doxxan on December 4, 2002 8:22:17 AM]

Share this post


Link to post
Share on other sites
*smacks jaw*

THATS MUST BE IT!!

My God, the stupidity of the absurdity of the simplicity of it all!!.

Thank you doxxan!


#Old Steve, he said to Xerox "Boys, turn your heads and cough"
And when no-one was looking he ripped their interfaces off#
Three Dead Trolls in a Baggie on the truth behind MacOS 1

SketchSoft | SketchNews


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!