Solved

Base 64 Decoding

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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)

743 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

13 Experts available now in Live!

Get 1:1 Help Now