How to use php to implement an 8-bit encryption and decryption function for digits

  1. encrypt numbers, such as 666, to get uiu2k1i2 and other similar characters without special symbols
  2. I tried to write, and I felt that my writing was rubbish. I hope some great god can give me excellent code
  3. example, url

clipboard.png


method 1:

$a = 666;
$key = "NWQTOwxUCU6FCbOsiod8Jasqw0GbuvoP";

function decrypt($string, $key) {
    $result = '';
    $string = base64_decode($string);
    for($i=0; $i<strlen($string); $iPP) {
        $char = substr($string, $i, 1);
        $keychar = substr($key, ($i % strlen($key))-1, 1);
        $char = chr(ord($char)-ord($keychar));
        $result.=$char;
    }
    return $result;
}

function encrypt($string, $key) {
    $result = '';
    for($i=0; $i<strlen($string); $iPP) {
        $char = substr($string, $i, 1);
        $keychar = substr($key, ($i % strlen($key))-1, 1);
        $char = chr(ord($char)+ord($keychar));
        $result.=$char;
    }
    return base64_encode($result);
}




$epy = encrypt($a, $key);

var_dump($epy);

var_dump(decrypt($epy, $key));

key can be changed at will

method 2:

function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
{
    $key = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $i = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    if ($passKey !== null) {
       $len = strlen($key); 

        $passhash = hash('sha256',$passKey);
        $passhash = (strlen($passhash) < $len)
            ? hash('sha512',$passKey)
            : $passhash;

        for ($n=0; $n < $len; $nPP) {
            $p[] = substr($passhash, $n ,1);
        }

        array_multisort($p, SORT_DESC, $i);
        $key = implode($i);
    }

    $base = strlen($key);

    if ($to_num) {
        $in = strrev($in);
        $out = 0;
        $len = strlen($in) - 1;
        for ($t = 0; $t <= $len; $tPP) {
            $bcpow = bcpow($base, $len - $t);
            $out = $out + strpos($key, substr($in, $t, 1)) * $bcpow;
        }

        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $out -= pow($base, $pad_up);
            }
        }
    } else {
        // Digital number -->> alphabet letter code
        if (is_numeric($pad_up)) {
            $pad_up--;
            if ($pad_up > 0) {
                $in += pow($base, $pad_up);
            }
        }

        $out = "";
        for ($t = floor(log10($in) / log10($base)); $t >= 0; $t--) {
            $a = floor($in / bcpow($base, $t));
            $out = $out . substr($key, $a, 1);
            $in = $in - ($a * bcpow($base, $t));
        }
        $out = strrev($out); // reverse
    }

    return $out;
}

$random_id=728559;
$encode=alphaID($random_id, false, 12);
$decode=alphaID($encode,true, 12);

echo "Encode : {$encode} <br> Decode : {$decode}";

if only lowercase, adjust $key = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' to remove uppercase letters.
this method refers to yutube link generation.


you can consider hashids/hashids . Of course, learning depends on its source code


Thank you very much for your comments
I have tried the hashids library
composer require hashids/hashids
after you get it, you can directly introduce the namespace

use Hashids\Hashids;
$hashObj = new Hashids('addhikashdaso2h91sdiy309', 10,'abcdefghijklmnopqrstuvwxyz123456789');
$hash_string = $hashObj ->encode($id);
$hashObj ->decode($hash_string);

those who are interested can continue to study in depth


irreversible encryption functions are: md5 (), crypt ()

reversible encryption: base64_encode (), urlencode ()
corresponding decryption functions: base64_decode (), urldecode ()

the landlord can use the function that comes with php

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4381a-43776.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4381a-43776.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?