Java中的RSA加密算法:没有BigIntegers
我需要用Java实现RSA算法。我找到了使用 BigIntegers 的最佳解决方案,问题是我只需要使用整数或长整数。 加密过程如下:M[i]^e mod n
其中 M[i] 是输入字符,e 是键值。我尝试使用字符的 ASCII 代码,但使用 115 和 116 等代码时,我很快就超出了范围。我该如何解决这个问题?提前致谢。
I need to implement RSA algorithm in Java. I've found the best solution using BigIntegers, problem is that I need to work only with ints or longs.
The encrypting is done like this: M[i]^e mod n
where M[i] is an input char and e is a key value. I tried using the ASCII codes of chars, and with codes such as 115 and 116 I quickly get out of range. How can I solve the problem? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以查看模幂。这样您就可以克服计算中的大部分溢出问题。
You may have a look at modular exponentiation. This way you overcome most of the overflows in your calculations.
澄清一下......
如果您回忆起基础数学,
那么,您可以将疯狂的高功率分解为较低的功率,然后取它们的值的模(从而保持值很小),然后重新组合它们:
我不不知道这是否算作“放弃”,但我的学生曾经遇到过麻烦,我毫无问题地向他们展示了这个技巧。此外,我认为这更多的是递归练习。
To clarify a bit...
If you recall from basic math,
So, you can split your crazy high powers into lower powers and then take the modulo of their value (thereby keeping the value small), and then recombine them afterwards:
I don't know if this counts as "giving it away" but my students had trouble with this once and I had no problem showing them this trick. Besides, I presume this is more of an exercise in recursion than anything else.