Perl Built-In Implementation of MIME Base64

This section provides a tutorial example on how to perform Base64 encoding on binary files with both standard and MIME options using the default Perl implementation of the Base64 encoding algorithm.

Actually, Perl only offer the MIME variation of the Base64 algorithm to wrap the encoded output into 76 characters to meet the MIME specification.

To perform the standard Base64 encoding, you need to remove line breaks from encoded string yourself.

Here is a sample program that allows you to perform Base64 encoding on any given file with both standard and MIME options:

# Base64_Mime_Encoder.pl
# Copyright (c) 2010 HerongYang.com. All Rights Reserved.

  use MIME::Base64;

  my $count = scalar(@ARGV);
  if ($count<2) {
    print("Usage:\n");
    print("perl Base64_Mime_Encoder.pl input_file mime\n");
    exit;
  }
  my ($fileName, $mime) = @ARGV;
   
  $size = -s $fileName;
  open(IN, "< $fileName");
  binmode(IN);
  read(IN, $inputBytes, $size);

  if ($mime eq "mime") {
    $theEncoded = encode_base64($inputBytes);
  } else {
    $theEncoded = encode_base64($inputBytes);
    $theEncoded =~ s/\r|\n//sg;
  }
  print($theEncoded."\n");

To try the above program, you can download the binary image file from my Website:

herong> curl http://herongyang.com/_logo.png > icon.png  

Now encode this binary file with both MIME and standard option:

herong> perl Base64_Mime_Encoder.pl icon.png mime 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAEYSURBVEhL7ZbLDcIwDIYdBGfYADahm1E24QRr
9AxLNBvAuUjgJE7ruI5S8RAXPslqaltxfsdSax4IfJEZPQeMSa2EyLdmTwHCKUhwLmYt1BTIIPIl
eovkyZUUj8zrrgDzJb0EQovuN64SbFt7d4/cCLE2bYWFCsxi2e9xutxCwOvormOV3OEMcxJkXLiO
55A/vuSIy+MsVrTA0+5TRfbQ0ErBl9EUIO2hGpxobUUXznzeCO6KCkI0U8DDA5oxuLvcooibjAy2
Zq3BQdEoF8CxsxWtBevdllZ51AJx1KJtGlQtwU4keTiiGmUFGdymU3i5wFTUAuk8BJOMcrpXL/lN
/gWKfK6A+A5EpheQYzORr7foB38VHwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

herong> perl Base64_Mime_Encoder.pl icon.png standard 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

As you can see, the MIME Base64 encoding is identical to the standard Base64 encoding, except that it adds line breaks to keep the output in 76-character line format.

According the Base64 specification, adding line breaks and other whitespaces in the encoded output is allowed.

Table of Contents

 About This Book

 Base64 Encoding

Base64 Encoding and Decoding Tools

 Base64.Guru - Base64 Online Tool

 Windows Command - "certutil -encode/-decode"

 Linux Command - "base64"

 macOS Command - "base64"

 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

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB