Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Using CryptoAPI 2.0 to encrypt/decrypt a file

Posted on 1998-06-17
10
224 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 150 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending a email via excel using vba 6 97
message box in access 4 51
Copy a row 12 64
Export PDF Form fields to Access  or Excel  in Tab order 16 80
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

829 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