Switch to full style
Project under GPL source codes are posted here
Post a reply

Rotor Machine Java

Wed Mar 19, 2008 1:02 am

* Project Name:   Rotor Machine Encryption Decryption Cipher implementation using Java
* Programmer:   msi_333
* Type:   Encryption Algorithms
* Technology:  Java
* IDE:   NetBeans
* Description:   The Rotor Machine was used in the WW2 . By German Army to encrypts massages and it was called the negma. It is the type of dot product which includes about 26*26*26 substitution until it repeats it self again.It Contain 3 Celenders.Slow , medium and fast one .I have implemented the Rotor Machine using Java2 .

java code
package rotormachines;

import java.util.Scanner;

public class Ecyphir {

/** Creates a new instance of Ecyphir */
public Ecyphir()
{
Scanner myin=new Scanner(System.in);
System.out.println("**************** ******************");
for(int i=0;i<26;i++)
{
C1[i][0]=((i+14)%26)+1;
C1[i][1]=((i+6)%26)+1;
C2[i][0]=((i+10)%26)+1;
C2[i][1]=((i+1)%26)+1;
C3[i][0]=((i+4)%26)+1;
C3[i][1]=((i+12)%26)+1;
System.out.println((char)(i+65)+" "+C1[i][0]+" "+C1[i][1]+" | "+C2[i][0]+" "+C2[i][1]+" | "
+C3[i][0]+" "+C3[i][1]+" | "+(char)(i+65));
}
System.out.println("***************** **********************");
System.out.println((char)90);
System.out.println("Plz enter the plain text ");
PlainText=myin.nextLine().toUpperCase();
doEcyphir();
}
public static void main(String[] args) {
// TODO code application logic here
new Ecyphir();
}
public void doEcyphir()
{
int index=0;
int mapindex=0;
char ch;
for(int i=0;i<PlainText.length();i++)
{
index=PlainText.charAt(i);
index-=65;
mapindex=getIndexof(C1,index);
mapindex=getIndexof(C2,mapindex);
mapindex=getIndexof(C3,mapindex);
ch=(char)(mapindex+65);
if(Cyphir==null)
{
Cyphir=Character.toString(ch);
}
else
{
Cyphir+=Character.toString(ch);
}
fastrotor++;
fastcount++;
moveCyl2(C1);
if(fastcount%26==0)
{
fastcount=0;
moveCy1l(C1);
}
if(fastrotor%26==0)
{
moveCyl2(C2);

fastrotor=0;
mediumrotor++;
mediumcount++;
if(mediumcount%26==0)
{
mediumcount=0;
moveCy1l(C2);
}
if(mediumrotor%26==0)
{
moveCyl2(C3);
mediumrotor=0;
slowcount++;
if(slowcount%26==0)
{
slowcount=0;
moveCy1l(C3);
}

}

}

PrintTables();

//https://enigma.dev.java.net/
}
System.out.print("The Cyphir Text is :"+Cyphir);
}
public int getIndexof(int C[][],int row)
{
for(int i=0;i<26;i++)
{
if(C[row][0]==C[i][1])
return i;
}
return -1;
}
public void moveCy1l(int C[][])
{
int temp1=C[0][0];

for(int i=0;i<25;i++)
{
C[i][0]=(C[(i+1)%26][0]);

}
C[25][0]=temp1;


}
public void moveCyl2(int C[][])
{

int temp2=C[0][1];
for(int i=0;i<25;i++)
{

C[i][1]=((C[(i+1)%26][1]));
}

C[25][1]=temp2;

}
public void PrintTables()
{
System.out.println("*********************** ***************");
for(int i=0;i<26;i++)
{

System.out.println((char)(i+65)+" "+C1[i][0]+" "+C1[i][1]+" | "+C2[i][0]+" "+C2[i][1]+" | "
+C3[i][0]+" "+C3[i][1]+" | "+(char)(i+65));
}
System.out.println("*************** ***************");
}
private int C1[][]=new int[26][2];
private int C2[][]=new int[26][2];
private int C3[][]=new int[26][2];
private int fastrotor;
private int mediumrotor;
private int slowrotor;
private int fastcount;
private int mediumcount;
private int slowcount;
private String PlainText;
private String Cyphir;
}


java code
package rotormachines;

