Total members 11890 |It is currently Sat Apr 20, 2024 8:38 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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 .

This is the cipher of the Rotor Machine in Java .

java code
/*
* 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;
}


This is the decipher of the rotor machine in java
java code
/*
* 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;
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Rotar Machine
 Rotor Machine Java     -  
 machine learning java libraries     -  
 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     -  



Topic Tags

Java Algorithms






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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