Page 1 of 1

Base64

Posted: Sun Oct 16, 2016 10:50 am
by bluesfannoz
Has anybody come up with a means in BR to encode and decode base64 data? Receiving some data from a website that has been encoded in base64 so I need to decode it. Wanted to check before I wrote my own.

Re: Base64

Posted: Sun Oct 16, 2016 5:43 pm
by GomezL
It looks like Windows has a command line tool that might meet your needs:

http://stackoverflow.com/questions/1694 ... 4-in-batch

To encode a file:

certutil -encode inputFileName encodedOutputFileName
To decode a file:

certutil -decode encodedInputFileName decodedOutputFileName
There are a number of available verbs and options available to CERTUTIL.

To get a list of nearly all available verbs:

certutil -?
To get help on a particular verb (-encode for example):

certutil -encode -?
To get complete help for nearly all verbs:

certutil -v -?
Mysteriously, the -encodehex verb is not listed with certutil -? or certutil -v -?. But it is described using certutil -encodehex -?

Re: Base64

Posted: Sun Oct 16, 2016 8:03 pm
by bluesfannoz
Perfect! Was not making much progress coding my own!

Thanks Luis!

Re: Base64

Posted: Mon Dec 05, 2016 5:08 pm
by bluesfannoz
Updating everyone on the status of my request for Base64 Encoding and Decoding.

Luis suggested a utility on Windows that I could use called certutil. I researched and found in fact that I could. The syntax is as follows:

Code: Select all

00450     If WBPLATFORM$="WINDOWS" Then Execute "sys -m certutil -decode encoded.fil decoded.fil"
This would decode a file containing data that had been encoded base64. This works on windows.

On the Mac I found a similar utility:

Code: Select all

00470  !   If WBPLATFORM$="MAC" Then Execute "sys -m openssl -d -in encoded.fil -out decoded.fil"
Both utilities seem to work just fine. But I was not satisfied with having to make a shell call anytime I wanted to encode or decode a dataset. So I started doing some research and found this Wikipedia page that described what was required to roll your own. So that I did.

https://en.wikipedia.org/wiki/Base64

I created a library that contains FN_ENCODEDBASE64(CONTENT$) & FN_DECODEBASE64(CONTENT$) Both functions use a pointer to CONTENT$ so the result is returned in place of the what was sent. CONTENT$ is limited to 1,200,000 characters and you will need to make sure your workstack can accommodate a string of that size. It can handle any characters is the ASCII character set from 0 to 255.

Just load the library in your calling program Then use one of the functions. Here is an example of Encoding some simple text:

Code: Select all

00010     Library Release,"\macprg\base64_l": FN_ENCODEBASE64,FN_DECODEBASE64
00030     DIM CT$*1200000
00050     LET CT$="Man"
00070     LET FN_ENCODEBASE64(CT$)
00090     PRINT CT$
Your output from this should be: TWFu

This is one of the examples on the Wikipedia Page. You can read through that to see how the encoding is done.

Please test in your environment and don't hesitate to give me feedback if you find any issues!

I have attached the source code to this post. You will need to rename the extension to .wbs or .brs to use

Re: Base64

Posted: Wed Dec 07, 2016 8:20 am
by John
Thanks for sharing!