2010-03-15 81 views
0
import gnupg, urllib 

retk = urllib.urlopen("http://keyserver.pramberger.at/pks/" 
"lookup?op=get&search=userid for the key is required") 
pub_key = retk.read() 
#print pub_key 
gpg = gnupg.GPG(gnupghome="/tmp/foldername", verbose=True) 
print "Import the Key :", gpg.import_keys(pub_key).summary() 
print "Encrypt the Message:" 
msg = "Hellllllllllo" 
uid = "userid that has the key on public key server" 
enc = gpg.encrypt(msg, uid,always_trust=True) 
print "*The enc content***************************== ", enc 

用python编写的这个函数给了我加密的消息。加密是使用我从公钥服务器(pramberger.at)获得的公钥完成的。加密解密功能在php

现在我怎么能在php中实现相同的功能(从任何公钥服务器获取密钥并使用该密钥加密消息)。

回答

0

看一看mcrypt这里:PHP.NET Mcrypt

教程如下:

Code example使用这个Mcrypt:

<?php 

/* 
* phpFreaksCrypto.class.php4 -> phpFreaksCrypto Class (PHP4) 
*/ 

/** 
    * @author Dustin Whittle 
    * @version 0.01 
    */ 

if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) 
{ 
    // tell people trying to access this file directly goodbye... 
    exit('This file can not be accessed directly...'); 
} 

class phpFreaksCrypto 
{ 

    var $td; 

    // this gets called when class is instantiated 
    function phpFreaksCrypto($key = 'MyRandomStringThatWillAlwaysBeTheSame', $iv = false, $algorithm = 'tripledes', $mode = 'ecb') 
    { 

    if(extension_loaded('mcrypt') === FALSE) 
    { 
     $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : ''; 
     dl($prefix . 'mcrypt.' . PHP_SHLIB_SUFFIX) or die('The Mcrypt module could not be loaded.'); 
    } 

    if($mode != 'ecb' && $iv === false) 
    { 
     /* 
     the iv must remain the same from encryption to decryption and is usually 
     passed into the encrypted string in some form, but not always. 
     */ 
     die('In order to use encryption modes other then ecb, you must specify a unique and consistent initialization vector.'); 
    } 

    // set mcrypt mode and cipher 
    $this->td = mcrypt_module_open($algorithm, '', $mode, '') ; 

    // Unix has better pseudo random number generator then mcrypt, so if it is available lets use it! 
    $random_seed = strstr(PHP_OS, "WIN") ? MCRYPT_RAND : MCRYPT_DEV_RANDOM; 

    // if initialization vector set in constructor use it else, generate from random seed 
    $iv = ($iv === false) ? mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), $random_seed) : substr($iv, 0, mcrypt_enc_get_iv_size($this->td)); 

    // get the expected key size based on mode and cipher 
    $expected_key_size = mcrypt_enc_get_key_size($this->td); 

    // we dont need to know the real key, we just need to be able to confirm a hashed version 
    $key = substr(md5($key), 0, $expected_key_size); 

    // initialize mcrypt library with mode/cipher, encryption key, and random initialization vector 
    mcrypt_generic_init($this->td, $key, $iv); 
    } 

    function encrypt($plain_string) 
    { 
    /* 
     encrypt string using mcrypt and then encode any special characters 
     and then return the encrypted string 
    */ 
    return base64_encode(mcrypt_generic($this->td, $plain_string)); 
    } 

    function decrypt($encrypted_string) 
    { 
    /* 
     remove any special characters then decrypt string using mcrypt and then trim null padding 
     and then finally return the encrypted string 
    */ 
    return trim(mdecrypt_generic($this->td, base64_decode($encrypted_string))); 
    } 

    // since php 4 does not have deconstructors, we will need to manually call this function 
    function __destruct() 
    { 
    // shutdown mcrypt 
    mcrypt_generic_deinit($this->td); 

    // close mcrypt cipher module 
    mcrypt_module_close($this->td); 
    } 

} 
?> 
0

PHP的等效值应该是GnuPG extension。查看gnupg_encrypt()和gnupg_decrypt()的手册。