在用户定义的函数中使用AES加密

时间:2016-04-30 12:11:33

标签: php aes

我需要在用户定义的函数中使用AES加密工具,以便在文件中多次访问它。

$sms = 'UF4niWyCEYBTEF2ELU+B2yBZT+ivehO+VXtDoalPPqg=';

decode($sms);    //the function below
$dec = decode($sms);
//echo "After encryption: ".$enc."<br/>";
echo "After decryption: ".$dec."<br/>";

function decode($sms) {
include 'AES.php';  //this is my AES file copied from the AES website
$inputText = $sms;
$inputKey = '_______'; //this is the underscore key pressed 7 times.
$blockSize = 256;
$aes = new AES($inputText, $inputKey, $blockSize);
$enc = $aes->encrypt();
$aes->setData($enc);
$dec=$aes->decrypt();

return $dec;
}

我尝试过使用上面的代码进行各种尝试并在函数内搜索函数但是我没有解析AES部分是没有错误的。我通常收到一封'无法重新声明/ home / myfile中的AES类'

1 个答案:

答案 0 :(得分:0)

从PHP Cannot redeclare class AES in /home/myfile获得的错误是正确的:通过在函数内部使用include,PHP将在每次调用函数时尝试包含文件。 / p>

在下面的代码中,'include'语句已被移出函数之外:

$sms = 'UF4niWyCEYBTEF2ELU+B2yBZT+ivehO+VXtDoalPPqg=';

decode($sms);    //the function below
$dec = decode($sms);
//echo "After encryption: ".$enc."<br/>";
echo "After decryption: ".$dec."<br/>";

include 'AES.php';  //this is my AES file copied from the AES website
function decode($sms) {
    $inputText = $sms;
    $inputKey = '_______'; //this is the underscore key pressed 7 times.
    $blockSize = 256;
    $aes = new AES($inputText, $inputKey, $blockSize);
    $enc = $aes->encrypt();
    $aes->setData($enc);
    $dec=$aes->decrypt();

    return $dec;
}

这可以解决您提到的错误。