1. perl
  2. php
  3. python
  4. ruby

perl

#!/usr/bin/perl -wT

BEGIN { use CGI::Carp qw/fatalsToBrowser/ }

use strict;
use CGI qw/:standard/;
use MIME::Base64;

my $mode = param('mode') || 'encode';
my $text = param('before_field') || '';
my $after_field = "";

if (length $text > 0) {
    if ($mode eq 'encode') {
        $after_field = encode_base64($text);
    }
    elsif ($mode eq 'decode') {
        $after_field = decode_base64($text);
    }
    else {
        die "Error: wrong mode";
    }
}

put
    header,
    start_html("Base64 Encode/Decode"),
    start_form("post", url),
    p(textarea("before_field", $text, 5, 80, -override=>1)),
    p(textarea("after_field", $after_field, 5, 80, -override=>1)),
    p(radio_group("mode", [qw/encode decode/], "encode", 0,
                 {encode => "Encode", "decode" => "Decode"}),
      submit),
    end_form,
    end_html;
    
__END__
    

php

<?php

error_reporting(E_ALL);

if (isset($_REQUEST['before_field'])) {
	if (ini_get('magic_quotes_gpc')) {
		$text = stripslashes($_REQUEST['before_field']);
	}
	else {
		$text = $_REQUEST['before_field'];
	}
}
else {
	$text = '';
}

?>
<html>
<head>
<title>Base64 Encode/Decode</title>
<style type="text/css">
.field { width:680px; height:100px; font-size:12px;  }
</style>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<p><textarea name="before_field" class="field"><?php
	echo ($text) ? htmlspecialchars($text) : "Input here.";
?></textarea></p>
<p><textarea name="after_field" class="field" readonly="readonly"><?php
	if (isset($_REQUEST['mode'])) {
		switch ($_REQUEST['mode']) {
		case 'encode':
			echo chunk_split(base64_encode($text));
			break;
		case 'decode':
			echo base64_decode($text);
			break;
		default:
			break;
		}
	}
?></textarea></p>
<p>Encode <input type="radio" name="mode" value="encode" checked="checked">
   Decode <input type="radio" name="mode" value="decode">
   <input type="submit" />
</form>
</body>
</html>

python

#!/usr/bin/python

import os
import re
import cgi
import base64

if __name__ == '__main__':
    
    # initizlize variables
    form = cgi.FieldStorage()
    text = form.getvalue("before_field", "")
    mode = form.getvalue("mode", "decode")
    title = "Base64 encode/decode"
    self_url = "http://%s%s" % (os.environ.get("SERVER_NAME", "localhost"),
                                os.environ.get("SCRIPT_NAME", "/"))

    #### Main logic ####
    if len(text) > 0:
        if mode == "encode":
            after_field = base64.encodestring(text)
        elif mode == "decode":
            after_field = base64.decodestring(text)
        else:
            after_field = "Error: Wrong mode string!"
    else:
        after_field = ""

    # prepare param
    param = {}
    param["title"] = title
    param["action_url"] = self_url
    param["after_field"] = after_field
    param["before_field"] = text

    # escape param
    for key, value in param.items():
        param[key] = cgi.escape(value)
    
    # prepare template and set param
    output = """
        <html>
        <head>
        <title>%(title)s</title>
        <style type="text/css"><!--
        .field {width:600px; height:100px;}
        --></style>
        </head>
        <body>
        <form action="%(action_url)s" method="post">
        <p><textarea name="before_field"
                     class="field">%(before_field)s</textarea></p>
        <p><textarea name="after_field"
                     class="field">%(after_field)s</textarea></p>
        <p>
        Encode : 
        <input type="radio" name="mode" value="encode" checked="checked" />
        Decode :
        <input type="radio" name="mode" value="decode" />
        <input type="submit" />
        </p>
        </form>
        </body>
        </html>
    """ % param

    # strip indnet spaces
    re_indent = re.compile(r'^\s{,8}', re.MULTILINE)
    output = re_indent.sub("", output)

    # Output HTML
    print "Content-type: text/html"
    print
    print output
    

ruby

#!/usr/local/bin/ruby -Ku

require 'cgi'
require 'base64'

cgi = CGI.new('html3')

before_field = String.new
after_field = String.new

if cgi.has_key? 'before_field' and cgi['before_field'].length > 0 then
  before_field = cgi['before_field']
  
  if cgi.has_key? 'mode' then
    case cgi['mode']
    when 'encode' then after_field = encode64(before_field)
    when 'decode' then after_field = decode64(before_field)
    else
      # Error: wrong mode param.
    end
  end
end

cgi.out {
 cgi.html {
  cgi.head { cgi.title{'Base64 Encode/Decode'} } +
  cgi.body {
   cgi.form {
    cgi.p{ cgi.textarea('before_field', 80, 5){ CGI.escapeHTML before_field }} +
    cgi.p{ cgi.textarea('after_field', 80, 5){ CGI.escapeHTML after_field }} +
    cgi.p {
     'Encode' + cgi.radio_button('mode', 'encode', 'checked') +
     'Decode' + cgi.radio_button('mode', 'decode') +
     cgi.submit
    }
   }
  }
 }
}