這段 python 想改寫成 PHP,怎麼破?求解
天蓬老师
天蓬老师 2017-05-16 13:01:43
0
1
490
  • python 程式碼

#!/usr/bin/env python
# coding=utf-8
text = 'odd9xifHtMbFIDSY'
pubKey = '010001'
modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
text = text[::-1]
rs = int(text.encode('hex'), 16)**int(pubKey, 16)%int(modulus, 16)
r = format(rs, 'x').zfill(256)
print r
  • php 自己嘗試寫的,但失敗了,麻煩幫忙分析下具體原因。

$text = 'odd9xifHtMbFIDSY';
$pubKey = '010001';
$modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';

$_text = strrev($text);

// 十六进制转十进制
$biText = intval(bin2hex($_text),16);
$biEx = intval($pubKey, 16);
$biMod = intval($modulus, 16);

// 先平方,后取模, 再转十六进制
$biRet = dechex(fmod(pow($biText,$biEx),  $biMod));


// 十进制转十六进制,
while(strlen($biRet) < 256){
    $biRet = '0' . $biRet;
}

echo $biRet;

intval轉換的最大值是9223372036854775807,$biText $biMod 相等了,都等於最大值。
$biRet ='NAN',計算估計超出了範圍。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(1)
滿天的星座

https://github.com/phpmath/bi...
phpbigint可以看下

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!