【问题标题】:How can I use bigint with C#?如何在 C# 中使用 bigint?
【发布时间】:2012-05-24 09:16:04
【问题描述】:

我致力于实现RSA 密钥算法。但我不能使用 2048 位的值。如何使用它?

我想使用大整数。

【问题讨论】:

  • 通常 RSA 密钥算法一次使用 8 位。您的密钥将被放置在具有 8 个索引的字节数组中。
  • 不重复。我不想使用 long 或 int64。他们对我来说还不够

标签: c# biginteger


【解决方案1】:

最好使用System.Numerics.BigInteger

【讨论】:

    【解决方案2】:

    这里使用BigInteger。此方法在斐波那契数列中打印最多 n 的数字。

    public static void FibonacciSequence(int n)
    {
        /** BigInteger easily holds the first 1000 numbers in the Fibonacci Sequence. **/
        List<BigInteger> fibonacci = new List<BigInteger>();
        fibonacci.Add(0);
        fibonacci.Add(1);
        BigInteger i = 2;
        while(i < n)
        {                
            int first = (int)i - 2;
            int second = (int) i - 1;
    
            BigInteger firstNumber =  fibonacci[first];
            BigInteger secondNumber = fibonacci[second];
            BigInteger sum = firstNumber + secondNumber;
            fibonacci.Add(sum);
            i++;
        }         
    
        foreach (BigInteger f in fibonacci) { Console.WriteLine(f); }
    }
    

    【讨论】:

      【解决方案3】:

      .NET 4.0 中引入了对大整数的本机支持。只需添加对System.Numerics 的程序集引用,在代码文件顶部添加using System.Numerics; 声明,就可以开始了。你要的类型是BigInteger

      【讨论】:

        【解决方案4】:

        BigInteger 在 .NET 4.0 或更高版本中可用。 There are some third-party implementations as well(如果您使用的是较早版本的框架)。

        【讨论】:

          【解决方案5】:

          您可以使用System.Numerics.BigInteger(添加对 System.Numerics 程序集的引用)。正如 cmets 中提到的,这可能不是正确的方法。

          【讨论】:

          • 确实,BigInteger 是要走的路。小提示:它仅在 .NET 4.0 及更高版本中可用。
          猜你喜欢
          • 2021-11-07
          • 2021-05-17
          • 2012-08-09
          • 1970-01-01
          • 1970-01-01
          • 2018-01-24
          • 2020-10-31
          • 1970-01-01
          相关资源
          最近更新 更多