import java.util.Arrays;
import java.util.Vector;
import java.util.*;
/*
* Created on Mar 24, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ElGamal {
/**
*
*/
public ElGamal() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Vector primeVector = new Vector();
Generator generator = new Generator();
int primeNumber = 0;
while (primeVector.isEmpty()) {
primeNumber = generator.createPrimeNumber(1000, 10000);
System.out.println("--------Prime Number--------");
System.out.println("Prime Number : " + primeNumber);
//Vector pnVector = generator.createPrimeNumberVector(1000, 2357);
primeVector = generator.createGenerator(primeNumber);
}
generator.initializeGenerator(Integer.parseInt(primeVector.elementAt(0)
.toString()), primeNumber);
System.out.println("--------Generator--------");
System.out.println("Generator : " + generator.getGenerator());
System.out.println("Prime Number : " + generator.getPrimeNumber());
// Create Encryption and Decryption keys
Keys keys = new Keys();
keys.initializeKeys(generator.getGenerator(), generator
.getPrimeNumber());
EncryptionKey encryptionKey = keys.getEncryptionKey();
DecryptionKey decryptionKey = keys.getDecryptionKey();
System.out.println("--------EncryptionKey--------");
System.out.println("Public Key : " + encryptionKey.getPublicKey());
System.out.println("Generator :" + encryptionKey.getGenerator());
System.out.println("Prime Number : " + encryptionKey.getPrimeNumber());
System.out.println("--------DecryptionKey--------");
System.out.println("Private Key : " + decryptionKey.getPrivateKey());
System.out.println("Prime Number : " + decryptionKey.getPrimeNumber());
// Initialize Encryption
String encryptionMessage = "suranga kulathunga";
Encryption encryption = new Encryption();
encryption.initializeEncryption(encryptionKey, encryptionMessage);
encryption.encrypt(encryptionMessage);
System.out.println("--------Encryption--------");
System.out.println("Encryption Message :" + encryptionMessage);
System.out.println("CipherTextOne : " + encryption.getCipherTextOne());
System.out.print("CipherTextTwo : ");
for (int i = 0; i < encryption.getCipherTextTwoArray().length; i++) {
System.out.print(encryption.getCipherTextTwoArray()[i] + " ");
}
System.out.println("");
// Initialize Decryption
Decryption decryption = new Decryption();
decryption.initializeDecryption(decryptionKey);
int[] mes = decryption.decrypt(encryption.getCipherTextOne(),
encryption.getCipherTextTwoArray());
System.out.println("--------Decryption--------");
String message = "";
int ASCII = 0;
System.out.print("Decryption Message :");
for (int i = 0; i < mes.length; i++) {
message += "" + ((char) mes[i]);
System.out.print(mes[i] + " ");
}
System.out.println("");
decryption.setMessage(message);
System.out.println("Final Message : " + decryption.getMessage());
}
}
package elgamal;
/*
* Created on Mar 31, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Decryption {
private DecryptionKey decryptionKey = null;
private String message ="";
/**
*
*/
public Decryption() {
super();
// TODO Auto-generated constructor stub
}
/*
* Inverse of b mod a
*/
public long FindInverse(long a, long b){
long q =1;
long temp = 0;
long r = 1;
long sign = 1;
long store = a;
long s =0;
while(b!=0){
q=a/b;
temp = r;
r = temp*q+s;
s= temp;
temp = b;
b = a- q*temp;
a = temp;
sign =- sign;
}
long answer = (r-(sign*s))%store;
return answer;
}
public void initializeDecryption(DecryptionKey decryptionKey){
this.decryptionKey =decryptionKey;
}
public DecryptionKey getDecryptionKey(){
if(decryptionKey != null){
return this.decryptionKey;
}else{
this.decryptionKey = new DecryptionKey ();
return this.decryptionKey;
}
}
public int getModValue(int tempGenerator , int modNumber, int power){
int ReturnModValue = 1;
for(int i =0;i < power; i++){
ReturnModValue =(ReturnModValue*tempGenerator) % (modNumber);
}
return ReturnModValue;
}
public int[] decrypt(int cipherTextOne , int [] cipherTextTwoArray){
int []InverseValue2 = new int[cipherTextTwoArray.length];
long InverseValue = FindInverse(this.decryptionKey.getPrimeNumber(),cipherTextOne);
int InverseValue1 =getModValue((int)InverseValue,this.decryptionKey.getPrimeNumber(),this.decryptionKey.getPrivateKey());
for(int i =0; i
}
return InverseValue2;
}
public String getMessage(){
return this.message;
}
public void setMessage(String message){
this.message = message;
}
}
package elgamal;
/*
* Created on Mar 27, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CipherText {
/**
*
*/
private int CipherTextOne = 0;
private int CipherTextTwo = 0;
public CipherText() {
}
public void setCipherTextOne(int CipherTextOne ){
this.CipherTextOne = CipherTextOne;
}
public void setCipherTextTwo(int CipherTextTwo ){
this.CipherTextTwo = CipherTextTwo;
}
public int getCipherTextOne(){
return this.CipherTextOne;
}
public int getCipherTextTwo(){
return this.CipherTextTwo;
}
}
package elgamal;
/*
* Created on Mar 28, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DecryptionKey {
/**
*
*/
private int primeNumber = 0;
private int privateKey = 0;
public DecryptionKey() {
}
public int getPrivateKey(){
return privateKey;
}
public int getPrimeNumber(){
return primeNumber;
}
public void setPrivateKey(int privateKey){
this.privateKey = privateKey;
}
public void setPrimeNumber(int primeNumber){
this.primeNumber = primeNumber;
}
}
package elgamal;
/*
* Created on Mar 28, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Encryption {
/**
*
*/
private int randomNumber = 0;
private int cipherTextOne = 0;
private int cipherTextTwo = 0;
private int [] cipherTextTwoArray = null;
private String Message = "";
private EncryptionKey encryptionKey = new EncryptionKey();
public Encryption() {
super();
// TODO Auto-generated constructor stub
}
public void createCipherTextOne(){
int primeNumber = this.encryptionKey.getPrimeNumber();
int generator= this.encryptionKey.getGenerator();
this.cipherTextOne = getModValue(generator,primeNumber,randomNumber);
//this.cipherTextOne =(Math.pow(generator,randomNumber))%publicKey;
}
public int createCipherTextTwo(int message){
int primeNumber = this.encryptionKey.getPrimeNumber();
int generator= this.encryptionKey.getGenerator();
int randomNumber =this.randomNumber;
//int powerValue = randomNumber*primeNumber;
int powerValue = getModValue(encryptionKey.getPublicKey(),primeNumber,randomNumber);
this.cipherTextTwo = (message*powerValue)%primeNumber;
//this.cipherTextTwo = (message*getMadValue(generator,primeNumber,randomNumber))%primeNumber;
//this.cipherTextOne =(Math.pow(generator,randomNumber))%publicKey;
return cipherTextTwo;
}
public void initializeEncryption(EncryptionKey encryptionKey,String encryptionMessage){
this.encryptionKey =encryptionKey;
this.randomNumber =(int)((this.encryptionKey.getPrimeNumber()-2)*(Math.random()));
this.Message = encryptionMessage;
}
public int getModValue(int tempGenerator , int modNumber, int power){
int ReturnModValue = 1;
for(int i =0;i < power; i++){
ReturnModValue =(ReturnModValue*tempGenerator) % (modNumber);
}
return ReturnModValue;
}
public int getCipherTextOne(){
return this.cipherTextOne;
}
public int getCipherTextTwo(){
return this.cipherTextTwo;
}
public int[] getCipherTextTwoArray(){
return this.cipherTextTwoArray;
}
public void encrypt(String Message){
this.createCipherTextOne();
int length = Message.length();
//int [] Ascii = new int[Message.];
cipherTextTwoArray = new int[length];
for (int i = 0 ; i
}
}
}
package elgamal;
/*
* Created on Mar 28, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class EncryptionKey {
/**
*
*/
private int primeNumber = 0;
private int publicKey = 0;
private int generator = 0;
public EncryptionKey() {
super();
// TODO Auto-generated constructor stub
}
public int getPrimeNumber(){
return primeNumber;
}
public int getPublicKey(){
return publicKey;
}
public int getGenerator(){
return generator;
}
public void setPrimeNumber(int primeNumber){
this.primeNumber = primeNumber;
}
public void setPublicKey(int publicKey){
this.publicKey = publicKey;
}
public void setGenerator(int generator){
this.generator = generator;
}
}
package elgamal;
import java.util.Vector;
/*
* Created on Mar 26, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Generator {
private int primeNumber = 0;
private int generator = 0;
/**
*
*/
public Generator() {
super();
// TODO Auto-generated constructor stub
}
public Vector createGenerator(int primeNumber){
Vector aa = new Vector();
int tempGenerator = 0;
for ( tempGenerator =2 ; tempGenerator
int[] TempArray = new int[primeNumber - 1];
int ReturnModValue = 1;
for (int tempVal = 1; tempVal < primeNumber; tempVal++) {
// Return the Mod value
ReturnModValue = getModValue(tempGenerator, primeNumber,
tempVal,ReturnModValue);
// Check the Mod value , if it is equal to 1 and not the last number
if(ReturnModValue == 1 && tempVal!=primeNumber-1){
break;
}
// Compare it with other mod values
boolean temp = compareValue(TempArray, ReturnModValue , tempVal);
// Add the Mod values to a array
TempArray[tempVal - 1] = ReturnModValue;
if (temp == true) {
break;
}
// If this is a generator set the flag true
if(tempVal == primeNumber-1 && TempArray[tempVal-1]==1){
valueGenerated= true;
break;
}
}
// Add the Generator to the Vetor
if(valueGenerated==true){
aa.addElement(String.valueOf(tempGenerator));
break;
}
}
return aa;
//return tempGenerator;
}
public int getModValue(int tempGenerator , int primeNumber, int tempVal, int ReturnModValue){
int anu =(ReturnModValue*tempGenerator) % (primeNumber);
return anu;
}
public boolean compareValue(int TempArray[],int ReturnModValue, int tempVal){
for ( int i =0 ; i
return true;
}
}
return false;
}
public int createPrimeNumber(int StartNumber , int EndNumber){
double x =0;
while (true){
double PrimeNumber =StartNumber+ (EndNumber - StartNumber)*Math.random();
int PrimeNumber1 =(int)PrimeNumber;
//double x = (Math.pow(2,PrimeNumber1-1))%PrimeNumber1;
x = getTotalModValue(2,PrimeNumber1,PrimeNumber1-1);
if(x==1){
return PrimeNumber1;
}
}
}
public Vector createPrimeNumberVector(int StartNumber , int EndNumber){
Vector aa = new Vector();
double x =0;
double y =0;
double xfinal =0;
int ans =0;
for ( int i =StartNumber ; i<=EndNumber ; i++){
// int index = i/1000;
// x = (Math.pow(2,1000))%i;
// y = (Math.pow(2,i-1-(index*1000)))%i;
// xfinal =Math.pow(x,index);
// ans = (xfinal*y)%i;
ans = getTotalModValue(2,i,i-1);
if(ans==1){
aa.addElement(String.valueOf(i));
}
}
return aa;
}
public int getTotalModValue(int tempGenerator , int modNumber, int power){
int ReturnModValue = 1;
for(int i =0;i < power; i++){
ReturnModValue =(ReturnModValue*tempGenerator) % (modNumber);
}
return ReturnModValue;
}
public void setPrimeNumber(int primeNumber){
this.primeNumber = primeNumber;
}
public void setGenerator(int generator){
this.generator = generator;
}
public int getPrimeNumber(){
return primeNumber;
}
public int getGenerator(){
return generator;
}
public void initializeGenerator(int generator,int primeNumber){
setPrimeNumber(primeNumber);
setGenerator(generator);
}
}
package elgamal;
/*
* Created on Mar 26, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author suranga kulathunga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Keys {
/**
*
*/
public Keys() {
}
private int primeNumber = 0;
private int privateKey = 0;
private int publicKey = 0;
private int generator = 0;
public int getPrivateKey(){
return privateKey;
}
public int getPrimeNumber(){
return primeNumber;
}
public int getPublicKey(){
return publicKey;
}
public int getGenerator(){
return generator;
}
public void setPrimeNumber(int primeNumber){
this.primeNumber = primeNumber;
}
public void setGenerator(int generator){
this.generator = generator;
}
public void setPrivatePublicKeys(int privateKey,int primeNumber){
this.privateKey = privateKey;
//this.publicKey = (Math.pow(this.generator,privateKey))%primeNumber;
this.publicKey = getMadValue(generator,primeNumber,privateKey);
}
public void initializeKeys(int generator,int primeNumber){
setPrimeNumber(primeNumber);
setGenerator(generator);
this.privateKey =(int)((primeNumber-2)*(Math.random()));
this.publicKey = getMadValue(generator,primeNumber,privateKey);
//this.publicKey = (Math.pow(this.generator,privateKey))%primeNumber;
}
public EncryptionKey getEncryptionKey(){
EncryptionKey publicKey = new EncryptionKey();
publicKey.setGenerator(this.generator);
publicKey.setPrimeNumber(this.primeNumber);
publicKey.setPublicKey(this.publicKey);
return publicKey;
}
public DecryptionKey getDecryptionKey(){
DecryptionKey privateKey = new DecryptionKey();
privateKey.setPrivateKey(this.privateKey);
privateKey.setPrimeNumber(this.primeNumber);
return privateKey;
}
public int getMadValue(int tempGenerator , int modNumber, int power){
int ReturnModValue = 1;
for(int i =0;i < power; i++){
ReturnModValue =(ReturnModValue*tempGenerator) % (modNumber);
}
return ReturnModValue;
}
}
29 comments:
thanks. I need el-gamal source
but, it is not ready for subliminal channel, right?
but the source can be use for my other projects!
Thanks you very much!
In class Generator something is missing in code, line goes:
for ( tempGenerator =2 ; tempGenerator
Thanx for this post anyway!
Somethings missing here in class Generator:
for ( tempGenerator =2 ; tempGenerator
Thank you for your post anyway.
Missing code lines:
for(int i =0; i
InverseValue2[i] =
Missing lines in Encryption and Decrytion.
Hi Sanjaya,
Could you please zip file of your code at my email ID kumar.sandip@gmail.com . I am having some problem in running your code. Actually I want to loan part of your code for my project.
Thanks and Regards
Sandip Kumar
Hi Sanjaya,
Could you please send zip file of your code at my email ID kumar.sandip@gmail.com . I am having some problem in running your code. Actually I want to loan part of your code for my project.
Thanks and Regards
Sandip Kumar
view source on this page to see the missing information, it's all there it just doesn't display in html.
Hello All,
very sorry that there are some errors in few classes which happened due to coping of the files. Some parts of the while loops are missing.
In Decryption Class
public int[] decrypt(int cipherTextOne , int [] cipherTextTwoArray){
//new Change
for(int i =0; i< cipherTextTwoArray.length ;
i++){
Thank you
Hello All,
very sorry that there are some errors in few classes which happened due to coping of the files. Some parts of the while loops are missing.
In Decryption Class
public int[] decrypt(int cipherTextOne , int [] cipherTextTwoArray){
.....code.....
//new Change
for(int i =0; i< cipherTextTwoArray.length ;i++){.......
In Encryption Class
public void encrypt(String Message){
......code........
//new Change
for (int i = 0 ; i< length ; i++){
.....
In Generator Class
public Vector createGenerator(int primeNumber){
Vector aa = new Vector();
int tempGenerator = 0;
//new Change
for ( tempGenerator =2 ; tempGenerator< primeNumber ;tempGenerator++ ){
.....
public boolean compareValue(int TempArray[],int ReturnModValue, int tempVal){
//new Change
for ( int i =0 ; i< TempArray.length ;i++){
...........
All of this errors are due to for loops. please update your code or send me an email surangakulathunga@yahoo.com
Thank you
thanks!you can help me that problem:in your code,have you use square & multiplication algorithms?? can you send your answer to my email:hoang403@gmail.com, because i really need it,I have a exercise must finish about elgamal in java,thanks a lot!!!
i need elgamal package code...
error saying that :cannot find symbol
pls help me!!!!!!!!!
Hi, may I know what's the purpose of:
temp = r;
r = temp*q+s;
in the coding?
This code does not compile at all
hi this is venu...I am having a little trouble with the code that you hava write...So please send me the zip file of your java files..please its urgent..I need them in my project..day after tomorrow is the project review date....,my email is venugopalsmartboy@gmail.com
please please its very urgent
plz will u send me zip file of ur code?
send me at
vikrant.nashik@gmail.com
plz send.
bye.
hello,
i want the modified new correct code for ElGamal Algorithm. Please send me the code zip file with detailed procedure of how to run that files.
Please send me immedietly on
savekaravinash@gmail.com
ok...........bye.........
plz will u send me zip file of ur code (Elgamal)?
at : sancha1chafik@gmail.com
plz :(.
Hi, can you send a copy of the code to my email herun1988@gmail.com? Many thanks!
Hi, can you send a copy of the code to my email hamka.opz@gmail.com?
I wanna learn elgamal
Many thanks!
Could you please zip file of your code at my email ID nits.rnsm@gmail.com . I am having some problem in running your code.
dsm
can u send me your complete code to me, i m trying understand u code, but so many error, please need help
can u send me your complete code to me, I do not understand, please send the finished file. thank you
please send in Rizalduasembilan@gmail.com
please, I really need that file
Hi...can you plz send me the complete code zip file on email id rajputsanchi@yahoo.com...i need the code
hello can you send me this code on my email plz vhutshilomawela25@gmail.com
thanks in advance
Post a Comment