【问题标题】:Java BigInteger to the intValue [duplicate]Java BigInteger 到 intValue [重复]
【发布时间】:2021-09-18 02:12:06
【问题描述】:

我正在尝试使用 Java 代码练习“仿射密码”。我尝试在 BigInteger 类中使用 modInverse() 函数。我必须在 modInverse 函数中放入一些整数值。因此,我使用 BigInteger.valueOf(Integer) 来获取整数的 ModInverse。但是问题出现在这里,当我尝试将 BigInteger 值更改为整数时,它给了我一个错误“无法从 BigInteger 类型对非静态方法 modInverse(BigInteger) 进行静态引用。我应该如何解决问题?

这是我的代码:

for(int a = 1; a<=25;a++)
        {
            for(int b = 0; b<=26; b++)
            {
                for(int i  =0; i < cipherText.length;i++)
                {
                    cipherText[i] = (byte) ( ((cipherText[i]-'A')-b)* (BigInteger.modInverse(BigInteger.valueOf(a)).intValue() % 26 + 'A' );
                }
            }
        }

【问题讨论】:

    标签: java encryption integer static-methods biginteger


    【解决方案1】:

    BigInteger 是类名。当您执行BigInteger.someMethod 时,您假设someMethodstatic 方法。在这种情况下,它不是。这是一个instance 方法,因此您需要一个BigInteger 类的实例才能使用它。这是一个使用b and c 的示例,它们是相对质数。

    BigInteger b = BigInteger.valueOf(33);
    BigInteger c = BigInteger.valueOf(14);
    int v = b.mod(c).intValue();
    System.out.println(v);
    v = c.modInverse(b).intValue();
    System.out.println(v);
    

    打印

    5
    26
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2021-11-15
      • 2023-04-06
      相关资源
      最近更新 更多