public static void main(String[] ar)
{
.....
  int N = Integer.parseInt(args[0]);
.....
}

Error is : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

public static void main(String[] ar)
{
.....
  int N = Integer.parseInt(args[0]);
.....
}

Error is : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

how does this:

public static void main(String[] ar)//string[] ar

match this:

args[0]

im surprised it even compiles

how does this:

public static void main(String[] ar)//string[] ar

match this:

args[0]

im surprised it even compiles

hmm .. I missed that. but no, that wouldn't compile. probably the code above is not the one he's running.

hmm .. I missed that. but no, that wouldn't compile. probably the code above is not the one he's running.

Lol yeah it must be :D

writing mistake here, in my program both are same i mean ar is not present but args is present in my program.. sorry


also i dont know how to pass command line arguments.. please help

writing mistake here, in my program both are same i mean ar is not present but args is present in my program.. sorry


also i dont know how to pass command line arguments.. please help

well if from command prompt: java jar *.jar hello argument
where 'hello' and 'argument' would be in the args[0] and args[1] indexes

Edited 4 Years Ago by DavidKroukamp: n/a

or, if it's just a single class file,

if you would originally run it like:
java runMyProg

now, try:
java runMyProg firstArgument secondArgument

here is the full code.. now hep me in removing the error

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("encrpyted = " + encrypt);
      System.out.println("decrypted = " + decrypt);
   }
}
Comments
we already told you what the error is and how to remove it. we've done our part, now you do yours.
thats not a nice way to ask
This question has already been answered. Start a new discussion instead.