thinkphp
PHP实现DES加密、解密
2022-07-11
    /**
     * des 加密
     * str 需要加密的账号
     * key 加密秘钥
     */
    function encryptStr($str, $key, $toBase64 = true){
        $block = mcrypt_get_block_size('des', 'ecb');
        $pad = $block - (strlen($str) % $block);
        $str .= str_repeat(chr($pad), $pad);
        $enc_str = mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
        return (true == $toBase64) ? base64_encode($enc_str) : $enc_str;
    }
    /**des解密
     * @param $str
     * @param $key
     * @return mixed
     */
    function decryptStr($str, $key){
        $str = base64_decode($str);
        $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
        $block = mcrypt_get_block_size('des', 'ecb');
        $pad = ord($str[($len = strlen($str)) - 1]);
        $res = urldecode(substr($str, 0, strlen($str) - $pad));
        return json_decode(urldecode($res),true);
 
    }


文章由鸿邑科技成都网站建设编辑整理,转载请注明出处!

0
分享至: