2012-04-19 11 views
5

función() Me estoy haciendo siguiente advertencia cuando trato de usar base64_decodebase64_ * funciones deshabilitadas, ¿qué hacer ahora?

"Warning: base64_decode() has been disabled for security reasons" 

Parece que mi anfitrión tiene discapacidad base64_ * funciones.

tengo algunas preguntas

  1. Creo funciones base64_ * pueden ser activadas por defecto en php, ¿correcto?
  2. ¿Hay alguna razón de seguridad por la cual las funciones de base64_ * no están habilitadas? ¿Alguna violación de seguridad?
  3. ¿Alternativa a las funciones base64_ *, que están disponibles por defecto?
  4. ¿Dónde puedo encontrar clases/funciones personalizadas para la implementación de base64_ *, para que pueda tenerlas en mi archivo PHP y usarlas si las funciones base64_ * de PHP no están disponibles?

Ayuda apreciada.

+0

adjudicar de guiones desagradables se codifican en base_64 funciones eval. Si realmente lo necesita, cambie de host –

+4

Si su host cree en "seguridad por paranoia" ... CAMBIAR HOSTS. –

+0

¿su software espía no es compatible con base64? :) – Hajo

Respuesta

-2

Este es el problema de alojamiento.

Cambie sus hosting o pedirle que elimine este problema de seguridad funciones alternativas

7

he escrito a todos los efectos base64.

vistazo aquí:

function base64_decode($input) { 
    $keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 
    $chr1 = $chr2 = $chr3 = ""; 
    $enc1 = $enc2 = $enc3 = $enc4 = ""; 
    $i = 0; 
    $output = ""; 

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or = 
    $filter = $input; 
    $input = preg_replace("[^A-Za-z0-9\+\/\=]", "", $input); 
    if ($filter != $input) { 
     return false; 
    } 

    do { 
     $enc1 = strpos($keyStr, substr($input, $i++, 1)); 
     $enc2 = strpos($keyStr, substr($input, $i++, 1)); 
     $enc3 = strpos($keyStr, substr($input, $i++, 1)); 
     $enc4 = strpos($keyStr, substr($input, $i++, 1)); 
     $chr1 = ($enc1 << 2) | ($enc2 >> 4); 
     $chr2 = (($enc2 & 15) << 4) | ($enc3 >> 2); 
     $chr3 = (($enc3 & 3) << 6) | $enc4; 
     $output = $output . chr((int) $chr1); 
     if ($enc3 != 64) { 
      $output = $output . chr((int) $chr2); 
     } 
     if ($enc4 != 64) { 
      $output = $output . chr((int) $chr3); 
     } 
     $chr1 = $chr2 = $chr3 = ""; 
     $enc1 = $enc2 = $enc3 = $enc4 = ""; 
    } while ($i < strlen($input)); 
    return urldecode($output); 
} 

function base64_encode($data) { 
    $b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/='; 
    $o1 = $o2 = $o3 = $h1 = $h2 = $h3 = $h4 = $bits = $i = 0; 
    $ac = 0; 
    $enc = ''; 
    $tmp_arr = array(); 
    if (!$data) { 
     return data; 
    } 
    do { 
    // pack three octets into four hexets 
    $o1 = charCodeAt($data, $i++); 
    $o2 = charCodeAt($data, $i++); 
    $o3 = charCodeAt($data, $i++); 
    $bits = $o1 << 16 | $o2 << 8 | $o3; 
    $h1 = $bits >> 18 & 0x3f; 
    $h2 = $bits >> 12 & 0x3f; 
    $h3 = $bits >> 6 & 0x3f; 
    $h4 = $bits & 0x3f; 
    // use hexets to index into b64, and append result to encoded string 
    $tmp_arr[$ac++] = charAt($b64, $h1).charAt($b64, $h2).charAt($b64, $h3).charAt($b64, $h4); 
    } while ($i < strlen($data)); 
    $enc = implode($tmp_arr, ''); 
    $r = (strlen($data) % 3); 
    return ($r ? substr($enc, 0, ($r - 3)) . substr('===', $r) : $enc); 
} 

function charCodeAt($data, $char) { 
    return ord(substr($data, $char, 1)); 
} 

function charAt($data, $char) { 
    return substr($data, $char, 1); 
} 

pero no se olvide el charCodeAt y la función charAt. Ambos son necesarios para base64_encode. Las dos funciones base64_encode y base64_decode ambas funcionan como las funciones PHP incorporadas. Pero solo utilícelos, si los incorporados no están disponibles porque no son tan rápidos como los incorporados.

Espero que ayude!

+0

