[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using CryptoAPI 2.0 to encrypt/decrypt a file

Posted on 1998-06-17
10
Medium Priority
?
234 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:jgycy
  • 6
  • 4
10 Comments
 

Author Comment

by:jgycy
ID: 1463598
Edited text of question
0
 
LVL 6

Accepted Solution

by:
alamo earned 600 total points
ID: 1463599
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
 
LVL 6

Expert Comment

by:alamo
ID: 1463600
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jgycy
ID: 1463601
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
 
LVL 6

Expert Comment

by:alamo
ID: 1463602
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
 

Author Comment

by:jgycy
ID: 1463603
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
 
LVL 6

Expert Comment

by:alamo
ID: 1463604
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
 
LVL 6

Expert Comment

by:alamo
ID: 1463605
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
 
LVL 6

Expert Comment

by:alamo
ID: 1463606
"' 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
 

Author Comment

by:jgycy
ID: 1463607
Thanks, Alamo. This is a great help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question