El Gamal Problem 2

import java.math.BigInteger;

import java.math.*;

import java.util.*;

class ElGamel2

{

BigInteger p =new BigInteger("0");

BigInteger x =new BigInteger("10");//works if x devides by 10

BigInteger k =new BigInteger("7331");

//BigInteger g = new BigInteger("1173");

//BigInteger msg = new BigInteger("666");

BigInteger y =new BigInteger("0");

BigInteger a =new BigInteger("0");

BigInteger b =new BigInteger("0");

BigInteger temp =new BigInteger("0");

BigInteger t2 =new BigInteger("1");

int t1 = 0;

public BigInteger GetA(BigInteger g){

p = g.pow(150).add(t2);

t1 = x.intValue();

g = g.pow(t1);

y = g.mod(p);

a = g.modPow(k,p);

return a;

}

public BigInteger getB(BigInteger g, BigInteger eMsg){

p = g.pow(150).add(t2);

t1 = k.intValue();

y = y.pow(t1);

b = (y.multiply(eMsg)).mod(p);

return b;

}

public BigInteger decryptElGamel(BigInteger y1, BigInteger y2){

BigInteger msg =new BigInteger("0");

t1 = x.intValue();

y1 = y1.pow(t1);

//a = a.mod(p);

//a = a.mod(p);

msg = (y1.divide(y1)).mod(p);

//msg = msg.mod(p);

return msg;

}

}

import java.math.*;

class ElGamelMain

{

publicstaticvoid main(String[] args)

{

BigInteger teller1y1 =new BigInteger("0");

BigInteger teller1y2 =new BigInteger("0");

BigInteger g =new BigInteger("131");

BigInteger a =new BigInteger("0");

BigInteger b =new BigInteger("0");

BigInteger u =new BigInteger("0");

BigInteger msg =new BigInteger("111");

a = ElGamel2.GetA(g);

System.out.println(a);

b = ElGamel2.GetB(g,msg);

System.out.println(b);

msg = ElGamel2.decryptElGamel(a,b);

System.out.println(msg);

}

}

I'm getting the following error

- Compile -

ElGamelMain.java:17: non-static method GetA(java.math.BigInteger) cannot be referenced from a static context

BigInteger a1 = ElGamel2.GetA(g);

^

ElGamelMain.java:20: cannot resolve symbol

symbol : method GetB (java.math.BigInteger,java.math.BigInteger)

location: class ElGamel2

b = ElGamel2.GetB(g,msg);

^

ElGamelMain.java:23: non-static method decryptElGamel(java.math.BigInteger,java.math.BigInteger) cannot be referenced from a static context

msg = ElGamel2.decryptElGamel(a,b);

^

3 errors

Output completed (0 sec consumed) - Normal Termination

What have i done wrong? can anyone help me out please? I just want to call the method from the first class and passing the value of bigInteger.

[4743 byte] By [fai_hasana] at [2007-10-2 4:53:42]
# 1

the ElGamal2 that you have defined is a structure

That structure contains elements, namely a bunch of big integers like p, x, k g ...

But in your main routine you have have never once created an ElGamal2 object, so you have never created a structure with those values in it that you can work with.

When you make a call like

ElGamal2.getA(g)

you are calling getA() as if it is a static function which it is not. getA can only operate on an ElGamal2 structure

so you need to do something like this:

ElGamal2 e = new ElGamal2(); // this creates an ElGamal2 objcet

a = e.getA(g); // this calls the getA routine to operate on the e structure.

Your other bug is that you named your routine getB and you called it with GetB - case is significant.

Also you should rename your GetA routine, and the place that it is called to getA, just to follow the name convention that Classes start with capital letters and methods and members start with lowercase.

marlin314a at 2007-7-16 0:58:10 > top of Java-index,Other Topics,Algorithms...
# 2

> What have i done wrong? can anyone help me out

> please? I just want to call the method from the first

> class and passing the value of bigInteger.

The compiler messages speak for themselves. You can't reference non-static methods (or variables) from ElGamel2. So you either create an ElGamel2-object in your ElGamelMain-class and then call the methods:

public class ElGamelMain {

public static void main(String[] args) {

ElGamel2 elg2 = new ElGamel2();

BigInteger a = elg2.getA(BigInteger);

// ...

}

}

Or you should declare all your methods and "global" variables in your ElGamel2 -class static:

public class ElGamel2 {

private static BigInteger p = new BigInteger("0");

// ...

public static BigInteger getA(BigInteger g) { /**/ }

// ...

}

Good luck.

prometheuzza at 2007-7-16 0:58:10 > top of Java-index,Other Topics,Algorithms...
# 3
Hmmm, I need more coffee.
prometheuzza at 2007-7-16 0:58:10 > top of Java-index,Other Topics,Algorithms...
# 4
Thanks mate, it works absolutely fine.
fai_hasana at 2007-7-16 0:58:10 > top of Java-index,Other Topics,Algorithms...