2012-04-19 22 views
5

我得到以下警告,當我嘗試使用BASE64_DECODE()函數base64_ *功能已禁用,現在要做什麼?

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

看起來像我的主機已禁用base64_ *功能。

我有幾個問題

  1. 我覺得base64_ *功能可以在默認情況下在PHP中啓用,是否正確?
  2. 是否有任何安全原因,爲什麼base64_ *函數未啓用?任何安全漏洞?
  3. base64_ *函數的替代方法,默認情況下可用?
  4. 我在哪裏可以找到base64_ *實現的自定義類/函數,以便我可以將它們放在我的PHP文件中,並在PHP的base64_ *函數不可用時使用它們?

幫助讚賞。

+0

配發的base_64編碼成的eval函數。如果你真的需要它,請更換主機 – 2012-04-19 17:39:54

+4

如果你的主機相信「偏執狂的安全性」......改變主機。 – 2012-04-19 17:40:57

+0

是他們的間諜軟件不base64兼容? :) – Hajo 2012-04-19 17:49:04

回答

-2

這是託管問題。

請改變你的主機或要求他們刪除此安全問題

7

我寫的替代功能,爲所有的base64目的。

看看這裏:

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); 
} 

但是不要忘了charCodeAtcharAt功能。它們都是base64_encode所必需的。 這兩個函數base64_encodebase64_decode都像內置的PHP函數一樣工作。但只有使用它們,如果內置的不可用,因爲它們不如內置的那樣快。

希望它有幫助!

+0

我愛你!這是你想要在緊急情況下找到的答案,無需編碼= P – 2017-01-04 17:21:07

+1

Thanks @Ivan Shatsky'return($ r?substr($ enc,0,($ r - 3))。substr('== =',$ r):$ enc);' – jankal 2017-01-04 17:47:48

+0

如果您需要模仿_base64_decode_的確切行爲,就像我一樣,您必須「返回FALSE如果輸入包含來自base64字母之外的字符。」([PHP手動](https://secure.php.net/manual/en/function.base64-decode.php#refsect1-function.base64-decode-parameters)說)。爲了做到這一點,我將$ input名稱更改爲$ filter(保留參數的原始值),然後添加了以下行:'if($ filter!= $ input)return false;'。 – 2017-01-06 06:14:32

1

剛剛註冊,以評論jankal的答案,但不能沒有聲譽做它。

在jankal的回答功能BASE64_ENCODE代碼最後一行是

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

其中$ r可以爲0,1或2。據我所知,($ R || 3)通過代碼邏輯表達式的值必須當$ r = 0時爲3,而在其他兩種情況下爲$ r,但實際上(PHP 5.6/7.0)該表達式的值始終等於1,所以我們的BASE64編碼的字符串總是以兩個'='符號結尾,即絕對錯誤。

我的解決辦法:討厭的腳本

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

非常感謝! – jankal 2017-01-04 17:46:22

+1

我可以在編輯時在我的答案中使用您的更改嗎? – jankal 2017-01-04 17:46:49

+2

我剛剛做到了...如果有任何投訴:請發表評論或給我發消息。 – jankal 2017-01-04 17:49:04

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)); 
    }