鍍金池/ 問答/PHP  Linux/ 如何使用php實現(xiàn)一個對數(shù)字進行8位的加密解密函數(shù)

如何使用php實現(xiàn)一個對數(shù)字進行8位的加密解密函數(shù)

  1. 對數(shù)字進行加密,比如對666進行加密,得到uiu2k1i2等類似的無特殊符號的字符
  2. 我嘗試寫了寫,感覺自己寫的很垃圾,希望有大神能給出優(yōu)秀的代碼
  3. 示例,簡書url

clipboard.png

回答
編輯回答
傻叼

不可逆的加密函數(shù)為:md5()、crypt()

可逆轉(zhuǎn)的加密為:base64_encode()、urlencode()
相對應的解密函數(shù):base64_decode() 、urldecode()

樓主可使用php自帶的函數(shù)呀

2017年10月2日 01:37
編輯回答
幼梔

可以考慮hashids/hashids, 學習呢當然就看它源碼了

2017年4月27日 18:07
編輯回答
失心人

方式一:

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

function decrypt($string, $key) {
    $result = '';
    $string = base64_decode($string);
    for($i=0; $i<strlen($string); $i++) {
        $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); $i++) {
        $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 可以隨意更改

方式二:

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; $n++) {
            $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; $t++) {
            $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}";

如果單純的只要小寫部分,直接調(diào)整$key = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'移除大寫字母。
該方法參考yutube 鏈接生成.

2018年4月24日 13:48
編輯回答
假灑脫

非常感謝大家的意見
我嘗試了一下hashids庫
composer require hashids/hashids
大家獲取后可以直接引入命名空間

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

有興趣的可以繼續(xù)深入學習

2017年7月20日 02:22