Để mã hóa dữ liệu dạng base32_encode, base32_decode bạn dùng code dưới để mã hóa và giải mã dữ liệu PHP cấp thấp. Cách 1: Để mã hóa bạn dùng code sau: Mã: function base32_encode($d) { list($t, $b, $r) = array("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", "", ""); foreach(str_split($d) as $c) $b = $b . sprintf("%08b", ord($c)); foreach(str_split($b, 5) as $c) $r = $r . $t[bindec($c)]; return($r); } Để giải mã bạn dùng code sau: Mã: function base32_decode($d) { list($t, $b, $r) = array("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", "", ""); foreach(str_split($d) as $c) $b = $b . sprintf("%05b", strpos($t, $c)); foreach(str_split($b, 8) as $c) $r = $r . chr(bindec($c)); return($r); } Cách 2: Để mã hóa bạn dùng code sau: Mã: function webigin2_base32_encode($str) { $BASE32_TABLE = '0123456789bcdfghjklmnpqrstuvwxyz'; $out = ''; $i = $v = $bits = 0; $str_len = strlen($str); while ($i < $str_len) { $v |= ord($str[$i++]) << $bits; $bits += 8; while ($bits >= 5) { $out .= $BASE32_TABLE[$v & 31]; $bits -= 5; $v >>= 5; } } if ($bits > 0) { $out .= $BASE32_TABLE[$v & 31]; } return $out; } Để giải mã bạn dùng code sau: Mã: function webigin2_base32_decode($str, $extrabits = 0) { $BASE32_TABLE = '0123456789bcdfghjklmnpqrstuvwxyz'; $str_len = strlen($str); $out = ''; $i = $v = $vbits = 0; while ($i < $str_len) { if (($x = strpos($BASE32_TABLE, $str[$i++])) == FALSE) { return FALSE; } $v |= $x << $vbits; $vbits += 5; if ($vbits >= 8) { $out .= chr($v); $v >>= 8; $vbits -= 8; } } $vbits += $extrabits; while ($vbits >= 8) { $out .= chr($v); $v >>= 8; $vbits -= 8; } return $out; }