Solved

Base 64 Decoding

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

775 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