2011-11-11 22 views
8

我從來沒有在Mathematica中發生溢出錯誤,發生下列情況。Mathematica溢出[]錯誤:爲什麼和如何繞過?

我演示-ED的RSA加密原理如下:

n = 11*13 
m = EulerPhi[n] 
e = 7 
GCD[e, m] 
d = PowerMod[e, -1, m] 
cipher2[m_String] := Map[Mod[#^e, n] &, ToCharacterCode[m]] 
decipher2[x_Integer] := FromCharacterCode[Map[Mod[#^d, n] &, x]] 

In[207]:= cipher2["StackOverflow"] 
decipher2[cipher2["StackOverflow"]] 
Out[207]= {8,129,59,44,68,40,79,62,49,119,4,45,37} 
Out[208]= StackOverflow 

沒問題SOFAR。

然後,我改變了素數到更現實,但仍然非常適中的大小。

n = 252097800611*252097800629 

In[236]:= cipher2["StackOverflow"] 
decipher2[cipher2["StackOverflow"]] 

Out[236]= {27136050989627, 282621973446656, 80798284478113, \ 
93206534790699, 160578147647843, 19203908986159, 318547390056832, \ 
107213535210701, 250226879128704, 114868566764928, 171382426877952, \ 
207616015289871, 337931541778439} 

During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >> 

During evaluation of In[236]:= General::ovfl: Overflow occurred in computation. >> 

Out[237]= FromCharacterCode[{Overflow[], Overflow[], Overflow[], 
    Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], 
    Overflow[], Overflow[], Overflow[], Overflow[], Overflow[]}] 

問題:我是否簡單地通過了Mathematica的限制?我用過不正確的方法嗎?什麼是旁路,如果有的話?

回答

8

嘗試在decyphering操作使用PowerMod

n = 252097800611*252097800629; 
m = EulerPhi[n]; 
e = 7; 
Print[GCD[e, m]]; 
d = PowerMod[e, -1, m]; 
Print[{"n" -> n, "m" -> m, "e" -> e, "d" -> d}]; 
Grid[ 
Join[{ 
    {"Input", "Encrypted", "Decrypt with Mod", "Decrypt with PowerMod"}}, 
    Table[{i, (j = Mod[i^e, n]), Mod[j^d, n], PowerMod[j, d, n]}, {i, 40}]], 
Frame -> All] 
+0

感謝阿諾德·,(你的名字暗示你有祖先在我住的比利時或者NL。) –

6

是的,你已經通過數學的極限了。在特定版本的Mathematica系統中可以表示的最大數量由$MaxNumber顯示。在你的第二個例子中,d=18158086021982021938023,因此27136050989627^d大於$MaxNumber

您可以在太像你這樣爲d第二個步驟,這將超過Mod更有效地計算a^b mod n使用PowerMod。隨着decipher2[x_List] := FromCharacterCode[Map[PowerMod[#, d, n] &, x]],您可以:

cipher2["StackOverflow"] 
decipher2[cipher2["StackOverflow"]] 

Out[1]= {27136050989627, 282621973446656, 80798284478113, \ 
93206534790699, 160578147647843, 19203908986159, 318547390056832, \ 
107213535210701, 250226879128704, 114868566764928, 171382426877952, \ 
207616015289871, 337931541778439} 

Out[2]= "StackOverflow" 
+1

我想我假設Mathematica會在Mod n和小n之間切換到PowerMod。 - 無論如何,PowerMod的作品,謝謝。 - 我會接受Arnoud Buzing的回答,不過因爲他先回答了,並沒有你那麼重要,對不起。 –

0

是的,正如其他人回答你已經很好,真正達到了$ MAXNUMBER Mathematica可以處理。

有一個旁路可以找到許多大於$ MaxNumber的大數字的mod。

與其將大量數據直接輸入到Mathematica中,比如163840000000^18158086021982021938023這是絕對巨大的,使用模塊化算術來節省Mathematica是不得不計算這麼大數目的麻煩。

你應該能夠爲此開發一個Mathematica代碼,我還不知道如何做到這一點。但是你可以通過以下方式手動完成: Mod [Mod [Mod [Mod [Mod [Mod [Mod [Mod [Mod [163840000000^181,n]^580,n]^860,n]^219,n] 820,N]^219,N]^380,N]^23,N]

,讓你正在尋找正確的答案,不超過$ MAXNUMBER