本文介绍了Mathematica Overflow[] 错误:为什么以及如何绕过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mathematica 中从未出现过溢出错误,发生了以下情况.

I never had an overflow error in Mathematica, the following happened.

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

I demo-ed the principle of RSA-encryption as follows:

 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

目前没问题.

然后我将质数更改为更真实但仍然非常适中的大小.

Then I changed the prime numbers to a more realistically, but still very moderate size.

 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 的极限?我是否使用了不正确的方法?什么是旁路(如果有)?

推荐答案

在解密操作中尝试使用PowerMod:

Try using PowerMod in the decyphering operation:

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]

这篇关于Mathematica Overflow[] 错误:为什么以及如何绕过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:12