当前位置:首页 > 技术分享

​CentOS 安装libsodium 支持 crypto_aead_aes256gcm_decrypt 兼容php5.6,php7.2

admin4年前 (2021-12-18)技术分享4194

微信小微商户下载证书返回的密文用 AEAD_AES_256_GCM 算法 解密的方法。其中用到了 string sodium_crypto_aead_aes256gcm_decrypt ( string $ciphertext , string $ad , string $nonce , string $key ) 这个函数。但是这个函数可能你们调用的时候会报错,那是因为使用这个函数需要开启 libsodium 扩展才能使用。


(PHP ≥ 7.2) 直接去 php.ini 开启扩展就行 ,extension=php_sodium.dll 。因为 PHP7.2 版本后 PHP core 中就整合加密库 Libsodium
(PHP < 7.2.0) 需要安装,libsodium

CentOS:安装libsodium


参考:https://zhuanlan.zhihu.com/p/84899708


在http://pecl.php.net/package/libsodium和https://github.com/jedisct1/libsodium-php看了下,


只合适用1.*的版本


wget http://pecl.php.net/get/libsodium-1.0.7.tgz
pecl install libsodium-1.0.7.tgz

 

安装好之后提示:

Build process completed successfully
Installing '/usr/local/php5/lib/php/extensions/no-debug-non-zts-20131226/libsodium.so'
install ok: channel://pecl.php.net/libsodium-1.0.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=libsodium.so" to php.ini

  

vi /usr/local/php/etc/php.ini

添加上

extension=libsodium.so

即可

 

测试代码

<?php
$str='17pV1s3a8bIHpOJrIOtTAuRSEToHhFDU1KFARw==';
$ad = 'fullstackpm'; // 加密证书的随机串
$nonce = '4k3HqbrXwtpg';
$kengen = 'D2Ffwerq2c3242aa2F3RF52B315dff11';
// 解密
$pem=base64_decode($str);
if(version_compare(PHP_VERSION,'7.2.0','<')){
    $v = \Sodium\crypto_aead_aes256gcm_decrypt($pem, $ad, $nonce, $kengen);
}else{
    $v = sodium_crypto_aead_aes256gcm_decrypt($pem, $ad, $nonce, $kengen);
}
var_dump($v);
?>


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/221.html

分享给朋友:

“​CentOS 安装libsodium 支持 crypto_aead_aes256gcm_decrypt 兼容php5.6,php7.2” 的相关文章

input search更改默认删除按钮的样式

改input输入框search属性下输入文字之后会在输入框最后出现一个默认样式的X不过这个样式不太好看想自定义怎么办呢方法一input[type="search"]::-webkit-search-cancel-butt…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

PHP AES加解密 (ECB模式/sha1prng算法/PKCS5Padding和PKCS7Padding补码) ECB 模式不需求设置 iv

php7+ 版本/**  * [AesSecurity aes加密,支持PHP7+]  * 算法模式:ECB  * 密钥长度:128  * 补…

七牛html js上传带进度条源码

七牛html js上传带进度条源码注册链接https://s.qiniu.com/uM7RJv完整代码下载:https://n802.com/f/349707-489018989-c141f6(访问密码:5036)http://www.yi…

在CentOS 5.x 6.x里使用yum源 换阿里云国内源换vault.centos.org源

阿里云CentOS 5 的系统,无法用yum来安装应用软件。  原因:CentOS 5 在2017-03-31日已经结束支持,不再提供维护更新,所以包括阿里云镜像站的文件可能都是过时或已经有部分文件缺失。 &n…

阿里云盘 分享脚本 抢先实现文件分享功能

Win端基本所有浏览器均可进行安装安装地址:https://bbs.tampermonkey.net.cn/thread-427-1-1.html如何安装:Tampermoneky安装:谷歌浏览器参考:https://bbs.tamperm…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。