读取时字符串为空

时间:2017-02-24 22:21:27

标签: php mysql function

所以基本上我调用一个MYSQL查询来获取生成并保存到数据库的密钥。然后它将一个名为“ServerCryptKey”的字符串设置为查询的输出,该输出应为“yQnK9xDCxaLIGwzpsKEXeJR2Iz5ZoHnWyLHHikkQv5zbC8B5Sf36ZU9HteHSW5Ov”。

$SQLGetUsers = $odb -> query("SELECT * FROM settings");
while ($getInfo = $SQLGetUsers -> fetch(PDO::FETCH_ASSOC))
{
    $NewServerHash = $getInfo['newhash'];
}   

我可以调用字符串,它会读取上面的^,但是只要我有它从这个函数调用它它什么也没读,并显示它是一个空字符串......

function encrypt($data)
{
    $secret = $ServerCryptKey;
    //Generate a key from a hash
    $key = md5(utf8_encode($secret), true);

    //Take first 8 bytes of $key and append them to the end of $key.
    $key .= substr($key, 0, 8);

    //Pad for PKCS7
    $blockSize = mcrypt_get_block_size('tripledes', 'ecb');
    $len = strlen($data);
    $pad = $blockSize - ($len % $blockSize);
    $data .= str_repeat(chr($pad), $pad);

    //Encrypt data
    $encData = mcrypt_encrypt('tripledes', $key, $data, 'ecb');

    return base64_encode($encData);
}

function decrypt($data)
{
    $secret = $ServerCryptKey;
    //Generate a key from a hash
    $key = md5(utf8_encode($secret), true);

    //Take first 8 bytes of $key and append them to the end of $key.
    $key .= substr($key, 0, 8);

    $data = base64_decode($data);

    $data = mcrypt_decrypt('tripledes', $key, $data, 'ecb');

    $block = mcrypt_get_block_size('tripledes', 'ecb');
    $len = strlen($data);
    $pad = ord($data[$len-1]);

    return substr($data, 0, strlen($data) - $pad);
}

如果有人可以帮我弄清楚为什么不抓住字符串,请告诉我,谢谢!

0 个答案:

没有答案