note 78765 added to function.base64-encode
| From: | web at pkasperski dot com | Date: | Fri, 26 Oct 2007 14:08:49 +0000 |
| Subject: | note 78765 added to function.base64-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131749@lists.php.net to get a copy of this message | ||
I have implemented the base64_encode() function with some custom mapping table so I could encode
some binary data more securely without anyone knowing how to decode it
here is the class
<?php
class Base64 {
/**
* I have changed letter placement (P <=> x, S <=> 9) and the cases
* You can completely redo the mapping table
*/
private static $BinaryMap = array(
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', // 7
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'x', // 15
'q', 'r', '9', 't', 'u', 'v',
'w', 'x', // 23
'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', // 31
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', // 39
'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', // 47
'W', 'P', 'Y', 'Z', '0', '1',
'2', '3', // 55
'4', '5', '6', '7', '8', 'S',
'+', '/', // 63
'=', // padding char
);
public function __construct() {}
public function base64_encode($input) {
$output = "";
$chr1 = $chr2 = $chr3 = $enc1 = $enc2 = $enc3 = $enc4 = null;
$i = 0;
// $input = self::utf8_encode($input);
while($i < strlen($input)) {
$chr1 = ord($input[$i++]);
$chr2 = ord($input[$i++]);
$chr3 = ord($input[$i++]);
$enc1 = $chr1 >> 2;
$enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4);
$enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);
$enc4 = $chr3 & 63;
if (is_nan($chr2)) {
$enc3 = $enc4 = 64;
} else if (is_nan($chr3)) {
$enc4 = 64;
}
$output .= self::$BinaryMap[$enc1]
. self::$BinaryMap[$enc2]
. self::$BinaryMap[$enc3]
. self::$BinaryMap[$enc4];
}
return $output;
}
public function utf8_encode($input) {
$utftext = null;
for ($n = 0; $n < strlen($input); $n++) {
$c = ord($input[$n]);
if ($c < 128) {
$utftext .= chr($c);
} else if (($c > 128) && ($c < 2048)) {
$utftext .= chr(($c >> 6) | 192);
$utftext .= chr(($c & 63) | 128);
} else {
$utftext .= chr(($c >> 12) | 224);
$utftext .= chr((($c & 6) & 63) | 128);
$utftext .= chr(($c & 63) | 128);
}
}
return $utftext;
}
}
?>
and the usage as follows:
<?php
$string = pack('H*', "31c85c5aaa56c1f0102301ea497d0ab010e4e131af261787"); // HEX
to binary
echo Base64::base64_encode($string);
echo "<br />";
echo base64_encode($string);
?>
and the output will be:
mCHCwQPwWFaqiWhQ9x0kSbdK4tgVjHEh // with custom mapping
MchcWqpWwfAQIwHqSX0KsBDk4TGvJheH // the base64_encode()
----
Server IP: 212.108.64.191
Probable Submitter: 193.95.179.200
----
Manual Page -- http://www.php.net/manual/en/function.base64-encode.php
Edit -- https://master.php.net/note/edit/78765
Del: integrated -- https://master.php.net/note/delete/78765/integrated
Del: useless -- https://master.php.net/note/delete/78765/useless
Del: bad code -- https://master.php.net/note/delete/78765/bad+code
Del: spam -- https://master.php.net/note/delete/78765/spam
Del: non-english -- https://master.php.net/note/delete/78765/non-english
Del: in docs -- https://master.php.net/note/delete/78765/in+docs
Del: other reasons-- https://master.php.net/note/delete/78765
Reject -- https://master.php.net/note/reject/78765
Search -- https://master.php.net/manage/user-notes.php