Linux-python rewrite php code

Source: Internet
Author: User
Tags pkcs7
The pkcs7 encryption in python seems to be implemented differently, and the results are completely different. The php code is as follows {code...}. How should I write this method using python?
  1. The pkcs7 encryption in python seems to have different implementations, and the results are completely different.

  2. The php code 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"; // Save the signed base64 File $ signedDataFile = ". /rsa /". $ Millisecond. "signedData.txt"; // Save the envelope File $ envelopeFile = ". /rsa /". $ Millisecond. "envelope.txt"; # Load p12 openssl_pkcs12_read (file_get_contents (Contants: pri), $ certs, Contants: passWord); $ signCert = $ certs ['cert']; $ signKey = $ certs ['pkey']; # load the encryption certificate $ encryCert = file_get_contents (Contants: pub); # encrypt the original text $ fp = fopen ($ dataFile, "w"); fwrite ($ fp, $ data); fclose ($ fp); # sign openssl_pkcs7_sign ($ dataFile, $ signedFile, $ signCert, array ($ signKey, ""), NULL, bytes | PKCS7_BINARY | bytes); $ signedBase64 = bytes ($ signedFile); $ signedBase64 = substr ($ signedBase64, strpos ($ signedBase64, "base64 ") + strlen ("base64"); trim ($ signedBase64); # print_r ($ signedBase64); # echo"


    "; $ SignedData = base64_decode ($ signedBase64); $ fp = fopen ($ signedDataFile," w "); fwrite ($ fp, $ signedData); fclose ($ fp ); # envelope encryption ($ signedDataFile, $ envelopeFile, $ encryCert, NULL, PKCS7_BINARY, encoding); $ encryption = encoding ($ envelopeFile); $ envelopeBase64 = substr ($ envelopeBase64, strpos ($ envelopeBase64, "base64") + strlen ("base64"); trim ($ envelopeBase64); $ envelopeBase64 = base64_decode ($ envelopeBase64 ); $ response = base64_encode ($ envelopeBase64); // $ envelopeBase64 = str_replace ("\ r \ n", "", $ envelopeBase64); // print_r ($ envelopeBase64 ); // Delete the unlink ($ dataFile) file created during encryption; unlink ($ signedFile); unlink ($ signedDataFile); unlink ($ envelopeFile); return $ envelopeBase64;

    }

How should I write this method in python?

Reply content:
  1. The pkcs7 encryption in python seems to have different implementations, and the results are completely different.

  2. The php code 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"; // Save the signed base64 File $ signedDataFile = ". /rsa /". $ Millisecond. "signedData.txt"; // Save the envelope File $ envelopeFile = ". /rsa /". $ Millisecond. "envelope.txt"; # Load p12 openssl_pkcs12_read (file_get_contents (Contants: pri), $ certs, Contants: passWord); $ signCert = $ certs ['cert']; $ signKey = $ certs ['pkey']; # load the encryption certificate $ encryCert = file_get_contents (Contants: pub); # encrypt the original text $ fp = fopen ($ dataFile, "w"); fwrite ($ fp, $ data); fclose ($ fp); # sign openssl_pkcs7_sign ($ dataFile, $ signedFile, $ signCert, array ($ signKey, ""), NULL, bytes | PKCS7_BINARY | bytes); $ signedBase64 = bytes ($ signedFile); $ signedBase64 = substr ($ signedBase64, strpos ($ signedBase64, "base64 ") + strlen ("base64"); trim ($ signedBase64); # print_r ($ signedBase64); # echo"


    "; $ SignedData = base64_decode ($ signedBase64); $ fp = fopen ($ signedDataFile," w "); fwrite ($ fp, $ signedData); fclose ($ fp ); # envelope encryption ($ signedDataFile, $ envelopeFile, $ encryCert, NULL, PKCS7_BINARY, encoding); $ encryption = encoding ($ envelopeFile); $ envelopeBase64 = substr ($ envelopeBase64, strpos ($ envelopeBase64, "base64") + strlen ("base64"); trim ($ envelopeBase64); $ envelopeBase64 = base64_decode ($ envelopeBase64 ); $ response = base64_encode ($ envelopeBase64); // $ envelopeBase64 = str_replace ("\ r \ n", "", $ envelopeBase64); // print_r ($ envelopeBase64 ); // Delete the unlink ($ dataFile) file created during encryption; unlink ($ signedFile); unlink ($ signedDataFile); unlink ($ envelopeFile); return $ envelopeBase64;

    }

How should I write this method in python?

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

You just need to rewrite it. You cannot find it.

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.