We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Using CryptoAPI 2.0 to encrypt/decrypt a file

jgycy asked
Medium Priority
Last Modified: 2020-04-13
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?
Watch Question


Edited text of question
Unlock this solution and get a sample of our free trial.
(No credit card required)

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)
Close #1


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?

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...


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.

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.

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
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
Close #1

That should do it, let me know if you have any problems with it!

"' 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 :-)


Thanks, Alamo. This is a great help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.