Base64

More advanced topics discussed.

Moderators: Susan Smith, admin, Gabriel

Post Reply
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Base64

Post 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.
Steve Koger
Computer Specialist
SEKESC-MACS Division
GomezL
Posts: 258
Joined: Wed Apr 29, 2009 5:51 am
Contact:

Re: Base64

Post 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 -?
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Base64

Post by bluesfannoz »

Perfect! Was not making much progress coding my own!

Thanks Luis!
Steve Koger
Computer Specialist
SEKESC-MACS Division
bluesfannoz
Posts: 291
Joined: Fri Jun 19, 2009 9:01 am
Location: Lawrence, Kansas
Contact:

Re: Base64

Post 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
Attachments
base64_l.txt
Rename extension to .wbs or .brs
(7.13 KiB) Downloaded 697 times
Steve Koger
Computer Specialist
SEKESC-MACS Division
John
Posts: 555
Joined: Sun Apr 26, 2009 8:27 am

Re: Base64

Post by John »

Thanks for sharing!
John Bowman
Post Reply