¡Te amo! Esta es la respuesta que desea encontrar en una emergencia donde no es momento de codificar = P –

+1

Gracias @Ivan Shatsky 'return ($ r? Substr ($ enc, 0, ($ r - 3)). Substr ('== = ', $ r): $ enc); ' – jankal

+0

Si necesita imitar el comportamiento exacto de _base64_decode_, como yo lo hago, debe" 'Devolver FALSE si la entrada contiene caracteres fuera del alfabeto base64." (como [PHP Manual] (https://secure.php.net/manual/en/function.base64-decode.php#refsect1-function.base64-decode-parameters) dice). Para hacer eso, cambié el $ input name a $ filter (preservando el valor original del parámetro) y luego agregué la línea: 'if ($ filter! = $ Input) return false;'. –

1

Acaba de registrarse para comentar la respuesta de jankal, pero no puede hacerlo sin reputación.

última línea del código de función base64_encode en la respuesta de jankal es

return ($r ? substr($enc, 0, ($r - 3)) : $enc) . substr('===', ($r || 3)); 

donde $ r puede ser 0, 1 ó 2. Según entiendo, ($ r || 3) valor de expresión por la lógica de código debe haber 3 cuando $ r = 0, y $ r en otros dos casos, pero en la práctica (PHP 5.6/7.0) el valor de esta expresión siempre es igual a 1, por lo que nuestra cadena codificada en BASE64 siempre termina con dos símbolos '=', que es Definitivamente mal.

Mi solución:

return ($r ? substr($enc, 0, ($r - 3)) . substr('===', $r) : $enc); 
+1

¡Muchas gracias! – jankal

+1

¿Puedo usar los cambios en mi respuesta cuando edito? – jankal

+2

Lo acabo de hacer ... si tiene alguna queja: por favor comenten o envíenme un mensaje. – jankal

0
function myencode($input) 
    { 
    $CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

    // the result/encoded string, the padding string, and the pad count 
    $r = ""; 
    $p= ""; 
    $c = strlen($input) % 3; 

    // add a right zero pad to make this string a multiple of 3 characters 
    if ($c > 0) 
    { 
     for (; $c < 3; $c++) 
    { 
     $p .= "="; 
     $input .= "\0"; 
    } 
    } 

    // increment over the length of the string, three characters at a time 
    for ($c = 0; $c < strlen($input); $c += 3) { 

// we add newlines after every 76 output characters, according to the MIME specs 

     if ($c > 0 && ($c/3 * 4) % 76 == 0) 
     $r += "\r\n"; 

// these three 8-bit (ASCII) characters become one 24-bit number 
$n = (ord($input[$c]) << 16) + (ord($input[$c +1]) << 8) + (ord($input[$c +2])); 


     // this 24-bit number gets separated into four 6-bit numbers 
     $n1 = $n >> 18 & 63; 
     $n2 = $n >> 12 & 63; 
     $n3 = $n >> 6 & 63; 
     $n4 = $n & 63; 

// those four 6-bit numbers are used as indices into the base64 character list 

$r .= "" . $CODES[$n1] . $CODES[$n2] . $CODES[$n3] . $CODES[$n4]; 

    } 
    return substr($r,0,(strlen($r)-strlen($p))) . $p; 
    } 



function mydecode($input) { 

$CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

// remove/ignore any characters not in the base64 characters list 
    // or the pad character -- particularly newlines 

    $input= str_replace("[^" . $CODES . "=]","",$input); 

    // replace any incoming padding with a zero pad (the 'A' character is zero) 
    $p = ($input[strlen($input) - 1] == '=' ?($input[strlen($input) - 2] == '=' ? "AA" : "A") : ""); 

    $r = ""; 
    $input = substr($input,0,(strlen($input) - strlen($p))) . $p; 
// increment over the length of this encoded string, four characters at a time 
    for ($c = 0; $c < strlen($input); $c += 4) { 

// each of these four characters represents a 6-bit index in the 
// base64 characters list which, when concatenated, will give the 
// 24-bit number for the original 3 characters 

     $n=(strpos($CODES,$input[$c]) << 18) 
     + (strpos($CODES,$input[$c+1]) << 12) 
     + (strpos($CODES,$input[$c+2]) << 6) 
     + (strpos($CODES,$input[$c+3])); 


// split the 24-bit number into the original three 8-bit (ASCII) characters 
$r .= "" . chr(($n >> 16) & 0xFF) . chr((($n >> 8) & 0xFF)) . chr(($n & 0xFF)); 

    } 
    // remove any zero pad that was added to make this a multiple of 24 bits 
     return substr($r,0, strlen($r)- strlen($p)); 
    } 
Cuestiones relacionadas