python裡面的pkcs7加密好像實現不一樣,得出的結果完全不一樣
php的代碼如下
public static function envelope($data){ $Millisecond = SignEnvelope::getMillisecond(); //儲存來源資料檔案 $dataFile = "./rsa/".$Millisecond."data.txt"; //儲存簽名檔案 $signedFile = "./rsa/".$Millisecond."signed.txt"; //儲存簽名後base64檔案 $signedDataFile = "./rsa/".$Millisecond."signedData.txt"; //儲存信封后檔案 $envelopeFile = "./rsa/".$Millisecond."envelope.txt"; #載入p12 openssl_pkcs12_read (file_get_contents(Contants::pri), $certs, Contants::passWord); $signCert = $certs ['cert']; $signKey = $certs['pkey']; #載入加密認證 $encryCert = file_get_contents(Contants::pub); #加密原文 $fp = fopen($dataFile, "w"); fwrite($fp, $data); fclose($fp); #簽名 openssl_pkcs7_sign($dataFile,$signedFile, $signCert, array($signKey,""), NULL, PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSIGS); $signedBase64 = file_get_contents($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); #信封 openssl_pkcs7_encrypt($signedDataFile,$envelopeFile, $encryCert, NULL, PKCS7_BINARY, OPENSSL_CIPHER_3DES); $envelopeBase64 = file_get_contents($envelopeFile); $envelopeBase64 = substr($envelopeBase64, strpos($envelopeBase64, "base64") + strlen("base64")); trim($envelopeBase64); $envelopeBase64 = base64_decode($envelopeBase64); $envelopeBase64 = base64_encode($envelopeBase64); //$envelopeBase64 = str_replace("\r\n","",$envelopeBase64); //print_r( $envelopeBase64 ); //刪除加密過程中建立的檔案 unlink($dataFile); unlink($signedFile); unlink($signedDataFile); unlink($envelopeFile); return $envelopeBase64;
}
如果要用python實現這個方法的話,應該怎麼寫?
回複內容:
python裡面的pkcs7加密好像實現不一樣,得出的結果完全不一樣
php的代碼如下
public static function envelope($data){ $Millisecond = SignEnvelope::getMillisecond(); //儲存來源資料檔案 $dataFile = "./rsa/".$Millisecond."data.txt"; //儲存簽名檔案 $signedFile = "./rsa/".$Millisecond."signed.txt"; //儲存簽名後base64檔案 $signedDataFile = "./rsa/".$Millisecond."signedData.txt"; //儲存信封后檔案 $envelopeFile = "./rsa/".$Millisecond."envelope.txt"; #載入p12 openssl_pkcs12_read (file_get_contents(Contants::pri), $certs, Contants::passWord); $signCert = $certs ['cert']; $signKey = $certs['pkey']; #載入加密認證 $encryCert = file_get_contents(Contants::pub); #加密原文 $fp = fopen($dataFile, "w"); fwrite($fp, $data); fclose($fp); #簽名 openssl_pkcs7_sign($dataFile,$signedFile, $signCert, array($signKey,""), NULL, PKCS7_NOATTR|PKCS7_BINARY|PKCS7_NOSIGS); $signedBase64 = file_get_contents($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); #信封 openssl_pkcs7_encrypt($signedDataFile,$envelopeFile, $encryCert, NULL, PKCS7_BINARY, OPENSSL_CIPHER_3DES); $envelopeBase64 = file_get_contents($envelopeFile); $envelopeBase64 = substr($envelopeBase64, strpos($envelopeBase64, "base64") + strlen("base64")); trim($envelopeBase64); $envelopeBase64 = base64_decode($envelopeBase64); $envelopeBase64 = base64_encode($envelopeBase64); //$envelopeBase64 = str_replace("\r\n","",$envelopeBase64); //print_r( $envelopeBase64 ); //刪除加密過程中建立的檔案 unlink($dataFile); unlink($signedFile); unlink($signedDataFile); unlink($envelopeFile); return $envelopeBase64;
}
如果要用python實現這個方法的話,應該怎麼寫?
你需要別人幫你重寫代碼還是這個:http://www.php2python.com/ ?
就是重寫下啦,搜不到