Heim >Backend-Entwicklung >PHP-Tutorial >linux - python复写php的代码

linux - python复写php的代码

WBOY
WBOYOriginal
2016-06-06 20:15:311016Durchsuche

  1. python里面的pkcs7加密好像实现不一样,得出的结果完全不一样

  2. php的代码如下

    <code>   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 "<br><br><br>";
       $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;</code>

    }

如果要用python实现这个方法的话,应该怎么写?

回复内容:

  1. python里面的pkcs7加密好像实现不一样,得出的结果完全不一样

  2. php的代码如下

    <code>   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 "<br><br><br>";
       $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;</code>

    }

如果要用python实现这个方法的话,应该怎么写?

你需要别人帮你重写代码还是这个:http://www.php2python.com/ ?

就是重写下啦,搜不到

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn