tux402 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Perl Monks, I am taking in a string of a mixture of Base64 and normal characters. I'm not entirely sure how to decode it under Perl.
An example of an encoded string would be
Georgia
or
Toni
The problem is that these strings will decode to a name, but some of the characters are randomly not encoded into base64. How would I go about decoding the string, and leave the non base64 characters alone? I will be taking in random names, so the length, and which characters are encoded will
vary.
Re: Base64 Decode issues
by zwon (Abbot) on Dec 21, 2008 at 22:28 UTC
|
It's not Base64, it's HTML encoding, you can use HTML::Entities to decode this stuff:
use HTML::Entities;
my $line="Toni";
print decode_entities($line);
| [reply] [d/l] |
|
| [reply] |
Re: Base64 Decode issues
by zentara (Archbishop) on Dec 21, 2008 at 22:25 UTC
|
If you have random non-base64 chars in the string, maybe it isn't base64encoded? Or else it is a clever attempt at encryption by obfuscation? Anyways, this will do it.
#!/usr/bin/perl
use warnings;
use strict;
my $str = 'Toni';
print "$str\n";
$str =~ tr#A-Za-z0-9+/##cd; # remove non-base64 chars
print "$str\n";
exit;
| [reply] [d/l] |
Re: Base64 Decode issues
by pKai (Priest) on Dec 21, 2008 at 22:31 UTC
|
Locking at your example strings, I think you are wrong when you speak of "base64".
The encoded characters in your strings are HTML::Entities and you can decode them like this:
C:\>perl -MHTML::Entities -pe "$_=decode_entities($_)"
Georgia
Georgia
Toni
Toni
^Z
| [reply] [d/l] |
|
You are right, I thought it was base64 because I was decoding it using this
http://www.paulschou.com/tools/xlate/
I guess I didn't recognize the encoding.
| [reply] |
|