首頁  >  問答  >  主體

這段 python 想改寫成 PHP,怎麼破?求解

#!/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
$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',計算估計超出了範圍。

天蓬老师天蓬老师2736 天前526

全部回覆(1)我來回復

  • 滿天的星座

    滿天的星座2017-05-16 13:03:43

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

    回覆
    0
  • 取消回覆