PHP-RSA暗号解読

3642 ワード

markの下で、会社のプロジェクトサーバーはRubyで書いたので、RSAで暗号化して、PHPで読み取ってみました.T.T、PHPの書き方はiOSよりずっと簡単だと思います.

    

 
 0) {
 
        if ($inputLen - $offSet > $maxDecryptBlock) {
            $cache = $method($key, substr($data, $offSet, $maxDecryptBlock));
        } else {
            $cache = $method($key, substr($data, $offSet, $inputLen - $offSet));
        }
        $en = $en . $cache;
 
        $i++;
        $offSet = $i * $maxDecryptBlock;
    }
    return bin2hex($en); //        
}

function rsa_decrypt($method, $key, $data, $rsa_bit = 1024) {
    $data = pack('H*',$data); //           
    $inputLen = strlen($data);
    $offSet = 0;
    $i = 0;
 
    $maxDecryptBlock = $rsa_bit / 8;
 
    $de = '';
    $cache = '';
 
    //        
    while ($inputLen - $offSet > 0) {
 
        if ($inputLen - $offSet > $maxDecryptBlock) {
            $cache = $method($key, substr($data, $offSet, $maxDecryptBlock));
        } else {
            $cache = $method($key, substr($data, $offSet, $inputLen - $offSet));
        }

        $de = $de . $cache;
 
        $i = $i + 1;
        $offSet = $i * $maxDecryptBlock;
    }

    return $de;
}



$pubk = '-----BEGIN PUBLIC KEY-----
xxx
-----END PUBLIC KEY-----';


        $url = 'http://xxx.com/login';
        $post_data['name']      = 'admin';
        $post_data['pwd']       = 'admin';
        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        $post_data = rsa_encrypt('rsa_publickey_encrypt', $pubk, $post_data);
        echo 'post_data='.$post_data.'';

        $res = request_post($url, $post_data); 
        $de = rsa_decrypt('rsa_publickey_decrypt', $pubk, $res);      

        echo "de=".$de.'';

/**
     *   post  url  
     * @param string $url
     * @param string $param
     */
    function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//   curl


        curl_setopt($ch, CURLOPT_URL,$postUrl);//      
        curl_setopt($ch, CURLOPT_HEADER, 0);//  header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//               
        curl_setopt($ch, CURLOPT_POST, 1);//post    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $headers = array();
        $headers[] = 'X-ANFAN-PRODUCTID: 2';
        $headers[] = 'X-ANFAN-RETAILER: 1';

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $data = curl_exec($ch);//  curl
        curl_close($ch);
        
        return $data;
}