Home>Article>Backend Development> How to encrypt files using gpg via php

How to encrypt files using gpg via php

醉折花枝作酒筹
醉折花枝作酒筹 forward
2021-07-06 15:53:27 2558browse

Encryption A simple yet practical task is to send encrypted email. For years, the standard for encrypting email has been PGP. But it is commercial software and cannot be used freely. As an alternative to PGP, an open source similar product is now available. GPG does not contain patented algorithms and can be used for commercial applications without restrictions.

How to encrypt files using gpg via php

#Background: The customer provides the private key and requires us to transfer the encrypted file to them through php.

Environment

  • macOS Sierra 10.12.1

  • php 7.0.8

Install gpg environment

macOS:
$ brew install gpg
CentOS
$ yum install gnupg
php

Install gnupg extension, please refer to my old article for specific methods: Use phpize to install the php extension

Import the private key, and the public key will be imported accordingly

$ gpg --import /Users/xjnotxj/downloads/6e.pri

How to encrypt files using gpg via php

Test the correctness of the key[can be skipped]

Encrypted file
$ gpg --recipient 0D39xxxx --output test_file.xls.gpg --encrypt test_file.xls

0D39xxxx => #1

Decrypted file in the above picture
$ gpg -o test_file_new.xls -d test_file.xls.gpg

Export public key

$ gpg -o pubkey.txt -a --export e6e6xxxx

e6e6xxxx => #

The above is the detailed content of How to encrypt files using gpg via php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete