【问题标题】:Modular exponentation in CC中的模幂运算
【发布时间】:2017-02-02 13:05:06
【问题描述】:

帮助!我需要实现一个 C 程序(仅使用字符串、stdlib 和 stdio 库),它使用非常大的数字的模幂运算,其中一些是 260 位。我正在考虑使用链表,但我找不到关于如何实现它的好的参考。我需要这个,因为我需要使用 RSA 来加密和解密消息。

另外,我在获得两个非常大的数字的 GCD 时遇到了完全相同的问题。有什么办法可以做到这一点吗?

【问题讨论】:

  • 我忘了说我要进行模块化的数字已经存储在链表中的单个数字中
  • 您需要在 C 中实现 BigInteger。如果您仅限于这些库,那么这将是很多工作。这是作业吗?你确定不能用更小的数字来实现吗?
  • 是的。我们应该处理大于整数限制的数字。 @LukePark
  • 祝你好运。您必须编写自己的 BigInteger 实现。玩得开心。

标签: encryption rsa exponentiation modular-arithmetic


【解决方案1】:

How to store large numbers? 您可以使用这种类型的存储数据,它可以帮助您使用少量内存,并且操作将比其他任何操作都快得多,因为您可以将它们直接放在位上,并且可以使用特殊公式: 用于添加 Irecomand 你检查溢出;

相乘(x=x*y):

aux=x;x=0;
while(y)
{ 
  if(last_bit(y)==1)
     x=x+aux;
  shift_left(aux);
  shift_right(y);
}

function modular_pow(base, exponent, modulus)
{
if modulus = 1 then return 0
Assert :: (modulus - 1) * (modulus - 1) does not overflow base
result = 1
base = base mod modulus
while exponent > 0
    if (last_bit(exponent)==1):
       result = (result * base) mod modulus
    exponent = exponent >> 1
    base = (base * base) mod modulus
return result
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-13
    • 2017-07-28
    • 1970-01-01
    • 2014-08-21
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多