0

There is this java.lang.ArrayIndexOutOfBoundsException: 0 error appearing whenever I run my program which is RSA algorithm.

package cn;
import java.math.BigInteger;
import java.security.SecureRandom;
public class rsa 
{
    private final static BigInteger one = new BigInteger("1");
    private final static SecureRandom random = new SecureRandom();
    private BigInteger privateKey;
    private BigInteger publicKey;
    private BigInteger modulus;
    rsa(int N) 
    {
        BigInteger p = BigInteger.probablePrime(N/2, random);
        BigInteger q = BigInteger.probablePrime(N/2, random);
        BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
        modulus = p.multiply(q);                                  
        publicKey = new BigInteger("65537");     
        privateKey = publicKey.modInverse(phi);
    }
    BigInteger encrypt(BigInteger message) 
    {
        return message.modPow(publicKey, modulus);
    }
    BigInteger decrypt(BigInteger encrypted) 
    {
        return encrypted.modPow(privateKey, modulus);
    }
    public String toString() 
    {
        String s = "";
        s += "public  = " + publicKey  + "\n";
        s += "private = " + privateKey + "\n";
        s += "modulus = " + modulus;
        return s;
    }
    public static void main(String[] args) 
    {
        int N = Integer.parseInt(args[0]);
        rsa key = new rsa(N);
        System.out.println(key);
        BigInteger message = new BigInteger(N-1, random);
        BigInteger encrypt = key.encrypt(message);
        BigInteger decrypt = key.decrypt(encrypt);
        System.out.println("message   = " + message);
        System.out.println("encrypted = " + encrypt);
        System.out.println("decrypted = " + decrypt);
    }
}

Can someone please help me out by finding where the error exist. In the eclipse IDE the error was pointed out at the 2nd line of the main function.rsa key = new rsa(N); <=This one

vibby47
  • 1
  • 1
  • 3
  • I think the error is `int N = Integer.parseInt(args[0]);` here. It is expecting you to pass a command line argument to the program, are you providing the required command line argument? If not then it will throw ArrayIndexOutOfBoundsException, since you are trying to access an array index that doesn't exist. – adn.911 Dec 02 '17 at 17:02
  • No there are no arguments to be passed, so how do i solve this problem? – vibby47 Dec 03 '17 at 08:02

0 Answers0