Using CryptoAPI 2.0 to encrypt/decrypt a file

I am using the CryptoAPI 2.0 to encrypt/decrypt a file in VB 5.0. I have no problems reading the file (using print # and line input # to write/read) one record at a time and encrypting the record and writing it to a file.

The problem is in decrypting the file because when the record is encrypted, it turns ordinary text/numbers into control characters, some of which are carriage returns, end of line, end of file, etc. This causes problems in trying to read that record back and trying to decrypt it.

Does anyone know how to read that encrypted file, record by record so it can be decrypted?
jgycyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jgycyAuthor Commented:
Edited text of question
0
alamoCommented:
Instead of opening the file "For Input", open it "For Binary". The easiest way to read it is then to use GET.

If you *know* that your file is small (<64K), the easiest way to do it is:

Open "afile" For Binary as #1
buffer$ = space$(LOF(1))
Get #1,,buffer$
Close #1
' Now the contents of the file are in buffer$

If you don't want to read an entire file at once, or the file is too large, set buffer$ to a smaller size (such as 4096, that's usually a pretty efficient way to read files) and loop through the file reading a chunk and processing it until you reach EOF(1).

Hope this helps, good luck!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alamoCommented:
oops, looking at it, I forgot a couple of details you need to know when looping to read the file in pieces. I'll just post the code:

buffer$ = Space$(4096)
Open "afile" For Binary As #1
BytesLeft& = LOF(1)
Do Until BytesLeft& = 0
    If Len(buffer$) > BytesLeft& Then buffer$ = Space$(BytesLeft&)
    BytesLeft& = BytesLeft& - Len(buffer$)
    Get #1, , buffer$
    ' buffer$ now contains the next chunk of the file - usually 4096 bytes (until there are fewer bytes available in the file on the last iteration of the loop)
Loop
Close #1

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jgycyAuthor Commented:
The only problem with this solution is that how do I know how big each 'record' is when reading it back, since the length of encrypted record could vary. How do I know when each record ends and the other begins?
0
alamoCommented:
You don't, but you don't need to if you read and encrypt the original file properly.

I should have realized earlier I needed to tell you that you need to read the original file using the same method above, and pass the buffers of data to the encryption routine as chunks rather than lines. When you write the encrypted data out to the file, you need to write in in BINARY mode as well, using PUT, rather than PRINT.

That way, you process the original file as just chunks of bytes, which are encrypted and written to and read from the file as is. There are multiple advantages to this: it will work for all types of files (not just text), it is more efficient (less code to write and it runs faster too), and it is usually a more secure method.

If you need to break the decrypted data stream into lines, that's easy to do (simply by looking for CRLF in the decrypted text).

If you really want to read the original and encrypt and decrypt on a per-line basis, tell me and I'll give you the changes to the above code. You'll have to change the code you use to write the file, too- there is no way to know the encrypted length of each line unless you write it to the file. This seems like a lot of work for little gain, but it's your choice.

Let me know...
0
jgycyAuthor Commented:
I would like to be able to read the original and encrypt and decrypt on a per record basis.

I need it like this, because the file will have to be uploaded and read by a mainframe Cobol program. I would need to add the CRLF at the end of each record so it could be read by the Cobol program, if I do it the way you suggest.
0
alamoCommented:
If you both read and write it the way I said, then the CRLF never gets stripped off by the Line Input# and thus doesn't need to be added back. The resulting file will be identical to the original.



0
alamoCommented:
Here's the code to write the file in encrypted form so it can be read a record at a time:

Dim linelen As Integer
Dim buffer As String
Open "c:\autoexec.bat" For Input As #1
On Error Resume Next
Kill "encrypt.tmp"
On Error GoTo 0
Open "encrypt.tmp" For Binary As #2
Do Until EOF(1)
 Line Input #1, buffer
 ' encrypt ln$
 linelen = Len(buffer)
 Put #2, , linelen
 Put #2, , buffer
Loop
Close #1
Close #2

And here's the code to read it:

Dim linelen As Integer
Dim buffer As String
Open "encrypt.tmp" For Binary As #1
Do Until Loc(1) >= LOF(1)
 Get #1, , linelen
 buffer = Space$(linelen)
 Get #1, , buffer
 ' decrypt buffer
Loop
Close #1

That should do it, let me know if you have any problems with it!
0
alamoCommented:
"' encrypt ln$ " was a typo, should be "' encrypt buffer" it's just a placeholder anyway for your encyption code. And obviously you need to change the filenames :-)
0
jgycyAuthor Commented:
Thanks, Alamo. This is a great help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.