好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

如何通过php使用gpg加密文件

加密一个简单但又实用的任务就是发送加密电子邮件。多年来,为电子邮件进行加密的标准一直是PGP。但它是商业软件,不能自由使用。作为PGP的替代,如今已经有一个开放源代码的类似产品可供使用。GPG不包含专利算法,能够无限制的用于商业应用。

安装gpg环境 macOS: 解密文件

使用php加密文件
// 设置gnupg在你本机的路径
putenv('GNUPGHOME=/root/.gnupg');
try {
    //获取公钥
    $publicKey = file_get_contents('application/report/pubkey.txt');
    //初始化gpg
    $gpg = new gnupg();
    //开启调试
    $gpg->seterrormode(gnupg::ERROR_EXCEPTION);
    //导入公钥
    $info = $gpg->import($publicKey);
    //获取公钥指纹
    $gpg->addencryptkey($info['fingerprint']);
    //获取需要加密的文件
    $uploadFileContent = file_get_contents($filename);
    //加密文件
    $enc = $gpg->encrypt($uploadFileContent);

    //保存文件到本地
    $filename = 'application/report/'.'file_xls' . '.gpg';
    file_put_contents($filename, $enc);

} catch (Exception $e) {
    //log something
    return $e->getMessage();
}

推荐学习:php视频教程

以上就是如何通过php使用gpg加密文件的详细内容!

查看更多关于如何通过php使用gpg加密文件的详细内容...

  阅读:34次