Ich habe einen Blick darauf geworfen und festgestellt, dass im Grunde bei nativem PHP die GPG-Erweiterung nicht installiert ist. Also installieren wir die GPG-Erweiterung für PHP selbst:
1. Zuerst laden wir das GPG-Quellcodepaket herunter:
http://pecl.php.net/package/gnupg
Was ich heruntergeladen habe, ist: gnupg-1.3.6.tgz
Nach dem Dekomprimieren cd in das Verzeichnis phpize und dann ./configure.
2.
<code>andy@AndyMacBookPro:~/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>$ phpize Configuring for: PHP Api Version: <span>20100412</span> Zend Module Api No: <span>20100525</span> Zend Extension Api No: <span>220100525</span> andy@AndyMacBookPro:~/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>$ <span>.</span>/configure checking for grep that handles long lines <span>and</span><span>-e</span><span>...</span> /usr/bin/grep checking for egrep<span>...</span> /usr/bin/grep <span>-E</span> checking for a sed that does <span>not</span> truncate output<span>...</span> /usr/bin/sed checking for cc<span>...</span> cc checking whether the C compiler works<span>...</span> yes checking for C compiler default output file name<span>...</span> a<span>.</span>out checking for suffix of executables<span>...</span> checking whether we are cross compiling<span>...</span> no checking for suffix of object files<span>...</span> o checking whether we are using the GNU C compiler<span>...</span> yes checking whether cc accepts <span>-g</span><span>...</span> yes checking for cc option <span>to</span> accept ISO C89<span>...</span><span>none</span> needed checking how <span>to</span> run the C preprocessor<span>...</span> cc <span>-E</span> checking for icc<span>...</span> no checking for suncc<span>...</span> no checking whether cc understands <span>-c</span><span>and</span><span>-o</span> together<span>...</span> yes checking for system library directory<span>...</span> lib checking <span>if</span> compiler supports <span>-R</span><span>...</span> no checking <span>if</span> compiler supports <span>-Wl</span>,<span>-rpath</span>,<span>...</span> yes checking build system <span>type</span><span>...</span> i386<span>-apple</span><span>-darwin13</span><span>.4</span><span>.0</span> checking host system <span>type</span><span>...</span> i386<span>-apple</span><span>-darwin13</span><span>.4</span><span>.0</span> checking target system <span>type</span><span>...</span> i386<span>-apple</span><span>-darwin13</span><span>.4</span><span>.0</span> checking for PHP prefix<span>...</span> /usr checking for PHP <strong>include</strong>s<span>...</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib checking for PHP extension directory<span>...</span> /usr/lib/php/extensions/no<span>-debug</span><span>-non</span><span>-zts</span><span>-</span><span>20100525</span> checking for PHP installed headers prefix<span>...</span> /usr/<strong>include</strong>/php checking <span>if</span> debug is enabled<span>...</span> no checking <span>if</span> zts is enabled<span>...</span> no checking for re2c<span>...</span> no configure: WARNING: You will need re2c <span>0.13</span><span>.4</span><span>or</span> later <span>if</span> you want <span>to</span> regenerate PHP parsers<span>.</span> checking for gawk<span>...</span> no checking for nawk<span>...</span> no checking for awk<span>...</span> awk checking <span>if</span> awk is broken<span>...</span> no checking for gnupg support<span>...</span> yes, shared checking for gnupg files <span>in</span> default path<span>...</span><span>not</span> found configure: error: Please reinstall the gpgme distribution</code>
Es ist ersichtlich, dass es uns daran erinnert, gpgme zu installieren. Ok, dann installieren wir gpgme.
<code>andy<span>@AndyMacBookPro</span>:~/Downloads/gnupg-<span>1.3</span><span>.6</span>$ brew install <span><span>gpgme</span> ==></span> Downloading <span>https</span>:<span>//</span>downloads.sf.net/project/machomebrew/Bottles/gpgme-<span>1.4</span><span>.3</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>######</span><span>100.0</span>% =<span>=></span> Pouring gpgme-<span>1.4</span><span>.3</span>.mavericks.bottle.tar.gz ?? /usr/local/Cellar/gpgme/<span>1.4</span><span>.3</span>: <span>17</span> files, <span>1.2</span>M andy<span>@AndyMacBookPro</span>:~/Downloads/gnupg-<span>1.3</span><span>.6</span>$ ./configure checking <span>for</span> grep that handles long lines <span>and</span> -e... /usr/bin/grep checking <span>for</span> egrep... /usr/bin/grep -E checking <span>for</span> a sed that does <span>not</span> truncate output... /usr/bin/sed checking <span>for</span> cc... cc checking whether the C compiler works... <span>yes</span> checking <span>for</span> C compiler <span>default</span> output file name... a.out checking <span>for</span> suffix <span>of</span> executables... checking whether we are cross compiling... <span>no</span> checking <span>for</span> suffix <span>of</span> object files... o checking whether we are using the GNU C compiler... <span>yes</span> checking whether cc accepts -g... <span>yes</span> checking <span>for</span> cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking <span>for</span> icc... <span>no</span> checking <span>for</span> suncc... <span>no</span> checking whether cc understands -c <span>and</span> -o together... <span>yes</span> checking <span>for</span> system library directory... lib checking <span>if</span> compiler supports -R... <span>no</span> checking <span>if</span> compiler supports -Wl,-rpath,... <span>yes</span> checking build system type... i386-apple-darwin13<span>.4</span><span>.0</span> checking host system type... i386-apple-darwin13<span>.4</span><span>.0</span> checking target system type... i386-apple-darwin13<span>.4</span><span>.0</span> checking <span>for</span> PHP prefix... /usr checking <span>for</span> PHP <strong>include</strong>s... -I/usr/<strong>include</strong>/php -I/usr/<strong>include</strong>/php/main -I/usr/<strong>include</strong>/php/TSRM -I/usr/<strong>include</strong>/php/Zend -I/usr/<strong>include</strong>/php/ext -I/usr/<strong>include</strong>/php/ext/date/lib checking <span>for</span> PHP extension directory... /usr/lib/php/extensions/<span>no</span>-debug-non-zts-<span>20100525</span> checking <span>for</span> PHP installed headers prefix... /usr/<strong>include</strong>/php checking <span>if</span> debug <span>is</span> enabled... <span>no</span> checking <span>if</span> zts <span>is</span> enabled... <span>no</span> checking <span>for</span> re2c... <span>no</span><span>configure</span>: <span>WARNING</span>: You will need re2c <span>0.13</span><span>.4</span><span>or</span> later <span>if</span> you want to regenerate PHP parsers. checking <span>for</span> gawk... <span>no</span> checking <span>for</span> nawk... <span>no</span> checking <span>for</span> awk... awk checking <span>if</span> awk <span>is</span> broken... <span>no</span> checking <span>for</span> gnupg support... <span>yes</span>, shared checking <span>for</span> gnupg files <span>in</span><span>default</span> path... found <span>in</span> /usr/local/<strong>include</strong> checking <span>for</span> gpgme_check_version <span>in</span> -lgpgme... <span>yes</span> checking <span>for</span> gpg... /usr/local/bin/gpg checking <span>for</span> ld used <span>by</span> cc... /<strong>Application</strong>s/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld checking <span>if</span> the linker (/<strong>Application</strong>s/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) <span>is</span> GNU ld... <span>no</span> checking <span>for</span> /<strong>Application</strong>s/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r checking <span>for</span> BSD-compatible nm... /usr/bin/nm checking whether ln -s works... <span>yes</span> checking how to recognize dependent libraries... pass_all checking <span>for</span> ANSI C header files... <span>yes</span> checking <span>for</span> sys/types.h... <span>yes</span> checking <span>for</span> sys/stat.h... <span>yes</span> checking <span>for</span> stdlib.h... <span>yes</span> checking <span>for</span> string.h... <span>yes</span> checking <span>for</span> memory.h... <span>yes</span> checking <span>for</span> strings.h... <span>yes</span> checking <span>for</span> inttypes.h... <span>yes</span> checking <span>for</span> stdint.h... <span>yes</span> checking <span>for</span> unistd.h... <span>yes</span> checking dlfcn.h usability... <span>yes</span> checking dlfcn.h presence... <span>yes</span> checking <span>for</span> dlfcn.h... <span>yes</span> checking the maximum length <span>of</span> command line arguments... <span>196608</span> checking command to parse /usr/bin/nm output from cc object... ok checking <span>for</span> objdir... .libs checking <span>for</span> ar... ar checking <span>for</span> ranlib... ranlib checking <span>for</span> strip... strip checking <span>for</span> dsymutil... dsymutil checking <span>for</span> nmedit... nmedit checking <span>for</span> -single_module linker flag... <span>yes</span> checking <span>for</span> -exported_symbols_list linker flag... <span>yes</span> checking <span>if</span> cc supports -fno-rtti -fno-exceptions... <span>yes</span> checking <span>for</span> cc option to produce PIC... -fno-common checking <span>if</span> cc PIC flag -fno-common works... <span>yes</span> checking <span>if</span> cc static flag -static works... <span>no</span> checking <span>if</span> cc supports -c -o file.o... <span>yes</span> checking whether the cc linker (/<strong>Application</strong>s/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... <span>yes</span> checking dynamic linker characteristics... darwin13<span>.4</span><span>.0</span> dyld checking how to hardcode library paths into programs... immediate checking whether stripping libraries <span>is</span> possible... <span>yes</span> checking <span>if</span> libtool supports shared libraries... <span>yes</span> checking whether to build shared libraries... <span>yes</span> checking whether to build static libraries... <span>no</span>creating libtool appending configuration tag <span>"CXX"</span> to libtool <span>configure</span>: creating ./config.status config.<span>status</span>: creating config.h andy<span>@AndyMacBookPro</span>:~/Downloads/gnupg-<span>1.3</span><span>.6</span>$</code>
make:
<code>andy@AndyMacBookPro:~/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>$ make /bin/sh /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/libtool <span>--</span>mode<span>=</span>compile cc <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/<strong>include</strong> <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/main <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib <span>-I</span>/usr/<span>local</span>/<strong>include</strong> <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/gnupg<span>.</span>c <span>-o</span> gnupg<span>.</span>lo mkdir <span>.</span>libs cc <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/<strong>include</strong> <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/main <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib <span>-I</span>/usr/<span>local</span>/<strong>include</strong> <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/gnupg<span>.</span>c <span>-fno</span><span>-common</span><span>-DPIC</span><span>-o</span><span>.</span>libs/gnupg<span>.</span>o /bin/sh /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/libtool <span>--</span>mode<span>=</span>compile cc <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/<strong>include</strong> <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/main <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib <span>-I</span>/usr/<span>local</span>/<strong>include</strong> <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/gnupg_keylistiterator<span>.</span>c <span>-o</span> gnupg_keylistiterator<span>.</span>lo cc <span>-I</span><span>.</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/<strong>include</strong> <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/main <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib <span>-I</span>/usr/<span>local</span>/<strong>include</strong> <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-c</span> /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/gnupg_keylistiterator<span>.</span>c <span>-fno</span><span>-common</span><span>-DPIC</span><span>-o</span><span>.</span>libs/gnupg_keylistiterator<span>.</span>o /bin/sh /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/libtool <span>--</span>mode<span>=</span><span>link</span> cc <span>-DPHP_ATOM_INC</span><span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/<strong>include</strong> <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/main <span>-I</span>/Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span><span>-I</span>/usr/<strong>include</strong>/php <span>-I</span>/usr/<strong>include</strong>/php/main <span>-I</span>/usr/<strong>include</strong>/php/TSRM <span>-I</span>/usr/<strong>include</strong>/php/Zend <span>-I</span>/usr/<strong>include</strong>/php/ext <span>-I</span>/usr/<strong>include</strong>/php/ext/<span>date</span>/lib <span>-I</span>/usr/<span>local</span>/<strong>include</strong> <span>-DHAVE_CONFIG_H</span><span>-g</span><span>-O2</span><span>-o</span> gnupg<span>.</span>la <span>-export</span><span>-dynamic</span><span>-avoid</span><span>-version</span><span>-prefer</span><span>-pic</span><span>-module</span><span>-rpath</span> /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/modules gnupg<span>.</span>lo gnupg_keylistiterator<span>.</span>lo <span>-Wl</span>,<span>-rpath</span>,/usr/<span>local</span>/<strong>include</strong>/lib <span>-L</span>/usr/<span>local</span>/<strong>include</strong>/lib <span>-lgpgme</span> cc ${wl}<span>-flat_namespace</span> ${wl}<span>-undefined</span> ${wl}suppress <span>-o</span><span>.</span>libs/gnupg<span>.</span>so <span>-bundle</span><span>.</span>libs/gnupg<span>.</span>o <span>.</span>libs/gnupg_keylistiterator<span>.</span>o <span>-L</span>/usr/<span>local</span>/<strong>include</strong>/lib <span>-lgpgme</span><span>-Wl</span>,<span>-rpath</span><span>-Wl</span>,/usr/<span>local</span>/<strong>include</strong>/lib ld: warning: directory <span>not</span> found for option <span>'-L/usr/local/<strong>include</strong>/lib'</span> dsymutil <span>.</span>libs/gnupg<span>.</span>so <span>||</span> : creating gnupg<span>.</span>la (cd <span>.</span>libs <span>&&</span> rm <span>-f</span> gnupg<span>.</span>la <span>&&</span> ln <span>-s</span><span>..</span>/gnupg<span>.</span>la gnupg<span>.</span>la) /bin/sh /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/libtool <span>--</span>mode<span>=</span>install cp <span>.</span>/gnupg<span>.</span>la /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/modules cp <span>.</span><span>/</span><span>.</span>libs/gnupg<span>.</span>so /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/modules/gnupg<span>.</span>so cp <span>.</span><span>/</span><span>.</span>libs/gnupg<span>.</span>lai /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/modules/gnupg<span>.</span>la <span>----------------------------------------------------------------------</span> Libraries have been installed <span>in</span>: /Users/andy/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>/modules <span>If</span> you ever happen <span>to</span> want <span>to</span><span>link</span> against installed libraries <span>in</span> a given directory, LIBDIR, you must either use libtool, <span>and</span> specify the <span>full</span> pathname of the library, <span>or</span> use the <span>`-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `</span>DYLD_LIBRARY_PATH<span>' environment variable during execution See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don'</span>t forget <span>to</span> run <span>'make test'</span><span>.</span>andy@AndyMacBookPro:~/Downloads/gnupg<span>-</span><span>1.3</span><span>.6</span>$</code>
4 Wir können sehen, dass die so-Datei generiert wurde, wir setzen die so-Datei in das Erweiterungsverzeichnis von PHP verschieben:
<code>andy<span>@AndyMacBookPro</span><span>:/usr/lib/php/extensions/no-debug-non-zts-</span><span>20100525</span><span>$ </span>sudo cp /<span>Users</span>/andy/<span>Downloads</span>/gnupg-<span>1.3</span>.<span>6</span>/modules/gnupg.so ./</code>
Dann fügen wir diese Erweiterung zu php.ini hinzu:
extension = gnupg.so
Dann können wir Gunpg-Erweiterung in PHP verwenden:
<code><span><span><?php</span><span>$url</span> = <span>'http://192.168.2.85/2015/09/20/加密文件.csv'</span>; <span>$data</span> = file_get_contents(<span>$url</span>); <span>$data</span> = mb_convert_encoding(<span>$data</span>, <span>'UTF-8'</span>, <span>'GBK'</span>); <span>$encrypted_text</span> = <span>$data</span>; <span>$res</span> = gnupg_init(); gnupg_adddecryptkey(<span>$res</span>,<span>"ABCFA237F71C57CA2042439F93704A63BCC8A2F"</span>,<span>"12345678"</span>); <span>$plain</span> = gnupg_decrypt(<span>$res</span>,<span>$encrypted_text</span>); var_dump(<span>$plain</span>);<span>exit</span>;</span></code>
Vielen Dank für diesen Artikel:
http://devzone.zend.com/1278/using-gnupg-with-php/
Das Obige stellt die Installation der GPG-Erweiterung in PHP vor, einschließlich der relevanten Inhalte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.