This extension makes use of the keyring of the current user. This keyring is normally located in ~./.gnupg/. To specify a custom location, store the path to the keyring in the environment variable GNUPGHOME. See putenv for more information how to do this.

Some functions require the specification of a key. This specification can be anything that refers to a unique key (userid, key-id, fingerprint, ...). This documentation uses the fingerprint in all examples.

Note:

As alternative to the explicitly documented functions using class="type">resources, you can also use an object-oriented style using gnupg objects.

gnupg_adddecryptkey

Add a key for decryption

说明

bool gnupg_adddecryptkey ( resource $identifier , string $fingerprint , string $passphrase )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

fingerprint
指纹键名。

passphrase
The pass phrase.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_adddecryptkey example

<?php
$res = gnupg_init();
gnupg_adddecryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
?>

示例 #2 OO gnupg_adddecryptkey example

<?php
$gpg = new gnupg();
$gpg -> adddecryptkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
?>

gnupg_addencryptkey

Add a key for encryption

说明

bool gnupg_addencryptkey ( resource $identifier , string $fingerprint )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

fingerprint
指纹键名。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_addencryptkey example

<?php
$res = gnupg_init();
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
?>

示例 #2 OO gnupg_addencryptkey example

<?php
$gpg = new gnupg();
$gpg -> addencryptkey("8660281B6051D071D94B5B230549F9DC851566DC");
?>

gnupg_addsignkey

Add a key for signing

说明

bool gnupg_addsignkey ( resource $identifier , string $fingerprint [, string $passphrase ] )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

fingerprint
指纹键名。

passphrase
The pass phrase.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_addsignkey example

<?php
$res = gnupg_init();
gnupg_addsignkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
?>

示例 #2 OO gnupg_addsignkey example

<?php
$gpg = new gnupg();
$gpg -> addsignkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
?>

gnupg_cleardecryptkeys

Removes all keys which were set for decryption before

说明

bool gnupg_cleardecryptkeys ( resource $identifier )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_cleardecryptkeys example

<?php
$res = gnupg_init();
gnupg_cleardecryptkeys($res);
?>

示例 #2 OO gnupg_cleardecryptkeys example

<?php
$gpg = new gnupg();
$gpg -> cleardecryptkeys();
?>

gnupg_clearencryptkeys

Removes all keys which were set for encryption before

说明

bool gnupg_clearencryptkeys ( resource $identifier )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_clearencryptkeys example

<?php
$res = gnupg_init();
gnupg_clearencryptkeys($res);
?>

示例 #2 OO gnupg_clearencryptkeys example

<?php
$gpg = new gnupg();
$gpg -> clearencryptkeys();
?>

gnupg_clearsignkeys

Removes all keys which were set for signing before

说明

bool gnupg_clearsignkeys ( resource $identifier )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_clearsignkeys example

<?php
$res = gnupg_init();
gnupg_clearsignkeys($res);
?>

示例 #2 OO gnupg_clearsignkeys example

<?php
$gpg = new gnupg();
$gpg -> clearsignkeys();
?>

gnupg_decrypt

Decrypts a given text

说明

string gnupg_decrypt ( resource $identifier , string $text )

Decrypts the given text with the keys, which were set with gnupg_adddecryptkey before.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

text
The text being decrypted.

返回值

On success, this function returns the decrypted text. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_decrypt example

<?php
$res = gnupg_init();
gnupg_adddecryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
$plain = gnupg_decrypt($res,$encrypted_text);
echo $plain;
?>

示例 #2 OO gnupg_encrypt example

<?php
$gpg = new gnupg();
$gpg -> adddecryptkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
$plain = $gpg -> decrypt($encrypted_text);
echo $plain;
?>

gnupg_decryptverify

Decrypts and verifies a given text

说明

array gnupg_decryptverify ( resource $identifier , string $text , string &$plaintext )

Decrypts and verifies a given text and returns information about the signature.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

text
The text being decrypted.

plaintext
The parameter plaintext gets filled with the decrypted text.

返回值

On success, this function returns information about the signature and fills the plaintext parameter with the decrypted text. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_decryptverify example

<?php
$plaintext = "";
$res = gnupg_init();
gnupg_adddecryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
$info = gnupg_decryptverify($res,$text,$plaintext);
print_r($info);
?>

示例 #2 OO gnupg_decryptverify example

<?php
$plaintext = "";
$gpg = new gnupg();
$gpg -> adddecryptkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
$info = $gpg -> decryptverify($text,$plaintext);
print_r($info);
?>

gnupg_encrypt

Encrypts a given text

