Help with OpenSSL request using Python
- by Ldn
Hi
i'm creating a program that has to make a request and then obtain some info. For doing that the website had done some API that i will use.
There is an how-to about these API but every example is made using PHP.
But my app is done using Python so i need to convert the code.
here is the how-to:
The request string is sealed with OpenSSL. The steps for sealing are as follows:
• Random 128-bit key is created.
• Random key is used to RSA-RC4 symettrically encrypt the request string.
• Random key is encrypted with the public key using OpenSSL RSA asymmetrical encryption.
• The encrypted request and encrypted key are each base64 encoded and placed in the appropriate fields. In PHP a full request to our API can be accomplished like so:
<?php // initial request.
$request = array('object' => 'Link',
'action' => 'get',
'args' => array(
'app_id' => 303612602
)
);
// encode the request in JSON
$request = json_encode($request);
// when you receive your profile, you will be given a public key to seal your request in.
$key_pem = "-----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALdu5C6d2sA1Lu71NNGBEbLD6DjwhFQO VLdFAJf2rOH63rG/L78lrQjwMLZOeHEHqjaiUwCr8NVTcVrebu6ylIECAwEAAQ== -----END PUBLIC KEY-----";
// load the public key
$pkey = openssl_pkey_get_public($key_pem);
// seal! $newrequest and $enc_keys are passed by reference.
openssl_seal($request, $enc_request, $enc_keys, array($pkey));
// then wrap the request
$wrapper = array( 'profile' => 'ProfileName',
'format' => 'RSA_RC4_Sealed',
'enc_key' => base64_encode($enc_keys[0]),
'request' => base64_encode($enc_request)
);
// json encode the wrapper. urlencode it as well.
$wrapper = urlencode(json_encode($wrapper));
// we can send the request wrapper via the cURL extension
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.site.com/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=$wrapper");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
?>
Of all of that, i was able to convert "$request" and i'v also made the JSON encode.
This is my code:
import urllib
import urllib2
import json
url = 'http://api.site.com/'
array = {'app_id' : "303612602"}
values = {
"object" : "Link",
"action": "get",
"args" : array
}
data = urllib.urlencode(values)
json_data = json.dumps(data)
What stop me is the sealing with OpenSSL and the publi key (that obviously i have)
Using PHP OpenSSL it's so easy, but in Python i don't really know how to use it
Please, help me!