Archived

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

Problem Writing BlowFish in VB to a File

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

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

''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)
i = i + 1
''Redeclare the size of the array to accommadate the next line
ReDim Encrypted(i)
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

''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)
i = i + 1
''Redeclare the size of the array to accommadate the next line
ReDim Decrypted(i)
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 on other sites
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 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 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

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• Who's Online (See full list)

There are no registered users currently online

×