PHP 加密解密和解决URL传输问题
程序员文章站
2022-06-22 16:17:42
...
base64 加密和解密
<meta charset="utf-8"> <?php $str = "http://www.onestopweb.cn"; $en=base64_encode($str); $de=base64_decode($en); echo "原型:$str<br>"; echo "加密后:$en<br>"; echo "解密后:$de<br>"; ?>
效果图:
解决URL传输的问题
<meta charset="utf-8"> <?php //urlencode 将字符串以 URL 编码。 $str = "http://www.onestopweb.cn"; $urlen = urlencode($str); $urlde = urldecode($urlen); //URL传输请使用下面这种 //rawurlencode 将字符串编码成 URL 专用格式。 $rawurlen = rawurlencode($str); $rawurlde = rawurldecode($rawurlen); echo "原型:$str<br>"; echo "urlencode加密后:$urlen<br>"; echo "urldecode解密后:$urlde<br>"; echo "rawurlencode加密后:$rawurlen<br>"; echo "rawurldecode解密后:$rawurlde<br>"; ?>
效果图:
有点复杂的加密解密和解决URL传输问题
<meta charset="utf-8"> <?php function encrypt($data, $key) { $prep_code = serialize($data); $block = mcrypt_get_block_size('des', 'ecb'); if (($pad = $block - (strlen($prep_code) % $block)) < $block) { $prep_code .= str_repeat(chr($pad), $pad); } $encrypt = mcrypt_encrypt(MCRYPT_DES, $key, $prep_code, MCRYPT_MODE_ECB); return base64_encode($encrypt); } function decrypt($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]); if ($pad && $pad < $block && preg_match('/' . chr($pad) . '{' . $pad . '}$/', $str)) { $str = substr($str, 0, strlen($str) - $pad); } return unserialize($str); } $key = 'login'; $data = array('id' => 100, 'username' => 'chaoyi', 'password' => '123456'); $snarr = serialize($data); $en = encrypt($data, $key); $de = decrypt($en, $key); echo "加密原型:"; print_r($data); echo "<br>密钥:$key <br> 加密结果:$en <br> 解密结果:";print_r($de); $en2 = rawurlencode(encrypt($data, $key)); $de2 = decrypt(rawurldecode($en2), $key); echo "<br>可以进行URL传输的加密:".$en2."<br>"; echo "转输后进行解密:";print_r($de2); ?>
效果图:
上一篇: 加密解密帮助类
推荐阅读