Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Base 64 Decoding

Posted on 2000-03-04
3
Medium Priority
?
190 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

877 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