0

Hi I am using python for integrating 2c2p payment gateway. They are using php in their documentation(https://developer.2c2p.com/docs/status-inquiry). I searched for corresponding packages in python to implement the same but I could not find proper python implementation. Is there any substitute for openssl_pkcs7_encrypt in python

This is the php example given in their documentation


Class pkcs7
{
    function encrypt($text,$publickey)
    {
        //write text to file
        if(!file_exists( dirname(__FILE__)."/tmp/"))
        {
            mkdir( dirname(__FILE__)."/tmp/");
        }
        $filename = dirname(__FILE__)."/tmp/".time().".txt";
        $this->text_to_file($text,$filename);
        $filename_enc = dirname(__FILE__)."/tmp/".time().".enc";

        $key = file_get_contents($publickey);
        if (openssl_pkcs7_encrypt($filename, $filename_enc, $key,
        array())) {
            // message encrypted - send it!
            unlink($filename);
            if (!$handle = fopen($filename_enc, 'r')) {
                     echo "Cannot open file ($filename_enc)";
                     exit;
                }

                $contents = fread($handle, filesize($filename_enc));
                fclose($handle);
                $contents = str_replace("MIME-Version: 1.0
Content-Disposition: attachment; filename=\"smime.p7m\"
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"
Content-Transfer-Encoding: base64
","",$contents);
                $contents = str_replace("\n","",$contents);
                unlink($filename_enc);
                return $contents;
        }
    }

    function decrypt($text,$publickey,$privatekey,$password)
    {
        $arr = str_split($text,64);
        $text = "";
        foreach($arr as $val)
        {
            $text .= $val."\n";
        }

        $text = "MIME-Version: 1.0
Content-Disposition: attachment; filename=\"smime.p7m\"
Content-Type: application/pkcs7-mime; smime-type=enveloped-data; name=\"smime.p7m\"
Content-Transfer-Encoding: base64

".$text;
        $text = rtrim($text,"\n");
        if(!file_exists( dirname(__FILE__)."/tmp/"))
        {
            mkdir( dirname(__FILE__)."/tmp/");
        }

        $infilename = dirname(__FILE__)."/tmp/".time().".txt";
        $this->text_to_file($text,$infilename);
                // echo $infilename;exit;
        $outfilename = dirname(__FILE__)."/tmp/".time().".dec";

        $public = file_get_contents($publickey);

        $private = array(file_get_contents($privatekey), $password);

        if (openssl_pkcs7_decrypt($infilename, $outfilename, $public, $private)) {
            unlink($infilename);
            $content = file_get_contents($outfilename);
            unlink($outfilename);
            return $content;
        }
        else{
            unlink($outfilename);
            unlink($infilename);
            echo "DECRYPT FAIL";exit;
        }
    }

    private function text_to_file($text,$filename)
    {
        if (!$handle = fopen($filename, 'w')) {
                 echo "Cannot open file ($filename)";
                 exit;
            }

            // Write $somecontent to our opened file.
            if (fwrite($handle, $text) === FALSE) {
                echo "Cannot write to file ($filename)";
                exit;
            }
    }
} ```

Prajwal K Gowda
  • 133
  • 1
  • 5
  • The pkcs7 class isn't a standard PHP class, so you have to track down that pkcs7.php file and take a look inside. – Peter May 15 '20 at 14:52
  • On the page provided by the OP, towards the top right is a download link that includes two required files that gets included. – Chris Haas May 15 '20 at 17:55
  • @Peter Maybe it's this one? https://github.com/blar/openssl/blob/master/lib/Blar/OpenSSL/Pkcs7.php – Isolin May 15 '20 at 21:57
  • This looks like a helpful answer for parsing pkcs7 in Python https://stackoverflow.com/questions/45782506/is-there-any-python-package-for-parsing-pkcs7 The rest is just a post request. – Isolin May 15 '20 at 22:00
  • @Peter I changed the question. Can u help me now. – Prajwal K Gowda May 20 '20 at 07:02

0 Answers0