2013-12-22 67 views
0

我是一位編程新手,需要PHP幫助。我有一個用於解密字符串的代碼。問題是它只顯示解密的第一個變量(decriptInfoDetails)。什麼需要改變,所以它也可以顯示函數中的第二個和第三個變量?例如,如果我有更多的變量來解密說10或更多,還有更好的方法嗎?我附上以下代碼以查看目的。希望有人能幫助這個新手,並提前致謝。PHP如何回顯多個輸出

<? 

     $privateKey = "xxxxx"; 
     $iv = "xxxxx"; 

     $keyPassword = "xxxxx"; 
     $ivPassword = "xxxxx";    

     function decriptInfoDetails($ciphertext_base64){ 
     global $privateKey,$iv; 

     $ciphertext_dec = trim(base64_decode($ciphertext_base64)); 

     $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, 
     $ciphertext_dec, MCRYPT_MODE_CBC, $iv);   

     return trim($plaintext_utf8_dec); 
     } 

     function encPass($ciphertext_base64){ 
     global $keyPassword,$ivPassword; 

     $ciphertext_dec = trim(base64_decode($ciphertext_base64)); 

     $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $keyPassword, 
     $ciphertext_dec, MCRYPT_MODE_CBC, $ivPassword); 

     return trim($plaintext_utf8_dec); 
     }   

     function encAndDecPass($data){ 
     global $keyPassword,$ivPassword; 

     $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $keyPassword, $data, MCRYPT_MODE_CBC, $ivPassword); 
     $ciphertext_base64 = trim(base64_encode($encrypted)); 

     return trim($ciphertext_base64); 
     } 

     echo decriptInfoDetails("D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=","ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1","zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1"); 

    ?> 

回答

1

將函數的參數更改爲數組。

<? 
error_reporting(E_ALL); 
$privateKey = "xxxxx"; 
$iv   = "xxxxx"; 

$keyPassword = "xxxxx"; 
$ivPassword = "xxxxx"; 

function decriptInfoDetails($ciphertext_base64_ar) 
{ 
    global $privateKey, $iv; 

    $result = array(); 

    foreach ($ciphertext_base64_ar as $ciphertext_base64) { 
     $ciphertext_dec = trim(base64_decode($ciphertext_base64)); 

     $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, 
     $ciphertext_dec, MCRYPT_MODE_CBC, $iv); 

     $result[]= trim($plaintext_utf8_dec); 
    } 

    return $result; 
} 
print_r(decriptInfoDetails(array(
    "D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=", 
    "ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1", 
    "zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1" 
))); 

?> 

注意:刪除了未使用的功能。

+0

我試過這個,但它只是顯示空白頁 – StrikeNeo

+0

@StrikeNeo修正了這個例子。 – Yousf

+0

這次它只顯示第一個變量,就像我的原始代碼。我如何顯示它的所有3個 – StrikeNeo

0

無法成爲這方面的專家,只是爲了幫助目的,我認爲在下面的代碼末尾,您在decriptInfoDetails函數中提供了太多的參數,而在代碼開始時,您聲明它獲取只有一個。你應該只給一個而不是三個。

echo decriptInfoDetails("D8D6OsHciT/rBfeNMGrwtQZegzDv02dTnotroNOe+Kk=","ojPlxa16CVhoLq8GP8d0h5l+Z+5GqFXSWXaX7GSXC/Q=1","zrJCOomZ4CJIeBTomAb9OGq2pQK17FBGqVNFdVawPB8=1"); 
+0

我怎樣才能讓它獲得3個參數? – StrikeNeo

+0

當你聲明你的函數時,你可以像這樣指定它:'function decriptInfoDetails($ var1,$ var2,$ var3){}' –