需要安装对应PHP版本的gnupg扩展
// 设置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(); }