旗下导航:搜·么
当前位置:网站首页 > PHP教程 > 正文

PHP 数据加密的要领【php教程】,PHP

作者:搜教程发布时间:2019-12-01分类:PHP教程浏览:70评论:0


导读:环境ApachePHP-7.0.12加密处理了什么问题:1.防备通讯内容被窃听;2.防备通讯内容被改动加密范例:1.对称加密:加密与解密运用的是同一个秘...
环境 Apache PHP-7.0.12

加密处理了什么问题:

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/
YsfpzteeAE09sbmFrmmaxpebwwCoFG0C8jC9Sy8ksiv5dFzbukIkV7/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 数据加密的要领的细致内容,更多请关注ki4网别的相干文章!

标签:PHP


欢迎 发表评论: