PHP常见的几种加密方式

cfn
2021-01-07
436

password_hash 函数

password_hash 用于创建密码的散列

echo password_hash("123456", PASSWORD_DEFAULT); # $2y$10$UDsDSviXPHSYFryVTbX0OuXNYKxr9xeRuwqy2p7OfXjxh95JBL/LW

校验密码

$password = "$2y$10$8Bs9WdtSwVGuIsEdiEWqj.5YOsU1bmEDmbx3DjfPgoIdmRcQD6p66"; var_dump(password_verify("123456",$password)); # bool(true)

md5 函数

md5函数用于计算字符串的MD5散列

echo md5("123456"); # e10adc3949ba59abbe56e057f20f883e

md5_file函数可对文件进行MD5散列计算

base64 加解密

base64_encode和base64_decode函数可对数据进行编码,结果为字符串,失败时返回false

# base64_encode 加密 echo base64_encode("123456"); # MTIzNDU2 # base64_encode 解密 echo base64_decode("MTIzNDU2"); # 123456

urlencode与urldecode

urlencode和urldecode可对URL进行编码和解码

echo urlencode("https://www.baidu.com/?a=1"); # https%3A%2F%2Fwww.baidu.com%2F%3Fa%3D1 echo urldecode("https%3A%2F%2Fwww.baidu.com%2F%3Fa%3D1"); # https://www.baidu.com/?a=1

3DES加密解密

$key = "abcdefg"; echo base64_encode(openssl_encrypt("1234567890", 'des-ede3', $key, OPENSSL_RAW_DATA)); # hgK3i8hbUeW+jrPtHBcWsQ== echo openssl_decrypt(base64_decode("hgK3i8hbUeW+jrPtHBcWsQ=="), 'des-ede3', $key, OPENSSL_RAW_DATA); # 1234567890
评论 1