Ein kleines PHP-Script, welches in der Linux-Konsole läuft und einen String nach base64 encodiert oder von base64 decodiert. Definiert sind hierbei short- und longoptions um festzulegen in welche Richtung das Script arbeiten soll.
In wie weit man das wirklich braucht, kann ich so nicht sagen, ich habs gerade benötigt, und damit ich, wenn ich sowas noch mal brauch nicht lange basteln muss, hier isses.
Aufruf:
1 | php base64.php |
base64.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php /** * PHP-Notice abschalten */ error_reporting(E_ALL ^ E_NOTICE); /** * Kurzoptionen festlegen * * : => requiered * :: => optional */ $shortopts = ""; $shortopts .= "e::"; // Encode (Optional value / -etest encodes "test") $shortopts .= "d::"; // Decode (Optional value / -dtest decodes "test") /** * Langoptionen festlegen * * : => requiered * :: => optional */ $longopts = array( "encode::", // Encode (Optional value / --encode=test encodes "test") "decode::", // Decode (Optional value / --decode=test decodes "test") ); $options = getopt($shortopts, $longopts); /** * Funktion zur Ueberpruefung ob ein String wirklich base64-encoded ist, oder nicht * * @param string $encodedString * @return bool */ function checkBase64Encoded($encodedString) { if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $encodedString)) { return true; } else { return false; } } if ($options['encode'] || $options['e']) { $encode = ($options['encode']) ? $options['encode'] : $options['e']; echo base64_encode($encode) . "\n"; } if ($options['decode'] || $options['d']) { $decode = ($options['decode']) ? $options['decode'] : $options['d']; if (checkBase64Encoded($decode)) { echo base64_decode($decode) . "\n"; } else { echo 'Dies ist kein Base64-String !!!' . "\n"; } } if (!$options) { echo 'Syntax:' . "\n\n"; echo 'php base64.php [--option] || [-o]' . "\n"; echo "\t" . 'Options:' . "\n"; echo "\t\t" . '-e:' . "\t\t" . 'encodes a string (-etest => encodes "test")' . "\n"; echo "\t\t" . '--encode:' . "\t" . 'encodes a string (--encode=test => encodes "test")' . "\n\n"; echo "\t\t" . '-d:' . "\t\t" . 'decodes a string (-dtest => decodes "test")' . "\n"; echo "\t\t" . '--decode:' . "\t" . 'decodes a string (--decode=test => decodes "test")' . "\n\n"; } ?> |






Hm, das kann man sicher mit PHP machen. Ich hab das bisher immer mit Perl gemacht:
2
3
4
5
VGVzdHRleHQ=
frank@laptop:~$ perl -MMIME::Base64 -e'print decode_base64(q[VGVzdHRleHQ=]) . qq[\n]';
Testtext
Holla Frank,
Danke für die Ergänzung.
Ja mit Perl ist dies natürlich auch möglich, aber ich stehe irgendwie auf Kriegsfuss mit Perl, das will sich aus irgendeinem Grunde nicht so richtig mit mir anfreunden