Search code examples
javaencryptionintegerstatic-methodsbiginteger

Java BigInteger to the intValue


Im trying to practice the 'Affine Cipher' with the Java code. I tried to use the modInverse() function in BigInteger class. I have to put some integer values in the modInverse function. Therefore, I used the BigInteger.valueOf(Integer) to get the ModInverse of the integer number. But the problem occurs here, when I tried to change the BigInteger value to the integer, it gives me an error that "Can not make a static reference to the non-static method modInverse(BigInteger) from the type BigInteger. How should I fix the problem?

here is my code:

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' );
                }
            }
        }

Solution

  • BigInteger is the class name. When you do BigInteger.someMethod you are assuming that someMethod is a static method. In this case it isn't. It is an instance method so you need an instance of the BigInteger class to use it. Here is an example using b and c which are relatively prime.

    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);
    

    Prints

    5
    26