import java.util.Scanner;


public class Decyphir {

/** Creates a new instance of Ecyphir */
public Decyphir()
{
Scanner myin=new Scanner(System.in);
System.out.println("*****************************************************");
for(int i=0;i<26;i++)
{
C1[i][0]=((i+14)%26)+1;
C1[i][1]=((i+6)%26)+1;
C2[i][0]=((i+10)%26)+1;
C2[i][1]=((i+1)%26)+1;
C3[i][0]=((i+4)%26)+1;
C3[i][1]=((i+12)%26)+1;
System.out.println((char)(i+65)+" "+C1[i][0]+" "+C1[i][1]+" | "+C2[i][0]+" "+C2[i][1]+" | "
+C3[i][0]+" "+C3[i][1]+" | "+(char)(i+65));
}
System.out.println("*****************************************************");
System.out.println((char)90);
System.out.println("Plz enter the Cyphir text ");
Cyphir=myin.nextLine().toUpperCase();
Decyphir();
}
public static void main(String[] args) {
// TODO code application logic here
new Decyphir();
}
public void Decyphir()
{
int index=0;
int mapindex=0;
char ch;
for(int i=0;i<Cyphir.length();i++)
{
index=Cyphir.charAt(i);
index-=65;
mapindex=getIndexof(C3,index);
System.out.println(mapindex);
mapindex=getIndexof(C2,mapindex);
System.out.println(mapindex);
mapindex=getIndexof(C1,mapindex);
System.out.println(mapindex);
ch=(char)(mapindex+65);
if(PlainText==null)
{
PlainText=Character.toString(ch);
}
else
{
PlainText+=Character.toString(ch);
}
fastrotor++;
fastcount++;
moveCyl2(C1);
if(fastcount%26==0)
{
fastcount=0;
moveCy1l(C1);
}
if(fastrotor%26==0)
{
moveCyl2(C2);

fastrotor=0;
mediumrotor++;
mediumcount++;
if(mediumcount%26==0)
{
mediumcount=0;
moveCy1l(C2);
}
if(mediumrotor%26==0)
{
moveCyl2(C3);
mediumrotor=0;
slowcount++;
if(slowcount%26==0)
{
slowcount=0;
moveCy1l(C3);
}

}

}

PrintTables();

//https://enigma.dev.java.net/
}
System.out.print("The PlainText Text is :"+PlainText);
}
public int getIndexof(int C[][],int row)
{
for(int i=0;i<26;i++)
{
if(C[row][1]==C[i][0])
return i;
}
return -1;
}
public void moveCy1l(int C[][])
{
int temp1=C[0][0];

for(int i=0;i<25;i++)
{
C[i][0]=(C[(i+1)%26][0]);

}
C[25][0]=temp1;


}
public void moveCyl2(int C[][])
{

int temp2=C[0][1];
for(int i=0;i<25;i++)
{

C[i][1]=((C[(i+1)%26][1]));
}

C[25][1]=temp2;

}
public void PrintTables()
{
System.out.println("*****************************************************");
for(int i=0;i<26;i++)
{

System.out.println((char)(i+65)+" "+C1[i][0]+" "+C1[i][1]+" | "+C2[i][0]+" "+C2[i][1]+" | "
+C3[i][0]+" "+C3[i][1]+" | "+(char)(i+65));
}
System.out.println("*****************************************************");
}
private int C1[][]=new int[26][2];
private int C2[][]=new int[26][2];
private int C3[][]=new int[26][2];
private int fastrotor;
private int mediumrotor;
private int slowrotor;
private int fastcount;
private int mediumcount;
private int slowcount;
private String PlainText;
private String Cyphir;
}



Attachments
RotorMachines.rar
Rotor machine source code
(15.47 KiB) Downloaded 1521 times

Re: Rotor Machine Java

Mon Jan 21, 2013 1:01 am

updated.

Post a reply
  Related Posts  to : Rotor Machine Java
 machine learning java libraries     -  
 Rotar Machine     -  
 clients cannot connect to server on another machine     -  
 how to connect client on one machine with server     -  
 Support Vector Machine coding on Matlab...     -  
 Applet Dice Game Fruit Machine simulation     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 what is java     -  
 Using FTP in java     -  
 Java course     -  

Topic Tags

Java Algorithms