简体   繁体   English

将变量传递给php7中的类

[英]pass variable to class in php7

I have code to run encryption and decryption, I wanna pass value from my mysql database, but it won't work 我有运行加密和解密的代码,我想从mysql数据库中传递值,但无法正常工作

this is my code 这是我的代码

    <?php
    require_once('koneksi.php');
    $sql = "SELECT * from keamanan WHERE id='1'";
    $r = mysqli_query($con,$sql);

        //Membuat Array Kosong
        $result = array();

        while($row = mysqli_fetch_array($r)){

                //Memasukkan Nama dan ID kedalam Array Kosong yang telah dibuat
                array_push($result,array(
                        "iv"=>$row['kunci_pribadi'],
                        "key"=>$row['kunci_bersama'],
                        "token1"=>$row['token_lampu1'],
                        "token2"=>$row['token_lampu2'],
                        "token2"=>$row['token_lampu3'],
                        "token2"=>$row['token_lampu4'],

                ));
        }

        $json = json_encode(array('result'=>$result));
        $data = json_decode($json);


class MCrypt

{
    $iv;
    $key;


    function __construct()
    {
        $this->iv = $key;
        $this->key = $iv;
    }
    function encrypt($str) {

      //$key = $this->hex2bin($key);
      $iv = $this->iv;

      $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

      mcrypt_generic_init($td, $this->key, $iv);
      $encrypted = mcrypt_generic($td, $str);

      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);

      return bin2hex($encrypted);
    }

    function decrypt($code) {
      //$key = $this->hex2bin($key);
      $code = $this->hex2bin($code);
      $iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

      mcrypt_generic_init($td, $this->key, $iv);
      $decrypted = mdecrypt_generic($td, $code);

      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);

      return utf8_encode(trim($decrypted));
    }

    protected function hex2bin($hexdata) {
      $bindata = '';

      for ($i = 0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
      }

      return $bindata;
    }
}

$mcrypt = new MCrypt($data->result[0]->key,$data->result[0]->iv);
#Encrypt
$encrypted = '9975e28df055c336a9b7090b03f88689';
#Decrypt
$decrypted = $mcrypt->decrypt($encrypted);
var_dump($encrypted);
var_dump($decrypted);

?>

where I miss? 我想念哪里? I don't know my script it still can be used in php7 or not. 我不知道我的脚本是否仍可以在php7中使用。 I tried to configure what the mistake. 我试图配置什么错误。 if I write it manually the code runs fine nothing an error, but why I can't pass the value variable from my database mysql. 如果我手动编写代码,则代码不会运行任何错误,但是为什么我不能从数据库mysql中传递value变量。 can anyone help me? 谁能帮我?

The constructor of your class don't have entry parameters whereas you call it with parameters here 您的类的构造函数没有输入参数,而在此处使用参数进行调用

new MCrypt($data->result[0]->key,$data->result[0]->iv);

Change your __construct function to 将__construct函数更改为

function __construct($key, $iv)
{
    $this->iv = $key;
    $this->key = $iv;
}

Moreover, I suggest you to use PDO for database connection, which is considered as more secure. 此外,我建议您使用PDO进行数据库连接,这被认为更安全。 Maybe you could have a look at this https://phpdelusions.net/pdo 也许您可以看看这个https://phpdelusions.net/pdo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM