Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.math.*;

public class Encryption
{
   public static void main(String[] args)
   {
       //declaring BigInteger variables for message,e and n
       BigInteger message,e,n,cipher;
      
       //creating BigInteger objects for message , e and n
       message=new BigInteger(args[0]);
       e=new BigInteger(args[1]);
       n=new BigInteger(args[2]);
      
       //finding the cipher text value using modPow method
       cipher=message.modPow(e,n);
      
       //printing the cipher text
       System.out.println("Message: "+args[0]);
       System.out.println("e      : "+args[1]);
       System.out.println("n      : "+args[2]);
       System.out.println("\nAfter Encryption, Cipher Text: "+cipher);
   }
}


I'm having error at line 14 for message=new BigInteger(args[0]);

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at learnprogramming.Encryption.main(Encryption.java:14)


What I have tried:

I tried using following code after void main
Java
if (args.length != 3)
        {
            System.out.println("java Encrypt message e n");
            System.exit(0);
        }

but this doesn't give me the required encrypted output numbers. It just prints:
java Encrypt message e n



Can anyone please help me solve this error?
Posted
Updated 4-Apr-19 11:12am
v2

Simpel: you haven't specified any parameters, so there are no arguments to process.
No arguments == empty array == error when you try to use any element at all.

Add a check for "no parameters", and when that works, add parameters to your run.
 
Share this answer
 
Comments
khajak80 4-Apr-19 16:37pm    
Thank you for your prompt answer!
Quote:
error at line 14 for message=new BigInteger(args[0]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at learnprogramming.Encryption.main(Encryption.java:14)

The error message tells you that your code was called with no parameter when it needs 3 parameters.
The only solution is to call this code with required parameters.
Quote:
but this doesn't give me the required encrypted output numbers. It just prints:
java Encrypt message e n

This code is doing exactly what it is supposed to do, but it can't create missing values.

It is like trying to use a car without the keys, if you are not a thief, it can get complicated.
It is like trying to solve c = a + b without knowing a and b.

The only possible solution is to call this code with 3 parameters.
 
Share this answer
 
v2
Comments
khajak80 4-Apr-19 16:36pm    
Thanks for your very very clear explanation. That was dumb move from my side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900