PHP Classes

GNU pgp: Manipulate messages and encryption keys with PGP

Recommend this page to a friend!
  Info   View files Example   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 68%Total: 3,710 All time: 893 This week: 349Up
Version License PHP version Categories
gnupgp 1.0.0Unspecified (ask ...4Cryptography
Description 

Author

This package can encrypt and decrypt messages using GnuPG cryptography.

It can take a message string and encrypt it with a given private key using the GNUPG program (gpg).

The class can also decrypt a message using the public key part of the key used to encrypt the message.

It can perform several types of operations with keys on a keyring like generate a key, list a key, import, export and remove keys from the keyring.

Picture of Rodrigo Z. Armond
Name: Rodrigo Z. Armond <contact>
Classes: 1 package by
Country: Brazil Brazil
Age: 51
All time rank: 86559 in Brazil Brazil
Week rank: 312 Up26 in Brazil Brazil Up

Recommendations

What is the best PHP gnugpg class?
Wrapper class to use with gnuGPG

Example

<?
include("gnupgp.class.php");

$gpg = new gnugpg;
$gpg->userName = $userName;
$gpg->userEmail = $userEmail;
$gpg->recipientName = $recipientName;
$gpg->recipientEmail = $recipientEmail;
$gpg->message = $message;

function
Tab_List($key_Array){
    echo
"<table border=1>";
    echo
"<tr><th>Type</th><th>Trust</th><th>Length</th><th>Algor.</th>";
    echo
"<th>KeyID</th><th>Creation</th><th>Expiration</th><th>Local ID</th>";
    echo
"<th>Ownertrust</th><th>User ID</th><th>???</th><th>???</th></tr>";
    for(
$i=2; $i < count($key_Array); $i++){
       
$tmp = explode(":",$key_Array[$i]);
        echo
"<tr>";
        echo
"<td>".$tmp[0]."</td>"; //type
       
echo "<td>".$tmp[1]."</td>"; //trust
       
echo "<td>".$tmp[2]."</td>"; //length
       
echo "<td>".$tmp[3]."</td>"; //algorithm
       
echo "<td>".$tmp[4]."</td>"; //KeyID
       
echo "<td>".$tmp[5]."</td>"; //Creation date
       
echo "<td>".$tmp[6]."</td>"; //Expiration date
       
echo "<td>".$tmp[7]."</td>"; //Local ID
       
echo "<td>".$tmp[8]."</td>"; //Ownertrust
       
echo "<td>".htmlspecialchars($tmp[9])."</td>"; //User ID
       
echo "<td>".$tmp[10]."</td>"; // ???
       
echo "<td>".$tmp[11]."</td>"; // ???
       
echo "</tr>";
        if(
$tmp[0] == "sub"){
            echo
"<tr><td colspan=\"12\">&nbsp;</td></tr>";
        }
    }
    echo
"</table>";
    echo
"<br><br>";
    echo
"<font size=-1>1. Field: Type of record<br>
        <UL>
                <LI>pub = public key
                <LI>sub = subkey (secondary key)
                <LI>sec = secret key
                <LI>ssb = secret subkey (secondary key)
                <LI>uid = user id (only field 10 is used)
                <LI>fpr = fingerprint: (fingerprint is in field 10)
                <LI>pkd = public key data (special field format, see below)
        </UL>
        </font><br>"
;
    echo
"<font size=-1>2. Field: A letter describing the calculated trust. This is a single
        letter, but be prepared that additional information may follow
        in some future versions. (not used for secret keys)<br>
            <UL>
            <LI>o = Unknown (this key is new to the system)
            <LI>d = The key has been disabled
            <LI>r = The key has been revoked
            <LI>e = The key has expired
            <LI>q = Undefined (no value assigned)
            <LI>n = Don't trust this key at all
            <LI>m = There is marginal trust in this key
            <LI>f = The key is full trusted.
            <LI>u = The key is ultimately trusted; this is only used for keys for which the secret key is also available.
        </UL>
           </font><br>"
;
    echo
"<font size=-1>3. Field: length of key in bits.</font><br><br>";
    echo
"<font size=-1>4. Field: Algorithm:<br>
        <UL>
            <LI>1 = RSA
            <LI>16 = ElGamal (encrypt only)
                   <LI>17 = DSA (sometimes called DH, sign only)
                   <LI>20 = ElGamal (sign and encrypt)
        </UL>
        </font><br>"
;
    echo
"<font size=-1>5. Field: KeyID.</font><br><br>";
    echo
"<font size=-1>6. Field: Creation Date (in UTC).</font><br><br>";
    echo
"<font size=-1>7. Field: Key expiration date or empty if none.</font><br><br>";
    echo
"<font size=-1>8. Field: Local ID: record number of the dir record in the trustdb.
        This value is only valid as long as the trustdb is not
        deleted. You can use \"#<local-id> as the user id when
        specifying a key. This is needed because keyids may not be
        unique - a program may use this number to access keys later.</font><br><br>"
;
    echo
"<font size=-1> 9. Field: Ownertrust (primary public keys only)
        This is a single letter, but be prepared that additional
        information may follow in some future versions.</font><br><br>"
;
    echo
"<font size=-1>10. Field: User-ID. The value is quoted like a C string to avoid
        control characters (the colon is quoted \"\x3a\").</font><br><br>"
;
    echo
"<font size=-1>11. Field: ????.</font><br><br>";
    echo
"<font size=-1>12. Field: ????.</font><br><br>";
}

switch (
$action){
    case
"gen_key":
       
$result = $gpg->gen_key($userName, $comment, $userEmail, $passphrase);
        if(!
$result){
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>The key was generated sucessful.</h3>";
        }
        break;
    case
"list_key":
       
$result = $gpg->list_keys();
        if(!
$result){
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>This is the keys on the <font color=red>".$gpg->userName."</font>'s keyring</h3><br>";
           
Tab_List($gpg->keyArray);
        }
        break;
    case
"export_key":
       
$result = $gpg->export_key();
        if(!
$result){
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>This is the <font color=red>".$gpg->userEmail."</font>'s Public Key</h3><br>";
            echo
"<form><TEXTAREA rows=\"30\" cols=\"80\">".$gpg->public_key."</TEXTAREA>";
        }
        break;
    case
"import_key":
       
$result = $gpg->import_key($key);
        if(!
$result){
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>The keys was imported successful.</h3><br>";
                   
$result = $gpg->list_keys();
            if(!
$result){
                echo
$gpg->error;
                exit();
            } else {
                echo
"<h3>This is the keys on the <font color=red>".$gpg->userEmail."</font>'s keyring</h3><br>";
               
Tab_List($gpg->keyArray);
            }
        }
        break;
    case
"remove_key":
        if(!empty(
$keyID)){
           
$key = $keyID;
        } elseif (!empty(
$emailID)){
               
$key = $emailID;
            } else {
               
$key = $nameID;
                }

       
$result = $gpg->remove_key($key);
        if(!
$result){
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>The key was successful removed.</h3><br>";
           
$result = $gpg->list_keys();
            if(!
$result){
                echo
$gpg->error;
                exit();
            } else {
                echo
"<h3>This is the keys on the <font color=red>".$gpg->userEmail."</font>'s keyring</h3><br>";
               
Tab_List($gpg->keyArray);
            }
        }
    case
"encrypt_Msg":
        if(empty(
$userEmail)){
            echo
"The \"From User:\" can't be empty!";
            exit();
        }
        if(empty(
$recipientEmail)){
            echo
"The \"To Email:\" can't be empty!";
            exit();
        }
       
$result = $gpg->encrypt_message();
        if (!
$result) {
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>The message was successful encrypted!</h3><br>";
            echo
"<form><TEXTAREA rows=\"20\" cols=\"80\">".$gpg->encrypted_message."</TEXTAREA></form>";
        }
        break;
    case
"decrypt_Msg":
        if(empty(
$userEmail)){
            echo
"The \"Name\" can't be empty!";
            exit();
        }
        if(empty(
$passphrase)){
            echo
"The \"Passphrase\" can't be empty!";
            exit();
        }
        if(empty(
$message)){
            echo
"The \"Message\" can't be empty!";
            exit();
        }
       
$result = $gpg->decrypt_message($message, $passphrase);
        if (!
$result) {
            echo
$gpg->error;
            exit();
        } else {
            echo
"<h3>The message was successful decrypted!</h3><br>";
            echo
"<form><TEXTAREA rows=\"20\" cols=\"80\">".$gpg->decrypted_message."</TEXTAREA></form>";
        }
        break;







}
// end switch $action

?>


Details

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 gnupgp.class.php - v0.2 Author: Rodrigo Z. Armond Email: rodzadra@yahoo.com ============================================= Este software pode ser usado SEM NENHUMA GARANTIA POR PARTE DO AUTOR. (LEIA LICENCA.txt ANTES DE USÁ-LO) This can be used WITHOUT ANY WARRANTY BY THE AUTHOR. (SEE LICENCA.txt BEFORE YOU USE THIS SOFTWARE) COMEÇO: ======= Para começar, descomprima este arquivo em um diretório acessível pelo servidor html. Edite o arquivo gnupgp.class.php e mude as variáveis GPG_BIN, GPG_USER_DIR de acordo com o seu sistema. depois, abra seu navegador em http://seu_servidor/<diretório_de_instalação>/index.html Para fazer um teste local, vc precisa gerar no mínimo 2 chaves. Clique em Gen Key no menu, preencha o formulário e mande gerar a chave. (aguarde a geração da chave). A geração da chave demora um pouco (principalmente se ela estiver em um servidor remoto), não se preocupe. aguarde a completa geração. Após confirmado q a chave foi gerada sem problemas, cadastre (gere) novamente uma outra chave diferente. Se tudo ocorrer bem, tudo está ok! :-) Se quiser ver quais chaves estão no seu "chaveiro eletrônico" (keyring), clique em List Key (no menu) e entre o email. Deverá aparece um tabela com as chaves cadastradas em seu "chaveiro eletrônico". etc, etc, etc... To start, decompress this file in a dir where the http server was access. Edit the gnupgp.class.php and change the GPG_BIN, GPG_USER_DIR to reflect your system. after, open your browser in http://your_host/<the_install_dir>/index.html To make an local test, you need to generate a minimum of 2 keys. Click in Gen Key, at menu, fill the form and click in the gen_key button. (wait to gen the key). The key geration is a slow process (principaly if this is on a remot server), dont worry. wait the complet key geration. After, the confirmation, make this process again with one other key different. If all its ok, its all ok! :-) If you need to see what keys are in your keyring, click in the List Key (in the menu) and anter the email. This will show an table with the all keys in the keyring. etc, etc, etc... métodos da classe gnupgp: ========================= function check_private_dir() Esta função checa se o diretório privado .gnupg do usuário existe. This function check if the private .gnupg dir exist function check_pubring() Esta função checa se o arquivo pubring.gpg existe no diretório privado (.gnupg) do usuário. This function check if the pubring.gpg file exist in the private .gnupg dir function check_all() Esta função faz o uso das 2 funções acima (check_private_dir and check_pubring) This function make the use of the 2 function above (check_private_dir and check_pubring) function mount_recipients($recipients) Esta função checa se a chave pública de quem irá a mensagem (recipient) está no seu "chaveiro eletrônico", - variável $recipients pode ser um único email ou vários separados por ";" (ponto-e-vírgula) - e retorna os recipientes válidos nas variáveis $this->valid_keys e as não válidas em $this->not_valid_keys. (recipientes válidos são aqueles q vc possue a chave pública). This function check if the recipients are in the keyring ($recipients can be one email or more separeted by ';') and returns the valids recipients in the $this->valid_keys var and the not valids recipients in the $this->not_valid_keys var. (valid recipients are that on the keyring of the user). function check_keyID($keyID) Esta função checa se a chave (conteúdo da variável $keyID) está em seu chaveiro eletrônico e retorna verdadeiro (true) se estiver. Se a chave ($keyID) não estiver retorna falso (false). A variável $keyID, pode ser uma simples chave (nome, email, ou qualquer outro identificador válido) ou várias chaves separadas por ponto-e-vírgula (";"). This function check if the $keyID is on the keyring of the user. And return true if yes. If isn't on the keyring return false. $keyID can be a simple key or various separeded with ";". function list_keys() Esta função preenche a variável $this->keyArray (do tipo array) com todas as chaves publicas de seu chaveiro eletrônico. Retorna verdadeiro (true) se tudo ocorrer bem, ou falso (false) se der algum problema. This function populate an array ($this->keyArray) with all pub keys of the user keyring. Return true if all runs good or false for an error. function encrypt_message() Esta função encripta a mensagem e retorna verdadeiro (true) se tudo ocorrer bem. A mensagem encriptada pode ser acessada pela variável $this->encrypted_message. This function encrypts the message and return true if all its ok. The encrypted message will be in the $this->encrypted_message var. function decrypt_message() Esta função decripta a mensagem e retorna verdadeiro (true) se tudo ocorrer bem. A mensagem decriptada pode ser acessada pela variável $this->decrypted_message. This function decrypts the message and return true if all its ok. The decrypted message will be in the $this->decrypted_message var. function import_key($key) Esta função importa uma chave pública para o seu chaveiro eletrônico. A variável $key tem q ser do tipo ASC. Se tudo ocorrer bem, retorna verdadeiro (true) ou falso (false) se o contrário. This function import a public key to the users keyring. If all its ok this return true. function export_key() Esta função exporta a chave pública do usuário. Se tudo ocorrer bem retorna verdadeiro (true) ou falso (false) se o contrário. A chave pública exportada pode ser acessada pela variável $this->public_key. This function export the user public key. If all its ok this return true. The exported public key will be in the $this->public_key var. function remove_key($key) Esta função remove um única chave do seu chaveiro eletrônico. Retorna verdadeiro ou falso. This function removes one key from the user keyrings. return true if all its ok. function gen_key($userName, $comment="", $userEmail, $passphrase) Esta função gera uma nova chave (para um novo usuário). $userName - é o nome do usuário. $comment - é um comentário q é adicionado na chave (não é obrigatório). $userEmail - é o email do usuário. $passphrase - é a senha q protegerá toda a encriptação. se tudo estiver ok, retorna verdadeiro. This function gen a new user key. $userName - is the user name. $comment - this is an comment that is appended after the user name (this can be empty) $userEmail - this is the user email $passphrase - this is the password used to encrypt all the user message if all its ok, this return true. RmdirR($userPath) Esta função remove o diretório privado do usuário (uso interno). This is an function to remove an user private path (internal) Todas as funções retornam verdadeiro (true) se ocorrer tudo bem, caso contrário, se alguma coisa falhar, retorna falso (false). Se alguma função retornar falso, o erro pode ser lido na variável $This->error. All the functions return true when all its ok, or false when somethinks not go on. If the function return false use the $this->error to see why. Eu não sei o q é pior, se meu inglês ou meu português, mas de qualquer forma qualquer correção é bem vinda! 8-) I don't know what is more bad, if my english or my portuguese but, any way, any correction is wellcome! 8-) ESTE SOFTWARE PODE SER EXPORTADO PARA O IRAQUE, CUBA, CHINA E QUALQUER OUTRO PAÍS O QUAL OS EUA USUALMENTE INTERDITA. ^^^^^^^^ THIS SOFTWARE CAN BE EXPORTED TO IRAK, CUBA, CHINA AND ANY COUNTRY THAT THE EUA USUALLY INTERDIT. ^^^^^^ BUG(s): ======= FUSQUINHA AMARELO PINTADO DE VERDE, COM UM CARANGUEIJO DENTRO DA ALAVANCA DE MARCHA E UMA IMAGEM DE NOSSA SENHORA NO PAINEL. ;-) Histórico: ========== GNUpgp PHP Class - by Rodrigo Z. Armond <rodzadra@passagemdemariana.com.br> ver 0.2 - A segunda versão. Básicamente, esta versão remove as mensagens dos logs HTTPD (logs de erro e de acesso). Nesta versão forão adicionados 2 novas opções de configurações FAIL_NO_RECIPIENT e GEN_HTTP_LOG, veja o arquivo gnupgp.class.php. Adicionado method="post" nos arquivos de exemplo para evitar mensagens no log do servidor Http. Algumas modificações menores no código. A criação deste histórico. (acho q ninguém lê isso! mas "voilá".) The second version This version, basicaly, remove messages from the HTTPD log file (error and access). This version was added 2 more config options, FAIL_NO_RECIPIENT and GEN_HTTP_LOG, see the gnupgp.class.php file. Was added the method="post" in the examples file to evite the httpd logs. Some minor modifications on the code. The creation of this historic. (some one read this??). ver 0.1a - A primeira versão. Esta versão pode Gerar, Listar, Importar, Exportar, Remover chaves públicas, encriptar e decriptar mensagens. The first version. This version can Gen, List, Import, Export, Remove public keys, encrypt and decrypt messages. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD4DBQE78Yw3K2GYoRRzSjYRAoceAJdg4JyKrCg7OpCLso/zjvTFM2tuAKCgiAPh bsgJhD/HXYTu96vIdTHWAQ== =cDn5 -----END PGP SIGNATURE-----

  Files folder image Files  
File Role Description
Accessible without login Plain text file 8859-1.TXT Data This is the ISO-8859-1 tab
Accessible without login Plain text file config.php Conf. the GNUpgp config class
Accessible without login HTML file decrypt_Msg.html Data This is the decrypt message function example
Accessible without login HTML file encrypt_Msg.html Data This is the encrypt message example
Accessible without login HTML file export_Key.html Data This is the export key function example
Accessible without login HTML file gen_Key.html Data This is the gen key function example
Plain text file gnupgp.class.php Class The gnupgp.class.php
Accessible without login HTML file import_Key.html Data This is the example of the import key function
Accessible without login HTML file index.html Data This is the index.html of the example application
Accessible without login Plain text file LICENCA.txt Lic. This is the licence for the gnupgp.class
Accessible without login HTML file list_Key.html Data The list key function of the example application
Accessible without login HTML file menu.html Data This is menu of the example application
Accessible without login Plain text file process.php Example This is the file to process the examples forms
Accessible without login Plain text file publickey.asc Data My public key.
Accessible without login Plain text file README Doc. The gnupgp.class.php readme file
Accessible without login HTML file remove_Key.html Data This is the remove key example file
Plain text file utf8.php Class This is an Unicode class used by the gnupgp.class.php

 Version Control Unique User Downloads Download Rankings  
 0%
Total:3,710
This week:0
All time:893
This week:349Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:87%StarStarStarStarStar
Documentation:63%StarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:68%StarStarStarStar
Rank:403