chenglei
chenglei

Reputation: 11

How to use PHP PKCS7 encrypt decrypt in JAVA

I'm using JAVA for the 2c2p(https://developer.2c2p.com/docs/status-inquiry) API, but they're using PHP, and I don't know how to implement the openssl_pkcs7_encrypt method of PHP in JAVA

I searched for the implementation of node.js(PKCS7 encrypt decrypt in Node.js), but I couldn't find the Java implementation

This is a PHP code example provided by 2c2p

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;
    }
}

Upvotes: 1

Views: 511

Answers (1)

Prajwal K Gowda
Prajwal K Gowda

Reputation: 143

Hello you can download complete code for 2c2p status-inquiry which uses pkcs7 encryption and decryption from this link https://support.2c2p.com/attachments/token/OaGZTrZWkqVMlHlhhpo3pBT2Y/?name=JAVA.rar

Upvotes: 1

Related Questions