El Gamal Algorithm and Java problem
I was trying to implement the El Gamal algorithm in java, but the problem is with the last part. I'm not getting the value of Message but according to algorithm i should get the value of message. Can anyone please tell me what did i do wrong?
theory says, msg = x * (z^a)modInverse(p);
import java.math.BigInteger;
import java.math.*;
import java.util.*;
class ElGamel
{
public static void main(String[] args)
{
BigInteger p = new BigInteger("7747");
BigInteger q = new BigInteger("3853");
BigInteger a = new BigInteger("773");
BigInteger k = new BigInteger("1193");
BigInteger msg = new BigInteger("39");
BigInteger y = new BigInteger("0");
BigInteger x = new BigInteger("0");
BigInteger z = new BigInteger("0");
BigInteger temp = new BigInteger("0");
BigInteger t2 = new BigInteger("0");
int t1 = 0;
y = q.modPow(a,p);
System.out.println("Y is "+y);
x = q.modPow(k,p);
System.out.println("X is "+x);
t1 = k.intValue();
z = (msg.multiply(y.pow(t1))).mod(p);
System.out.println("Z is "+z);
t1 = a.intValue();
x = x.pow(t1);
temp = x.modInverse(p);
msg = z.multiply(temp);
System.out.println("Message is "+msg);
}
}
[1322 byte] By [
fai_hasana] at [2007-10-2 4:47:43]

Reposting your code with markup. In future please use [code] tags, as you can see the effect it has on legibility: import java.math.BigInteger;
import java.math.*;
import java.util.*;
class ElGamel
{
public static void main(String[] args)
{
BigInteger p = new BigInteger("7747");
BigInteger q = new BigInteger("3853");
BigInteger a = new BigInteger("773");
BigInteger k = new BigInteger("1193");
BigInteger msg = new BigInteger("39");
BigInteger y = new BigInteger("0");
BigInteger x = new BigInteger("0");
BigInteger z = new BigInteger("0");
BigInteger temp = new BigInteger("0");
BigInteger t2 = new BigInteger("0");
int t1 = 0;
y = q.modPow(a,p);
System.out.println("Y is "+y);
x = q.modPow(k,p);
System.out.println("X is "+x);
t1 = k.intValue();
z = (msg.multiply(y.pow(t1))).mod(p);
System.out.println("Z is "+z);
t1 = a.intValue();
x = x.pow(t1);
temp = x.modInverse(p);
msg = z.multiply(temp);
System.out.println("Message is "+msg);
}
}
Java isn't C. You don't have to declare all your variables at the start of a method, so you can simplify your existing code a lot to import java.math.BigInteger;
class ElGamal
{
public static void main(String[] args)
{
BigInteger p = new BigInteger("7747");
BigInteger q = new BigInteger("3853");
BigInteger a = new BigInteger("773");
BigInteger k = new BigInteger("1193");
BigInteger msg = new BigInteger("39");
BigInteger y = q.modPow(a,p);
System.out.println("Y is "+y);
BigInteger x = q.modPow(k,p);
System.out.println("X is "+x);
int t1 = k.intValue();
BigInteger z = (msg.multiply(y.pow(t1))).mod(p);
System.out.println("Z is "+z);
t1 = a.intValue();
x = x.pow(t1);
BigInteger temp = x.modInverse(p);
msg = z.multiply(temp);
System.out.println("Message is "+msg);
}
}
Now, if you're going to use ints you should use ints, and if you're going to use BigIntegers you should use BigIntegers. One final substitution to your original code: import java.math.BigInteger;
class ElGamal
{
public static void main(String[] args)
{
BigInteger p = new BigInteger("7747");
BigInteger q = new BigInteger("3853");
BigInteger a = new BigInteger("773");
BigInteger k = new BigInteger("1193");
BigInteger msg = new BigInteger("39");
BigInteger y = q.modPow(a,p);
System.out.println("Y is "+y);
BigInteger x = q.modPow(k,p);
System.out.println("X is "+x);
BigInteger z = (msg.multiply(y.modPow(k, p))).mod(p);
System.out.println("Z is "+z);
x = x.modPow(a, p);
BigInteger temp = x.modInverse(p);
msg = z.multiply(temp);
System.out.println("Message is "+msg);
}
}
There's one line in there which doesn't contain the magic three characters "mod". Add a .mod(p) to it and you get the right output.
thanks for your help but it should return the original value of msg which is 39, but it is not returning. may be something wrong with the algorithm. can anyone help me with the algorithm please?
Read my lips: there's one line in there which doesn't contain the magic three characters "mod". Add a .mod(p) to it and you get the right output.