Created
August 6, 2016 03:58
-
-
Save vincent-paing/e1f168e087417a36f15c938cccd49e11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Created by Vincent on 23-Mar-15. | |
Copy paste this code and use it by running Engima.encrypt("YOUR_PLAIN_TEXT"); | |
*/ | |
public class Engima { | |
//set your Rotor Settings here | |
static String[] firstRotor = {"E", "K", "M", "F", "L", "G", "D", "Q", "V", "Z", "N", "T", "O", "W", "Y", "H", "X", "U", "S", "P", "A", "I", "B", "R", "C", "J"}; | |
static String[] secondRotor = {"A", "J", "D", "K", "S", "I", "R", "U", "X", "B", "L", "H", "W", "T", "M", "C", "Q", "G", "Z", "N", "P", "Y", "F", "V", "O", "E"}; | |
static String[] thirdRotor = {"B", "D", "F", "H", "J", "L", "C", "P", "R", "T", "X", "V", "Z", "N", "Y", "E", "I", "W", "G", "A", "K", "M", "U", "S", "Q", "O"}; | |
//Set the Reflector Settings | |
static String[][] reflector = { {"A","I"}, {"J","F"}, {"E","M"}, {"Z","X"}, {"W","O"}, {"S", "B"} }; | |
//Set Rotor for Reversing | |
static String[] firstRotorReverse = {"T", "A", "G", "B", "P", "C", "S", "D", "Q", "E", "U", "F", "V", "N", "Z", "H", "Y", "I", "X", "J", "W", "L", "R", "K", "O", "M"}; | |
static String[] secondRotorReverse = {"A", "J", "P", "C", "Z", "W", "R", "L", "F", "B", "D", "K", "O", "T", "Y", "U", "Q", "G", "E", "N", "H", "X", "M", "I", "V", "S"}; | |
static String[] thirdRotorReverse = {"P", "N", "F", "S", "R", "H", "Q", "J", "E", "T", "O", "M", "K", "W", "I", "Z", "V", "A", "U", "C", "Y", "X", "L", "B", "D", "G"}; | |
//Set the rotor positions here | |
static int firstRotorPosition = 13; | |
static int secondRotorPosition = 24; | |
static int thirdRotorPosition = 20; | |
/*@Param: plaintext = the plain text which you are converting */ | |
public static String encrypt(String plaintext) throws NullPointerException | |
{ | |
//Take the Plain Text | |
Scanner mScanner = new Scanner(System.in); | |
String plaintext = mScanner.nextLine(); | |
plaintext = plaintext.toUpperCase(); | |
//make sure there are no space and they are alphabet | |
for (int i = 0; i < plaintext.length(); i++) | |
{ | |
char c = plaintext.charAt(i); | |
if (!Character.isAlphabetic(c) && !Character.isWhitespace(c)) | |
{ | |
return null | |
} | |
} | |
//Encryption Process | |
for (int i = 0; i < plaintext.length(); i++) | |
{ | |
char c = plaintext.charAt(i); | |
if (Character.isWhitespace(c)) | |
{ | |
System.out.print("" + c); | |
} else { | |
char encryptedC; | |
c = Reflect(c); | |
int cValue = Character.getNumericValue(c); | |
cValue = upperASCtoIndex(cValue); | |
encryptedC = firstRotor[cValue].charAt(0); | |
cValue = upperASCtoIndex(Character.getNumericValue(encryptedC)); | |
encryptedC = secondRotor[cValue].charAt(0); | |
cValue = upperASCtoIndex(Character.getNumericValue(encryptedC)); | |
encryptedC = thirdRotor[cValue].charAt(0); | |
cValue = Character.getNumericValue(encryptedC); | |
cValue = upperASCtoIndex(cValue); | |
encryptedC = thirdRotorReverse[cValue].charAt(0); | |
cValue = upperASCtoIndex(Character.getNumericValue(encryptedC)); | |
encryptedC = secondRotorReverse[cValue].charAt(0); | |
cValue = upperASCtoIndex(Character.getNumericValue(encryptedC)); | |
encryptedC = firstRotorReverse[cValue].charAt(0); | |
encryptedC = Reflect(encryptedC); | |
//Change The Rotor Position | |
ChangeRotorPosition(); | |
return encryptedC; | |
} | |
} | |
} | |
//Change Rotor Position after every letter; | |
private static void ChangeRotorPosition() { | |
firstRotorPosition++; | |
firstRotor = SwapPositionbyOne(firstRotor); | |
firstRotorReverse = SwapPositionbyOne(firstRotorReverse); | |
if (firstRotorPosition > 26) | |
{ | |
secondRotorPosition++; | |
secondRotor = SwapPositionbyOne(secondRotor); | |
secondRotorReverse = SwapPositionbyOne(secondRotorReverse); | |
firstRotorPosition = 0; | |
if (secondRotorPosition > 26) | |
{ | |
thirdRotorPosition++; | |
thirdRotor = SwapPositionbyOne(thirdRotor); | |
thirdRotorReverse = SwapPositionbyOne(thirdRotorReverse); | |
secondRotorPosition = 0; | |
if (thirdRotorPosition > 26) thirdRotorPosition = 0; | |
} | |
} | |
} | |
//Swaping the Position in the arry | |
private static String[] SwapPositionbyOne(String[] array) { | |
for (int i =0; i < array.length; i++) | |
{ | |
int tempint = i+1; | |
if (i == array.length -1) tempint = 0; | |
String temp = array[i]; | |
array[i] = array[tempint]; | |
array[tempint] = temp; | |
} | |
return array; | |
} | |
//Changing ASC to Index Value; | |
public static int upperASCtoIndex(int i) | |
{ | |
return i - 10; | |
} | |
//Changing Index Value to ASCII | |
public static int upperIndextoASC(int i) | |
{ | |
return i + 'A'; | |
} | |
//Reflecting the letter | |
public static char Reflect(char c) | |
{ | |
for (int j = 0; j < reflector.length; j++) | |
{ | |
for (int k = 0; k < reflector[j].length; k++) | |
{ | |
char temp = reflector[j][k].charAt(0); | |
if (c == temp) | |
{ | |
switch (k) { | |
case 0: | |
c = reflector[j][1].charAt(0); | |
break; | |
case 1: | |
c = reflector[j][0].charAt(0); | |
break; | |
} | |
} | |
} | |
} | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment