linux - python复写php的代码
程序员文章站
2024-02-01 15:26:22
...
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/ ?
就是重写下啦,搜不到