4 Replies - 690 Views - Last Post: 13 February 2011 - 10:12 AM Rate Topic: -----

#1 nasrul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 11

Error with java.lang

Posted 13 February 2011 - 08:22 AM

Hi,im trying to compile my code and what i got is this error.anyone plz help me with this.
here the error that i'd got
Exception in thread "main" java.lang.NumberFormatException: For input string: "N
"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at RSA.main(RSA.java:69)

and this is my code
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;

   // generate an N-bit (roughly) public and private key
   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");     // common value in practice = 2^16 + 1
      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);
 
      // create random message, encrypt and decrypt
      BigInteger message = new BigInteger(N-1, random);

      //// create message by converting string to integer
      // String s = "test";
      // byte[] bytes = s.getBytes();
      // BigInteger message = new BigInteger(s);

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



Is This A Good Question/Topic? 0
  • +

Replies To: Error with java.lang

#2 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Error with java.lang

Posted 13 February 2011 - 08:55 AM

How are you trying to run your program? I get the feeling you are typing something like this:
java RSA N = 10

The error comes when you are trying to turn your command line argument from a String into an int. If the string contains any non-digit characters, you will get that error. Try running it like this:

java RSA 10
Was This Post Helpful? 1
  • +
  • -

#3 nasrul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 11

Re: Error with java.lang

Posted 13 February 2011 - 09:58 AM

View Postmoobler, on 13 February 2011 - 08:55 AM, said:

How are you trying to run your program? I get the feeling you are typing something like this:
java RSA N = 10

The error comes when you are trying to turn your command line argument from a String into an int. If the string contains any non-digit characters, you will get that error. Try running it like this:

java RSA 10

thanks.i'd got it.my mistake.heee
Was This Post Helpful? 0
  • +
  • -

#4 nasrul  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 11

Re: Error with java.lang

Posted 13 February 2011 - 10:07 AM

View Postnasrul, on 13 February 2011 - 09:58 AM, said:

View Postmoobler, on 13 February 2011 - 08:55 AM, said:

How are you trying to run your program? I get the feeling you are typing something like this:
java RSA N = 10

The error comes when you are trying to turn your command line argument from a String into an int. If the string contains any non-digit characters, you will get that error. Try running it like this:

java RSA 10

thanks.i'd got it.my mistake.heee

can anyone help me how to make this code able to insert a text also beside the number.for example:

java RSA This is a test
Was This Post Helpful? 0
  • +
  • -

#5 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Error with java.lang

Posted 13 February 2011 - 10:12 AM

Anything you send along to Java through the command line like that will be placed in the args array in the main method. The total input string will be split up into separate words (based off the spacing) and each word will be stored in one slot of args.

Using your example, this would be the contents of args:
java RSA This is a test
args[0] -> "This"
args[1] -> "is"
args[2] -> "a"
args[3] -> "test"

Using my example from earlier, this would be the contents of args:
java RSA N = 1
args[0] -> "N"
args[1] -> "="
args[2] -> "1"

And a third example:
java RSA N=1
args[0] -> "N=1"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1