Solved

Base 64 Decoding

Posted on 2000-03-04
3
181 Views
Last Modified: 2013-12-25
I have a file on the server(linux), which iwill be passing to the browser using a simple cgi program.

But the file is base64 encoded, how do i decode it.
0
Comment
Question by:LotsOfQuestions
3 Comments
 
LVL 1

Expert Comment

by:elpmet
ID: 2582999
LotsOfQuestions,

Use the following metamail command:

   metamail -w < your_encoded_filename

Elpmet
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 2583379
use MIME::Base64;

$decoded = decode_base64($encoded);

0
 
LVL 8

Accepted Solution

by:
jhurst earned 50 total points
ID: 2586224
Here is some code that I wrote not wanting to require any other modules - as you can see I tested it with a GIF - so I included the test program too:

There is a to64 and a from64, as you can see.


#!\usr\bin\perl

to64("test1.gif","temp.fil");
print "\n";
from64("temp.fil","test2.gif");
#system("cat test2.gif");
exit;

sub to64() #convert to base64, the parameters are the in and out file names
  {
  # calling sequence:
  #  from64("inputFile","OutputFile");

  my @v;
  my $x;
  my $i;
  my $c;
  my $val;
  my $y;
  my $lenWas;

  my $c64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      #the base 64 character set

  open(OUTFILE,">$_[1]"); #where to put the new stuff unencoded data
  binmode(OUTFILE);
  open(INFILE,"$_[0]"); #where to read the encoded file
  binmode(INFILE);
  while ($lenWas=read(INFILE,$x,3)) #read all triplets of input
    {
    @v=unpack("CCC",$x); #the three characters numberic values
    $val=0;
    for ($i=0;$i<3;$i++)
      {$val<<=8;
      $val+=$v[$i]; #make one 24 bit number
      }
    $y="";
    for ($i=0;$i<4;$i++) #make the 4 nibbles
      {
      $c= $val & 63; #the six bits
      $c= substr($c64,$c,1); #the encoded value for this
      $y=$c . $y; #read for output
      $val >>=6; #next bits soon
      }
    if ($count++ > 16) #splie into lines
      {$count=0;
      $y.="\n";
      #print "-";
      }
    if ($lenWas == 1) {substr($y,3)="=="} #as padding required
    if ($lenWas == 2) {substr($y,4)="="} #as padding required
    print OUTFILE $y;
    if ($lenWas < 3) {last} #we did it all
    }
  close(INFILE);
  print OUTFILE "\n";
  close(OUTFILE);
  }

sub from64() #convert from base64, the parameters are the in and out file names
  {
  # calling sequence:
  #  from64("inputFile","OutputFile");
  #
  my $c64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      #the base 64 character set
  my @v;
  my $y;
  my $x;

  open(OUTFILE,">$_[1]"); #where to put the new stuff unencoded data
  binmode(OUTFILE);
  open(INFILE,"$_[0]"); #where to read the encoded file
  while ($x=<INFILE>) #do all of it
    {
   # print "+";
    while (length($x) > 2) #there is still some input in this line
      {
      my $val=0; #where we will make the next triplet
      my $i;
      for ($i=0;$i<4;$i++)  #four characters in make 3 out
        {
        my $c1=index($c64,substr($x,$i,1)); #the characters are
        $val<<=6; #put thes 6 bits into the new 24 bit number
        $val+=$c1;
        }
      for ($i=0;$i<3;$i++) #now convert this to three characters
        {$v[$i]=$val & 255; #split the 24 into 3 8s
        $val >>=8; #what we did
        }
      $y.=pack("ccc",$v[2],$v[1],$v[0]);
      $x=substr($x,4);
      }
    print OUTFILE $y;
    $y="";
    }
  close(INFILE);
  close(OUTFILE);
  }
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now