Data Encoding Tutorials - Herong's Tutorial Examples - v5.22, by Herong Yang
Perl Built-In Implementation of Base64
This section provides a test program for the default Perl implementation of the Base64 encoding algorithm encode_base64() and decode_base64() functions.
The Base64 encoding algorithm bas been implemented in PHP language as 2 built-in functions from the MIME::Base64 model:
To test these built-in functions, I wrote this simple testing program:
# Base64_Encode_Test.pl # Copyright (c) 2010 HerongYang.com. All Rights Reserved. # use MIME::Base64; print "Test 1:\n"; test("A", "QQ=="); print "Test 2:\n"; test("AB", "QUI="); print "Test 3:\n"; test("ABC", "QUJD"); print "Test 4:\n"; $theInput = "The quick brown fox jumps over the lazy dog."; $theExpected = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4="; test($theInput, $theExpected); sub test { my ($theInput, $theExpected) = @_; $theEncoded = encode_base64($theInput); $theDecoded = decode_base64($theEncoded); print " Input : $theInput\n"; print " Encoded : $theEncoded"; print " Expected: $theExpected\n"; print " Decoded : $theDecoded\n"; }
Here is the test result:
herong> perl Base64_Encode_Test.pl Test 1: Input : A Encoded : QQ== Expected: QQ== Decoded : A Test 2: Input : AB Encoded : QUI= Expected: QUI= Decoded : AB Test 3: Input : ABC Encoded : QUJD Expected: QUJD Decoded : ABC Test 4: Input : The quick brown fox jumps over the lazy dog. Encoded : VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4= Expected: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4= Decoded : The quick brown fox jumps over the lazy dog.
The result matches my expectation perfectly.
Table of Contents
►Base64 Encoding and Decoding Tools
Base64.Guru - Base64 Online Tool
Windows Command - "certutil -encode/-decode"
Java Built-In Implementation of Base64
Java Built-In Implementation of MIME Base64
Python Built-In Implementation of Base64
Python Built-In Implementation of MIME Base64
PHP Built-In Implementation of Base64
PHP Built-In Implementation of MIME Base64
►Perl Built-In Implementation of Base64
Perl Built-In Implementation of MIME Base64
Base64URL - URL Safe Base64 Encoding