• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RSA algorithm

 
Greenhorn
Posts: 8
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}




------------------------------------OUTPUT--------------------------

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at rsaTest.RSAtest.main(RSAtest.java:70)
-----------------------------------------------------------------------

I am trying to Generate an N-bit public and private RSA key and use to encrypt and decrypt a random message.
help please?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Tadphale wrote:
------------------------------------OUTPUT--------------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at rsaTest.RSAtest.main(RSAtest.java:70)
-----------------------------------------------------------------------



This stack trace is saying ... in the main() method, of the RSAtest class, located in line 70 of the RSAtest.java file, it is trying to access the first element of an array. And it is an error, because the array being accessed does not have any elements (it is an array of size zero).

Henry
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of points :

  • Your code posted is lacking code tags..
  • Your variable names are really cryptic with 'p','q','phi',N. Please follow java naming conventions, it helps us understand your code too.
  • Where is class RSAtest in your source provided ? After all you are getting the error in that code


  • If you are new to java (just assuming), I suggest that you attempt simpler examples first

    reply
      Bookmark Topic Watch Topic
    • New Topic