75142913在线留言
PHP利用openssl_encrypt实现数据的加解密_PHP技术_网络人

PHP利用openssl_encrypt实现数据的加解密

Kwok 发表于:2019-11-04 16:04:10 点击:21 评论: 0

在我们项目中经常会使用到一些特殊、敏感的数据需要交换,为了防止第三方监听,我们可以对数据进行加密传输,当到达客户端时,通过密钥对数据解密还原,以达到加密传输的效果,虽然我们很多时候使用了SSL/HTTPS加密,但无法防止爬虫对数据的获取,所以自定义加密传输敏感内容也是很有必要的,下面我们将以PHP为例,对数据做基本的简单加密处理。

一、基本加密

$data = 'http://www.55mx.com'; //加密明文
$method = 'DES-ECB'; //加密方法
$passwd = '110'; //加密密钥
$options = 0; //数据格式选项(可选)
$result = openssl_encrypt($data, $method, $passwd, $options);
var_dump($result);//string(32) "XTEAhRrXSUUaXpFtQ/6xpFkML5cObhCX"
var_dump(openssl_decrypt($result, $method, $passwd, 0));//string(21) "http://www.55mx.com"

这是快速简单的加密和解密方法。常常用于数据交换防止被窃听。接收端只要密码正确就能还原内容。

二、AES认证加密

下面是PHP 5.6+ 的 AES 认证加密例子,同样的内容每次输出字符都会变化(更安全)。

$key = '123456';//加密密码
$plaintext = "http://www.55mx.com/";
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
$ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );
var_dump($ciphertext);//string(108) "输入内容是变化的哦"

 下面是解密内容

$c = base64_decode($ciphertext);//解码base64
$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len);
$original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
if (hash_equals($hmac, $calcmac)) //PHP 5.6+ 定时攻击安全性比较
{
    echo $original_plaintext;//http://www.55mx.com/
}

通过AES加解密对于数据传输更安全,同样的内容被抓包识别的可能性更低。

三、高版本PHP7.1+ 加密

上面的代码是不是又多又复杂,好了,可以升级到PHP7.1+看看这样的代码满足你吗?

$key = '我是密码';
$plaintext = "http://www.55mx.com/";
$cipher = "aes-128-gcm";
if (in_array($cipher, openssl_get_cipher_methods())) {
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options = 0, $iv, $tag);
    //使用 $cipher, $iv, and $tag 解密
    $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options = 0, $iv, $tag);
    echo $original_plaintext;//http://www.55mx.com/
}

我们可以切换加密方式,达到混合加密的效果,当然这对客户端、服务端CPU加、解密也会有小小压力的,所以请选择适合自己的加密方式,服务器端可以考虑缓存已加密好的数据以缓解压力。

四、加密方式介绍(扩展知识)

我们可以通过openssl_get_cipher_methods函数获取当前PHP支持的加密方式(获取可用的加密算法),大约有上百种:

<?php
$ciphers             = openssl_get_cipher_methods();
$ciphers_and_aliases = openssl_get_cipher_methods(true);
$cipher_aliases      = array_diff($ciphers_and_aliases, $ciphers);

//过滤ECB模式
$ciphers = array_filter( $ciphers, function($n) { return stripos($n,"ecb")===FALSE; } );

//2016年8月起,Openssl就声明了基于:RC2, RC4, DES, 3DES, MD5的这几种加密方式已不再安全
$ciphers = array_filter( $ciphers, function($c) { return stripos($c,"des")===FALSE; } );
$ciphers = array_filter( $ciphers, function($c) { return stripos($c,"rc2")===FALSE; } );
$ciphers = array_filter( $ciphers, function($c) { return stripos($c,"rc4")===FALSE; } );
$ciphers = array_filter( $ciphers, function($c) { return stripos($c,"md5")===FALSE; } );
$cipher_aliases = array_filter($cipher_aliases,function($c) { return stripos($c,"des")===FALSE; } );
$cipher_aliases = array_filter($cipher_aliases,function($c) { return stripos($c,"rc2")===FALSE; } );

print_r($ciphers);
print_r($cipher_aliases);
除非注明,网络人的文章均为原创,转载请以链接形式标明本文地址:https://www.55mx.com/post/53
标签:加密解密Kwok最后编辑于:2021-12-13 12:13:37
12
感谢打赏!

《PHP利用openssl_encrypt实现数据的加解密》的网友评论(0)

本站推荐阅读

热门点击文章