C++ Source for BASE64 Encoding-Within a Clarion Application

Now with File to File examples in CW6.3 and CW7.2 and CW8 and CW10

 

Base64 Decode Problem
Aug. 2016: I recently discovered a problem when decoding data that contains bytes with 00h. This is because the C code returns CSTRINGS and Clarion truncates the returned value at the first byte. I am working to try fix this. John.

I have often used the Blowfish Encryption class (JPWBlowfish) published by of http://www.strategyonline.co.za/ Gary James /Andy Ireland.
Blowfish encryption creates an encoded string that will invariably contain non-ascii characters. This is a problem when using ODBC to write (and read) to a SQL database. This is due to the likely inclusion of some illegal characters.

Until very recently, I have been using the EncodeHEX method from the JPWBlowfish class. One thing this results in is an encoded string twice the length of the original data. This is not a problem so long as the database is designed to handle the HEX encoded length.

In a recent Softvelocity newsgroup posting, Charles Edmunds alerted me to a better way. He uses the Encode1024 method which cleverly stores the original data length in the encoded string. Then, as the encoded data will contain some non-ascii bytes, he uses the EncodeBase64 method from Capesoft’s NetTalk to ‘clean’ the encoded string. Then it is safe to email, store in SQL etc as it is now all printable characters.

The size of the encoded string will be longer than the original. The size will now be int( ( OrigLen +8 +2) / 3 ) * 4
The ” + 8″ there is the extra bytes added by JPWBlowfish method Encode1024.

For a string of 20 characters whether you use EncodeHex or the combination of Encode1024 and Base64 you will end up with an encoded string of the same length (40 characters). The combination of Encode1024 and Base64 becomes more efficient once you are working with the original data of 30 chars or more.

1) Encoding/Decoding Strings

I searched the web and found several GPL copies of the C++ source code for BASE64 encode and decode.

I selected one that looked reasonable and set about adding it to a test CW App.

This took me about 30 minutes to get working and I have posted a download link to the zipped files below.

2) Encoding and Decoding Files Sample Code/App:
(added 2010-08-28)

If you only need to encode and decode some files from disk, then the second example in v4cwB64.zip will show you how.

This has a region to where you drag-n-drop the original file from Windows Explorer. Then it will encode the file to a temp folder. Then you can decode that encoded file back to an alternative name. If you compare the original file and the recovered file, they should match byte for byte.

 

Download Links

1) Encoding/Decoding Strings example.

 

This zip file contains a CW 6.3 ABC Template APP, the CPP source file and an INClude file for the APP.

Click HERE to download the file cwB64.ZIP ( only about 12K)

(2009-02-27) Click this link for CW5.0 or CW5.5 example,

 

2) Encoding and Decoding Files Sample Code/App Example.

 

Click HERE to download the CW6.3 file v4cwB64.ZIP ( only about 12K)

For the Clarion 7.2 Version download the file B64encodeCW72.zip

For Clarion8 Version download the file B64encodeCW8.zip

For Clarion10 version download:  CW10-B64Encode.zip

By: John Griffiths

One Response to BASE64 Encoding Within a Clarion App