注意 This documentation covers version 0.7.0 of Crypt_GPG which has an API that is still in beta. The code in these examples is subject to change.
注意 The following examples assume you have created a key following the instructions for generating a key that can both encrypt and sign data. The example key has a user id of test@example.com and a passphrase of test. All signature data is ficticious but is formatted like real signature data.
<?php require_once 'Crypt/GPG.php'; $signature = <<<DATA -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQBIl9Tf6ZWCktsVoskRAoAKAJ9VkbFDTSGY2ygaEGBcMOE8Or9puwCgppYm 0qq0bhtw5vsi0cJF5oC52RY= =VfxI -----END PGP SIGNATURE----- DATA; $signedData = file_get_contents($package_file_name); $gpg = new Crypt_GPG(); $details = $gpg->verify($signed_data, $signature); if ($details->isValid()) { echo "Package is valid.\n"; } else { echo "Package is invalid!\n"; } ?> |
<?php require_once 'Crypt/GPG.php'; // ... connect to database ... $card_number = '411111111111'; $card_type = 'visa'; $gpg = new Crypt_GPG(); $gpg->addEncryptKey('test@example.com'); $encrypted = $gpg->encrypt('test@example.com', $card_number); $sql = sprintf('insert into payments (card_type, card_number) ' . 'values (%s, %s)', mysql_real_escape_string($card_type), mysql_real_escape_string($card_number)); mysql_exec($sql); ?> |
<?php require_once 'Crypt/GPG.php'; // ... connect to database ... $gpg = new Crypt_GPG(); $gpg->addDecryptKey('test@example.com', 'test'); $sql = 'select card_type, card_number from payments'; $rs = mysql_query($sql); while ($row = mysql_fetch_object($rs)) { echo "Card type: ", $row->card_type, "\n"; echo "Card number: ", $gpg->decrypt($row->card_number), "\n"; } ?> |
<?php require_once 'Crypt/GPG.php'; $signedData = <<<DATA -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, World! -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFIl9Sb6ZWCktsVoskRArWDAJ9D5mq6p+4JnBy11OaAhnIA+uRSSACgoM5T WcUHQ9pKf9PvNUn1Izy6c9E= =k8+7 -----END PGP SIGNATURE----- DATA; $gpg = new Crypt_GPG(); $details = $gpg->verify($signedData); if ($details->isValid()) { echo "Message is valid.\n"; } else { echo "Message is invalid!\n"; } ?> |