说明

string gnupg_encrypt ( resource $identifier , string $plaintext )

Encrypts the given plaintext with the keys, which were set with gnupg_addencryptkey before and returns the encrypted text.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

plaintext
The text being encrypted.

返回值

On success, this function returns the encrypted text. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_encrypt example

<?php
$res = gnupg_init();
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
$enc = gnupg_encrypt($res, "just a test");
echo $enc;
?>

示例 #2 OO gnupg_encrypt example

<?php
$gpg = new gnupg();
$gpg -> addencryptkey("8660281B6051D071D94B5B230549F9DC851566DC");
$enc = $gpg -> encrypt("just a test");
echo $enc;
?>

gnupg_encryptsign

Encrypts and signs a given text

说明

string gnupg_encryptsign ( resource $identifier , string $plaintext )

Encrypts and signs the given plaintext with the keys, which were set with gnupg_addsignkey and gnupg_addencryptkey before and returns the encrypted and signed text.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

plaintext
The text being encrypted.

返回值

On success, this function returns the encrypted and signed text. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_encryptsign example

<?php
$res = gnupg_init();
gnupg_addencryptkey($res,"8660281B6051D071D94B5B230549F9DC851566DC");
gnupg_addsignkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
$enc = gnupg_encryptsign($res, "just a test");
echo $enc;
?>

示例 #2 OO gnupg_encryptsign example

<?php
$gpg = new gnupg();
$gpg -> addencryptkey("8660281B6051D071D94B5B230549F9DC851566DC");
$gpg -> addsignkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
$enc = $gpg -> encryptsign("just a test");
echo $enc;
?>

gnupg_export

Exports a key

说明

string gnupg_export ( resource $identifier , string $fingerprint )

Exports the key fingerprint.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

fingerprint
指纹键名。

返回值

On success, this function returns the keydata. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_export example

<?php
$res = gnupg_init();
$export = gnupg_export($res,"8660281B6051D071D94B5B230549F9DC851566DC");
echo $export;
?>

示例 #2 OO gnupg_export example

<?php
$gpg = new gnupg();
$export = $gpg -> export("8660281B6051D071D94B5B230549F9DC851566DC");
?>

gnupg_geterror

Returns the errortext, if a function fails

说明

string gnupg_geterror ( resource $identifier )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

返回值

Returns an errortext, if an error has occurred, otherwise FALSE.

范例

示例 #1 Procedural gnupg_geterror example

<?php
$res = gnupg_init();
echo gnupg_geterror($res);
?>

示例 #2 OO gnupg_geterror example

<?php
$gpg = new gnupg();
echo $gpg -> geterror();
?>

gnupg_getprotocol

Returns the currently active protocol for all operations

说明

int gnupg_getprotocol ( resource $identifier )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

返回值

Returns the currently active protocol, which can be one of GNUPG_PROTOCOL_OpenPGP or GNUPG_PROTOCOL_CMS.

范例

示例 #1 Procedural gnupg_getprotocol example

<?php
$res = gnupg_init();
echo gnupg_getprotocol($res);
?>

示例 #2 OO gnupg_getprotocol example

<?php
$gpg = new gnupg();
echo $gpg -> getprotocol();
?>

gnupg_import

Imports a key

说明

array gnupg_import ( resource $identifier , string $keydata )

Imports the key keydata and returns an array with information about the importprocess.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

keydata
The data key that is being imported.

返回值

On success, this function returns and info-array about the importprocess. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_import example

<?php
$res = gnupg_init();
$info = gnupg_import($res,$keydata);
print_r($info);
?>

示例 #2 OO gnupg_import example

<?php
$gpg = new gnupg();
$info = $gpg -> import($keydata);
print_r($info);
?>

gnupg_init

Initialize a connection

说明

resource gnupg_init ( void )

参数

此函数没有参数。

返回值

A GnuPG resource connection used by other GnuPG functions.

范例

示例 #1 Procedural gnupg_init example

<?php
$res = gnupg_init();
?>

示例 #2 OO gnupg initializer example

<?php
$gpg = new gnupg();
?>

gnupg_keyinfo

Returns an array with information about all keys that matches the given pattern

说明

array gnupg_keyinfo ( resource $identifier , string $pattern )

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

pattern
The pattern being checked against the keys.

返回值

Returns an array with information about all keys that matches the given pattern or FALSE, if an error has occurred.

范例

示例 #1 Procedural gnupg_keyinfo example

<?php
$res = gnupg_init();
$info = gnupg_keyinfo($res, 'test');
print_r($info);
?>

