Solved

Base 64 Decoding

Posted on 2000-03-04
3
183 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

821 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