Sat Jun 09, 2007 1:39 pm
/*
* Ecyphir.java
*
* Created on March 2, 2007, 5:29 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package rotormachines;
import java.util.Scanner;
/**
*
* @author mohamed
*/
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;
}
/*
* Decyphir.java
*
* Created on March 4, 2007, 9:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package rotormachines;
import java.util.Scanner;
/**
*
* @author mohamed
*/
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;
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.