示例 #2 OO gnupg_keyinfo example

<?php
$gpg = new gnupg();
$info = $gpg -> keyinfo("test");
print_r($info);
?>

gnupg_setarmor

Toggle armored output

说明

bool gnupg_setarmor ( resource $identifier , int $armor )

Toggle the armored output.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

armor
Pass a non-zero integer-value to this function to enable armored-output (default). Pass 0 to disable armored output.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_setarmor example

<?php
$res = gnupg_init();
gnupg_setarmor($res,1); // enable armored output;
gnupg_setarmor($res,0); // disable armored output;
?>

示例 #2 OO gnupg_setarmor example

<?php
$gpg = new gnupg();
$gpg -> setarmor(1); // enable armored output;
$gpg -> setarmor(0); // disable armored output;
?>

gnupg_seterrormode

Sets the mode for error_reporting

说明

void gnupg_seterrormode ( resource $identifier , int $errormode )

Sets the mode for error_reporting.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

errormode
The error mode.

errormode takes a constant indicating what type of error_reporting should be used. The possible values are GNUPG_ERROR_WARNING, GNUPG_ERROR_EXCEPTION and GNUPG_ERROR_SILENT. By default GNUPG_ERROR_SILENT is used.

返回值

没有返回值。

范例

示例 #1 Procedural gnupg_seterrormode example

<?php
$res = gnupg_init();
gnupg_seterrormode($res,GNUPG_ERROR_WARNING); // raise a PHP-Warning in case of an error
?>

示例 #2 OO gnupg_seterrormode example

<?php
$gpg = new gnupg();
$gpg -> seterrormode(gnupg::ERROR_EXCEPTION); // throw an exception in case of an error
?>

gnupg_setsignmode

Sets the mode for signing

说明

bool gnupg_setsignmode ( resource $identifier , int $signmode )

Sets the mode for signing.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

sigmode
The mode for signing.

signmode takes a constant indicating what type of signature should be produced. The possible values are GNUPG_SIG_MODE_NORMAL, GNUPG_SIG_MODE_DETACH and GNUPG_SIG_MODE_CLEAR. By default GNUPG_SIG_MODE_CLEAR is used.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Procedural gnupg_setsignmode example

<?php
$res = gnupg_init();
gnupg_setsignmode($res,GNUPG_SIG_MODE_DETACH); // produce a detached signature
?>

示例 #2 OO gnupg_setsignmode example

<?php
$gpg = new gnupg();
$gpg -> setsignmode(gnupg::SIG_MODE_DETACH); // produce a detached signature
?>

gnupg_sign

Signs a given text

说明

string gnupg_sign ( resource $identifier , string $plaintext )

Signs the given plaintext with the keys, which were set with gnupg_addsignkey before and returns the signed text or the signature, depending on what was set with gnupg_setsignmode.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

plaintext
The plain text being signed.

返回值

On success, this function returns the signed text or the signature. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_sign example

<?php
$res = gnupg_init();
gnupg_addsignkey($res,"8660281B6051D071D94B5B230549F9DC851566DC","test");
$signed = gnupg_sign($res, "just a test");
echo $signed;
?>

示例 #2 OO gnupg_sign example

<?php
$gpg = new gnupg();
$gpg->addsignkey("8660281B6051D071D94B5B230549F9DC851566DC","test");
$signed = $gpg->sign("just a test");
echo $signed;
?>

gnupg_verify

Verifies a signed text

说明

array gnupg_verify ( resource $identifier , string $signed_text , string $signature [, string &$plaintext ] )

Verifies the given signed_text and returns information about the signature.

参数

identifier
gnupg 标识符,由对 gnupg_initgnupg 的调用生成。

signed_text
The signed text.

signature
The signature. To verify a clearsigned text, set signature to FALSE.

plaintext
The plain text. If this optional parameter is passed, it is filled with the plain text.

返回值

On success, this function returns information about the signature. On failure, this function returns FALSE.

范例

示例 #1 Procedural gnupg_verify example

<?php
$plaintext = "";
$res = gnupg_init();
// clearsigned
$info = gnupg_verify($res,$signed_text,false,$plaintext);
print_r($info);
// detached signature
$info = gnupg_verify($res,$signed_text,$signature);
print_r($info);
?>

示例 #2 OO gnupg_verify example

<?php
$plaintext = "";
$gpg = new gnupg();
// clearsigned
$info = $gpg -> verify($signed_text,false,$plaintext);
print_r($info);
// detached signature
$info = $gpg -> verify($signed_text,$signature);
print_r($info);
?>

目录