加密处理了甚么成绩:
1.避免通讯内容被窃听;
2.避免通讯内容被窜改
加密类型:
1.对称加密:加密与解密应用的是同一个秘钥,例:DES(Data Encryption Standard),1977-1999年,1999年被破解;AES(Advance Encryption Standard),今朝最盛行的对称加密算法
2.非对称加密:RSA
AES 加密 / 解密
1.应用PHP加密/解密函数 openssl_encrypt/openssl_decrypt
留意:有不少例子应用的是PHP mcrypt_encrypt() 函数,官网给我的诠释:This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.
//猎取可用的明码加密算法列表 //$methods = openssl_get_cipher_methods(); //var_dump($methods); # AES加密演示 //明文(要加密的内容) $str = "这是测试用例 我是明文"; //秘钥(用例:应用uniqid()函数天生了一个惟一ID) $key = "5d3fb4acb2292"; //加密算法 $method = "AES-128-CBC"; //加密向量(要求18个字节) $iv = "1234567812345678"; $encrypt_str = openssl_encrypt($str, $method, $key, 0, $iv); var_dump("AES加密后果:".$encrypt_str); # AES解密演示 //$encrypt_str AES加密后孕育发生的密文 //$key 秘钥(同上) $decrypt_str = openssl_decrypt($encrypt_str, $method, $key, 0, $iv); var_dump("AES解密后果:".$decrypt_str);
RSA 加密
1.公/私钥加密算法,属于非对称加密:
2.优点:极难被破解;
3.缺陷:速率慢,运算次数多,没有适宜加密长文本;
//公钥(名目中可正在线消费亦可本人天生) $PUBLIC_KEY = "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApJJ7D/U9lHLNQdl4LZSr jNvdCelIraMnSD/iujWxyw/QDLXPCtP06ll42JURGlYaO2DU5c5BKEUF0alyzlE9 XiHRXPl0LabI/CjGtrIB4RApy1PjkQ31QOt+9R2Nmb7RUkfZwnCWHBlNVnwj4U6J woccrlUdElBWU5twFc2PNPbMR6nA/ldUwDpcveNHNp57BrgYfUFcLrjmf2LH6c7X ngBNPbG5ha5pmsaXm8MAqBRtAvIwvUsvJLIr+XRc27pCJFe/1MtS4hHhTPE4un/z Y/tIrpqm6MimdJcs8oqEQPoztfs5BTNu2jVgrKwtWExDXODWmHemQoaCwzgt3wMy 3wIDAQAB -----END PUBLIC KEY-----"; //私钥(名目中可正在线消费亦可本人天生) $PRIVATE_KEY = "-----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCkknsP9T2Ucs1B 2XgtlKuM290J6UitoydIP+K6NbHLD9AMtc8K0/TqWXjYlREaVho7YNTlzkEoRQXR qXLOUT1eIdFc+XQtpsj8KMa2sgHhECnLU+ORDfVA6371HY2ZvtFSR9nCcJYcGU1W fCPhTonChxyuVR0SUFZTm3AVzY809sxHqcD+V1TAOly940c2nnsGuBh9QVwuuOZ/ YsfpzteeAE09sbmFr妹妹axpebwwCoFG0C8jC9Sy8ksiv5dFzbukIkV7/Uy1LiEeFM 8Ti6f/Nj+0iumqboyKZ0lyzyioRA+jO1+zkFM27aNWCsrC1YTENc4NaYd6ZChoLD OC3fAzLfAgMBAAECggEAUcCieW7uREwzQr7xQFNWVQbzavUEMZ2W6gEydCYwSBt2 0pmOXGamh7QioBSNBnQ3W7/igrZPD94Z4ek3Kt6YiaZrBrC00ejEdt8at6791/vb hzIJHgm9B5701nbz3Kg5+5HNzxV2vEalcI0Cle4Z6RSNXtzRMEPQXoAc0ffnZ/tV 033zAN4nWb9zeLw03/D0nbcpaYA/WbwqsNiTxbbi0s54oTsaOTMBBAK9oH9H2M5J 506iINcKniyMi6i0cf/cQ+tP6VUCOMHdWm/zJmQ5s2eU/2SowSKMXMLIGUH2Q4AX Z2htX4YwvdHGlGA5yPuiMznkFidVcERfbVl9yi54YQKBgQDQYqj2bb0bvD8YuvXx htdBQrxiX53pZ1sVoh5SMxD+Lq6tpn4UtOJw6tpE7tgONmWRaKCH10fgX5nQoXPJ 0Y02qiDyk/TkE0OGiYRTjjkjY3yPkBIz9KRCoIUcwirEfWdzmjFLTq9hiaGo9JXN HcLXOgpAbiQe+qXf9x/waWB/hQKBgQDKLQB9Ep9A6UFlumXaEr971A7HcQI2BsfP kRfCcT1rphnENHCa37o+5i6tTImAXI+aayp9Jpv0rXLbzFbBkdUdUDINulXSsLRT bq3ttbu5c+NG21XW1fvVqf4VYOP7u/l0Z2eBIsg9uLswS3zltTG8ikm+RKhMf1DV PDAOoLmMEwKBgDn0po9a9/Rlx5qmLM7OtMFGwUQO2clXYILEwvATmc9HxncvTfOO V0gWWTxAvUA+qsLlOXhuTGQ/0nSu4pgnusGQUXeF5N8l6Grbhj0C2itYeQUoiZd/ m8uX/01/Rwu84O/K25jZOnfDIn3uAFe6xjy7vKwstckT5txCS9S+SgNNAoGAbvLl Sr32cUvQXMA+9r7FIHJOLfsBaJ6t9mW8cTNtrm63wym4BfXzImN1iBrxdmTVVbur 1IRkn5Cz8JUhoxahqnWBEnGIeZgJTaP2hPXvcCV9uzvQzpYdnrKsQhUq59HPYqcA cSiiVOTUrPswLmsSQVJuh6Dr7xcLSAnAobZoPMsCgYEAsJuY5RcB1sjYortRNsKb KHLiLI93P0MFF46V/343d3BU7TZfETg703Mj2AfOAGTM2p2BkHFri3l+4oigMqpr hAp4hNq4KFK2SCjzedrLV7QIgtp/uMZ+q/yhRtiG8kSWlI9c0Un00+KqIwFqfwAB l1zOX5QcMa1X7eWSvZ559ko= -----END PRIVATE KEY-----"; //待加密明文 $data = "这是RSA待加密明文"; //用于接纳加密后的密文 $content_encrypt = ""; # 私钥加密 openssl_private_encrypt($data, $content_encrypt, $PRIVATE_KEY, 1); var_dump("私钥加密后果:".$content_encrypt); # 公钥解密 //$content_encrypt 私钥加密后的密文 //用于接纳解密后的明文 $content_decrypt = ""; openssl_public_decrypt($content_encrypt, $content_decrypt, $PUBLIC_KEY, 1); var_dump("公钥解密后果:".$content_decrypt);
名目中 API 交互形式之 ---- 署名 / 验签
1. 新建 text.php 文件 ------ 天生署名
//根军 MD5()函数的不成逆性进行署名校验 //起首必需要有 $appKey与$secretKey $appKey = "5d3fb4acb2292"; $secretKey = "5d3fb4acb22925d3fb4acb22925d3fb4acb2292"; $url = "localhost/text_sig.php"; //待通报的参数 $params['appKey'] = $appKey; $params['name'] = "张三"; $params['age'] = "26"; $params['sex'] = "男"; $params['root'] = "admin"; $params['password'] = "123456"; $params['time'] = time(); //猎取署名 $params['sig'] = createSig($params,$secretKey); $param_str = http_build_query($params); $url = $url.'?'.$param_str; var_dump($url); //天生署名 function createSig($params,$secretKey){ //对参数进行排序 ksort($params); $str = http_build_query($params); $str .= $secretKey; return md5($str); }
2. 应用 text.php 文件失去的 URL 拜访 -> 效劳端 text_sig.php 文件 ------- 验证署名
//猎取传值 $get = $_GET; //猎取appKey $appKey = $get['appKey']; //依据appKey猎取存储正在数据库中对应的secretKey $secretKey = "5d3fb4acb22925d3fb4acb22925d3fb4acb2292"; //判别接口能否过时 if (abs($get['time'] - time()) > 100){ die("Time Out"); } //猎取署名 $sig = $get['sig']; //将署名从参数中剔除了 unset($get['sig']); //对参数进行排序 ksort($get); //将参数数组转为'&'衔接的字符串 $str = http_build_query($get); //参数字符串后拼接$secretKey $str .= $secretKey; //应用MD5加密 $md5_str = md5($str); //将失去的加密后的后果与署名比照 if ($sig === $md5_str){ var_dump("验签胜利"); }else{ var_dump("验签失败"); }
留意:以上仅为学习条记,以是较为简略,不敷谨严。
1.实际名目中须优化应用;
2.AES 与 RSA 加密形式最佳与署名 / 验签合营应用,以避免秘钥正在交互进程中被窃听并窜改
以上就是PHP 数据加密的办法的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学
抱歉,评论功能暂时关闭!