Home > Mobile >  How to mask or secure data-id(ID) in php
How to mask or secure data-id(ID) in php

Time:12-22

I am trying to secure or mask data-id="1" or ID in the URL, I want to use random numbers like this data-id= "518079"`.Though I found this method but getting the following error . Please help me to fix this or is there any else way. Please help

Error

Fatal error: Uncaught ArgumentCountError: Too few arguments to function createSecureId(), 1 passed in C:\xampp\htdocs\website\app\views\cl.php on line 70

Error line 70 is this one

<li ><a  href="javascript:void(0);"  data-id="' . createSecureId($value1->cat_id) . '"><span>' . $value1->category . '</i></a></li>';

This the way to encrypt and decrypt I found

$idsecret = "@/@#?2/32kjhasfdkjhsdfe982394895723489uSDIUPSDYsdfupydsfuisyf3";
$shuffleseq = Array("5","3","20","16","9","27","29","23","4","0","21","24","7","6","12","14","11","30","28","22","18","2","15","17","10","25","26","8","13","1","19","31");
function createSecureId($id,$subkey) {
    global $idsecret;
    $hex = sprintf("%x",shuffleId($id));
    $mac = md5($idsecret.$hex.$subkey.$idsecret);
    $mac = substr($mac,0,8);
    return $hex.$mac;
}

function readSecureId($str,$subkey) {
    global $idsecret;
    $l = strlen($str);
    if ($l <= 8) return -1;
    $hex = substr($str, 0, $l-8);
    $mac = substr($str, -8, 8);
    $vmac = md5($idsecret.$hex.$subkey.$idsecret);
    $vmac = substr($vmac,0,8);
    if ($mac !== $vmac) return -1;
    sscanf($hex, "%x", $id);
    return unshuffleId($id);
}

CodePudding user response:

    <?php // class file for encryption and decryption
class rc4crypt {
    function encrypt ($pwd, $data, $ispwdHex = 0)
    {
        if ($ispwdHex)
            $pwd = @pack('H*', $pwd); // valid input, please!

        $key[] = '';
        $box[] = '';
        $cipher = '';

        $pwd_length = strlen($pwd);
        $data_length = strlen($data);

        for ($i = 0; $i < 256; $i  )
        {
            $key[$i] = ord($pwd[$i % $pwd_length]);
            $box[$i] = $i;
        }
        for ($j = $i = 0; $i < 256; $i  )
        {
            $j = ($j   $box[$i]   $key[$i]) % 256;
            $tmp = $box[$i];
            $box[$i] = $box[$j];
            $box[$j] = $tmp;
        }
        for ($a = $j = $i = 0; $i < $data_length; $i  )
        {
            $a = ($a   1) % 256;
            $j = ($j   $box[$a]) % 256;
            $tmp = $box[$a];
            $box[$a] = $box[$j];
            $box[$j] = $tmp;
            $k = $box[(($box[$a]   $box[$j]) % 256)];
            $cipher .= chr(ord($data[$i]) ^ $k);
        }
        return $cipher;
    }
    
    function decrypt ($pwd, $data, $ispwdHex = 0)
    {
        return rc4crypt::encrypt($pwd, $data, $ispwdHex);
    }
}

// include the class file at the top of your page and call the encryption like below. The above class generates 256-bit key-based encryption, I used "securekey" string as a key.

<li ><a  href="javascript:void(0);"  data-id="' . urlencode(rc4crypt::encrypt("securekey",$value1->cat_id, 1)) . '"><span>' . $value1->category . '</i></a></li>';

// for decryption you can call it like the below line code.
$cat_id = rc4crypt::decrypt("securekey", urldecode($_GET['cat_id']), 1);
  • Related