Linux-python the code of the replication PHP

Source: Internet
Author: User
Tags pkcs7
  1. Python's PKCS7 encryption seems to be different, and the results are completely different.

  2. The code for PHP is as follows

       public static function Envelope ($data) {$Millisecond = Signenvelope::getmillisecond (); Save the source data file $dataFile = "./rsa/". $Millisecond. "   Data.txt "; Save the signature file $signedFile = "./rsa/". $Millisecond. "   Signed.txt "; After saving the signature base64 file $signedDataFile = "./rsa/". $Millisecond. "   SignedData.txt "; File $envelopeFile = "./rsa/" after saving the envelope. $Millisecond. "   Envelope.txt ";   #加载p12 Openssl_pkcs12_read (file_get_contents (contants::p ri), $certs, contants::p assword);   $signCert = $certs [' cert '];   $signKey = $certs [' Pkey '];   #加载加密证书 $encryCert = file_get_contents (contants::p UB);   #加密原文 $fp = fopen ($dataFile, "w");   Fwrite ($fp, $data);   Fclose ($FP); #签名 openssl_pkcs7_sign ($dataFile, $signedFile, $signCert, Array ($signKey, ""), NULL, pkcs7_noattr| pkcs7_binary|   Pkcs7_nosigs);   $signedBase = file_get_contents ($signedFile);   $signedBase = substr ($signedBase, Strpos ($signedBase, "base64") + strlen ("base64"));   Trim ($signedBase 64);   #print_r ($signedBase 64); #ecHo " 


    "; $signedData = Base64_decode ($signedBase 64); $fp = fopen ($signedDataFile, "w"); Fwrite ($fp, $signedData); Fclose ($FP); #信封 Openssl_pkcs7_encrypt ($signedDataFile, $envelopeFile, $encryCert, NULL, Pkcs7_binary, OPENS Sl_cipher_3des); $envelopeBase = file_get_contents ($envelopeFile); $envelopeBase = substr ($envelopeBase, Strpos ($envelopeBase, "base64") + strlen ("base64")); Trim ($envelopeBase 64); $envelopeBase = Base64_decode ($envelopeBase 64); $envelopeBase = Base64_encode ($envelopeBase 64); $envelopeBase = Str_replace ("\ r \ n", "", $envelopeBase 64); Print_r ($envelopeBase 64); Delete files created during encryption unlink ($dataFile); Unlink ($signedFile); Unlink ($signedDataFile); Unlink ($envelopeFile); return $envelopeBase 64;

    }

What should I do if I want to implement this method in Python?

Reply content:

  1. Python's PKCS7 encryption seems to be different, and the results are completely different.

  2. The code for PHP is as follows

       public static function Envelope ($data) {$Millisecond = Signenvelope::getmillisecond (); Save the source data file $dataFile = "./rsa/". $Millisecond. "   Data.txt "; Save the signature file $signedFile = "./rsa/". $Millisecond. "   Signed.txt "; After saving the signature base64 file $signedDataFile = "./rsa/". $Millisecond. "   SignedData.txt "; File $envelopeFile = "./rsa/" after saving the envelope. $Millisecond. "   Envelope.txt ";   #加载p12 Openssl_pkcs12_read (file_get_contents (contants::p ri), $certs, contants::p assword);   $signCert = $certs [' cert '];   $signKey = $certs [' Pkey '];   #加载加密证书 $encryCert = file_get_contents (contants::p UB);   #加密原文 $fp = fopen ($dataFile, "w");   Fwrite ($fp, $data);   Fclose ($FP); #签名 openssl_pkcs7_sign ($dataFile, $signedFile, $signCert, Array ($signKey, ""), NULL, pkcs7_noattr| pkcs7_binary|   Pkcs7_nosigs);   $signedBase = file_get_contents ($signedFile);   $signedBase = substr ($signedBase, Strpos ($signedBase, "base64") + strlen ("base64"));   Trim ($signedBase 64);   #print_r ($signedBase 64); #ecHo " 


    "; $signedData = Base64_decode ($signedBase 64); $fp = fopen ($signedDataFile, "w"); Fwrite ($fp, $signedData); Fclose ($FP); #信封 Openssl_pkcs7_encrypt ($signedDataFile, $envelopeFile, $encryCert, NULL, Pkcs7_binary, OPENS Sl_cipher_3des); $envelopeBase = file_get_contents ($envelopeFile); $envelopeBase = substr ($envelopeBase, Strpos ($envelopeBase, "base64") + strlen ("base64")); Trim ($envelopeBase 64); $envelopeBase = Base64_decode ($envelopeBase 64); $envelopeBase = Base64_encode ($envelopeBase 64); $envelopeBase = Str_replace ("\ r \ n", "", $envelopeBase 64); Print_r ($envelopeBase 64); Delete files created during encryption unlink ($dataFile); Unlink ($signedFile); Unlink ($signedDataFile); Unlink ($envelopeFile); return $envelopeBase 64;

    }

What should I do if I want to implement this method in Python?

Do you need someone to rewrite the code for you or this: http://www.php2python.com/?

It's just a rewrite, not a search.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.