Jump to content

How do I encrypt text with openssl using DES3?


shedokan

Recommended Posts

Hi,

I'm trying to encrypt a string so that paypal can read it.

I need somehow to encrypt a string and the output will look like:

-----BEGIN PKCS7----- Encrypted string -----END PKCS7-----

 

I already have the public cert and the private key but I can't find the right function.

I tried openssl_pkcs7_encrypt but it's too complicated and I need some headers...

 

What function do I need to use and how?

 

Thanks.

I googled 'php encrypt for paypal' and found an article with this function.

 

function paypal_encrypt($hash)
{
//Sample PayPal Button Encryption: Copyright 2006,2007 StellarWebSolutions.com
//Not for resale - license agreement at
//http://www.stellarwebsolutions.com/en/eula.php

global $MY_KEY_FILE;
global $MY_CERT_FILE;
global $PAYPAL_CERT_FILE;
global $OPENSSL;

if (!file_exists($MY_KEY_FILE)) {
	echo "ERROR: MY_KEY_FILE $MY_KEY_FILE not found\n";
}
if (!file_exists($MY_CERT_FILE)) {
	echo "ERROR: MY_CERT_FILE $MY_CERT_FILE not found\n";
}
if (!file_exists($PAYPAL_CERT_FILE)) {
	echo "ERROR: PAYPAL_CERT_FILE $PAYPAL_CERT_FILE not found\n";
}
if (!file_exists($OPENSSL)) {
	echo "ERROR: OPENSSL $OPENSSL not found\n";
}

//Assign Build Notation for PayPal Support
$hash['bn']= 'StellarWebSolutions.PHP_EWP';

$openssl_cmd = "$OPENSSL smime -sign -signer $MY_CERT_FILE -inkey $MY_KEY_FILE " .
                "-outform der -nodetach -binary | $OPENSSL smime -encrypt " .
                "-des3 -binary -outform pem $PAYPAL_CERT_FILE";

$descriptors = array(
        	0 => array("pipe", "r"),
	1 => array("pipe", "w"),
);

$process = proc_open($openssl_cmd, $descriptors, $pipes);
    
if (is_resource($process)) {
	foreach ($hash as $key => $value) {
		if ($value != "") {
			//echo "Adding to blob: $key=$value\n";
			fwrite($pipes[0], "$key=$value\n");
		}
	}
	fflush($pipes[0]);
        	fclose($pipes[0]);

	$output = "";
	while (!feof($pipes[1])) {
		$output .= fgets($pipes[1]);
	}
	//echo $output;
	fclose($pipes[1]); 
	$return_value = proc_close($process);
	return $output;
}
return "ERROR";
};

Thanks for trying but I already found:

http://dev.juokaz.com/php/paypal-payment-with-encryption

 

which led me to:

function getEncrypted($data){
$cert_path = $path.'certs/shedokan-pubcert.pem';
$prvkey_path = $path.'certs/shedokan-prvkey.pem';
$paypal_cert_path = $path.'certs/paypal_cert.pem';

$cert = openssl_x509_read('file://'.$cert_path);
$prvkey = openssl_get_privatekey('file://'.$prvkey_path);
$paypal_cert = openssl_x509_read('file://'.$paypal_cert_path);

$dataFile = tempnam(sys_get_temp_dir(), 'clear_');
$signedFile = preg_replace('/clear/', 'signed', $dataFile);

$out = fopen($dataFile, 'wb');
fwrite($out, $dataFile);
fclose($out);

if(openssl_pkcs7_sign($dataFile, $signedFile, $cert, $prvkey, array(), PKCS7_BINARY)) {
	$signedData = explode("\n\n", file_get_contents($signedFile));

	$out = fopen($signedFile, 'wb');
	fwrite($out, base64_decode($signedData[1]));
	fclose($out);

	$encryptedFile = preg_replace('/clear/', 'encrypted', $dataFile);
	if (openssl_pkcs7_encrypt($signedFile, $encryptedFile, $paypal_cert, array(), PKCS7_BINARY)) {
		$encryptedData = explode("\n\n", file_get_contents($encryptedFile));

		return $encryptedData[1];
	} else {
		return false;
		//echo 'Could not encrypt with paypal\'s certificate';
	}
} else {
	return false;
	//echo 'Could not encrypt with my certificate';
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.