Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ActualEnigma {
- private String newmessage = "This Was Made By Vladimir Alekseev";
- /*
- Sorry for any spaghetti code this was made in my sophomore year of high school, so feel free to modify it. Also feel free to view any other projects I made. Look at bottom to see what the methods do.
- */
- private String code;
- private boolean usePlug = false;
- private char[] letter;
- private int[] storedrotors = new int[3];
- private int[] rotors = new int[] {10,10,10};
- private char[] alphabet = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- private char[] plugboard = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- public ActualEnigma() {
- }
- //helper methods for plugboard
- private char replaceLetters(char c) {
- for(int i = 0; i < plugboard.length; i++) {
- if(c == alphabet[i]) {
- c = plugboard[i];
- return c;
- }
- }
- return c;
- }
- private char reversereplaceLetters(char c) {
- for(int i = 0; i < plugboard.length; i++) {
- if(c == plugboard[i]) {
- c = alphabet[i];
- return c;
- }
- }
- return c;
- }
- //encrypts message
- public String encrypt(String message) {
- newmessage = "";
- letter = new char[message.length()];
- rotors = new int[] {storedrotors[0], storedrotors[1], storedrotors[2]};
- for(int j =0; j < message.length(); j++) {
- if(rotors[2] - 1 >= 1) {
- rotors[2]--;
- }else if (rotors[1] - 1 >= 1) {
- rotors[1]--;
- rotors[2] = 26;
- }else if(rotors[0] - 1 >= 1) {
- rotors[2] = 26;
- rotors[1] = 26;
- rotors[0]--;
- }else {
- rotors[0] = 26;
- rotors[1] = 26;
- rotors[2] = 26;
- }
- }
- for(int i = 0; i < letter.length; i ++) {
- letter[i] = message.substring(i, i+1).charAt(0);
- if(letter[i] == ' ') {
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }else {
- letter[i] = replaceLetters(letter[i]);
- letter[i] += (rotors[0] + rotors[1] + rotors[2]);
- if(letter[i] > 90) {
- while(letter[i] > 90) {
- letter[i] -= 26;
- }
- }
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }
- letter[i] = replaceLetters(letter[i]);
- newmessage += letter[i];
- }
- String strg = "none";
- if(usePlug) {
- strg = "";
- for(int p = 0; p < plugboard.length; p++) {
- strg += plugboard[p];
- }
- }
- code = rotors [0] + " " + rotors [1] + " " + rotors [2];
- return "Encrypted Message: " + newmessage + "; Code: " + code + "; Plugboard: " + strg;
- }
- //decrypts message
- public String decrypt(String message, String key) {
- newmessage = "";
- letter = new char[message.length()];
- rotors = new int[] {(Integer.parseInt(key.substring(0, 2))),(Integer.parseInt(key.substring(3, 5))),(Integer.parseInt(key.substring(6, 8)))};
- for(int j =0; j < message.length(); j++) {
- if(rotors[2] - 1 >= 1) {
- rotors[2]--;
- }else if (rotors[1] - 1 >= 1) {
- rotors[1]--;
- rotors[2] = 26;
- }else if(rotors[0] - 1 >= 1) {
- rotors[2] = 26;
- rotors[1] = 26;
- rotors[0]--;
- }else {
- rotors[0] = 26;
- rotors[1] = 26;
- rotors[2] = 26;
- }
- }
- for(int i = 0; i < letter.length; i ++) {
- letter[i] = message.substring(i, i+1).charAt(0);
- letter[i] = reversereplaceLetters(letter[i]);
- if(letter[i] == ' ') {
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }else {
- letter[i] -= (rotors[0] + rotors[1] + rotors[2]);
- if(letter[i] < 65) {
- while(letter[i] < 65) {
- letter[i] += 26;
- }
- }
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }
- letter[i] = reversereplaceLetters(letter[i]);
- newmessage += letter[i];
- }
- code = rotors [0] + " " + rotors [1] + " " + rotors [2];
- return "Decrypted Message: " + newmessage;
- }
- //sets rotors to random numbers
- public void randomizeRotors() {
- rotors = new int[] {(int)(Math.random()*26) + 1,(int)(Math.random()*26) + 1,(int)(Math.random()*26) + 1};
- storedrotors = new int[] {rotors[0], rotors[1], rotors[2]};
- }
- //sets rotors
- public void setRotors(int i, int j, int k) {
- rotors = new int[] {i,j,k};
- storedrotors = new int[] {rotors[0], rotors[1], rotors[2]};
- }
- //sets plugboard either with (letter, letter to be replaced, true) or (scrambled alphabet, normal alphabet, false)
- public void setPlugboard(String c, String st, boolean ind) {
- usePlug = true;
- if(ind) {
- char lop = c.charAt(0);
- int i = lop -=65;
- System.out.println(i);
- char g = st.charAt(0);
- int l = g -= 65;
- System.out.println(l);
- plugboard[l] = plugboard[i];
- plugboard[i] = st.charAt(0);
- }else {
- for(int j = 0; j < st.length(); j++) {
- plugboard[j] = c.substring(j,j+1).charAt(0);
- }
- }
- }
- //prints possible codes based on portions of message (need to set plugboard beforehand
- public String guessCode(String message, String partofmessage) {
- newmessage = "";
- letter = new char[message.length()];
- rotors = new int[] {1,1,1};
- boolean b = false;
- int times = 0;
- int messages = 0;
- if(b) {
- return newmessage;
- }else {
- while(!b) {
- newmessage = "";
- if(times != 0) {
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }
- for(int i = 0; i < letter.length; i++) {
- letter[i] = message.substring(i, i+1).charAt(0);
- letter[i] = reversereplaceLetters(letter[i]);
- if(letter[i] == ' ') {
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }else {
- letter[i] -= (rotors[0] + rotors[1] + rotors[2]);
- if(letter[i] < 65) {
- while(letter[i] < 65) {
- letter[i] += 26;
- }
- }
- if(rotors[2] + 1 != 27) {
- rotors[2]++;
- }else if (rotors[1] + 1 != 27) {
- rotors[1]++;
- rotors[2] = 1;
- }else if(rotors[0] + 1 != 27) {
- rotors[2] = 1;
- rotors[1] = 1;
- rotors[0]++;
- }else {
- rotors[0] = 1;
- rotors[1] = 1;
- rotors[2] = 1;
- }
- }
- letter[i] = reversereplaceLetters(letter[i]);
- newmessage += letter[i];
- }
- if(newmessage.contains(partofmessage)) {
- System.out.print("Code: " + rotors [0] + " " + rotors [1] + " " + rotors [2]);
- System.out.println("; Message: " + newmessage);
- messages++;
- times++;
- }else {
- times++;
- }
- if(rotors[0] == 26 & rotors[1] == 26 & rotors[2] == 26) {
- b = true;
- }
- for(int j =0; j < message.length(); j++) {
- if(rotors[2] - 1 >= 1) {
- rotors[2]--;
- }else if (rotors[1] - 1 >= 1) {
- rotors[1]--;
- rotors[2] = 26;
- }else if(rotors[0] - 1 >= 1) {
- rotors[2] = 26;
- rotors[1] = 26;
- rotors[0]--;
- }else {
- rotors[0] = 26;
- rotors[1] = 26;
- rotors[2] = 26;
- }
- }
- }
- }
- if(messages == 0) {
- return "No Possible Messages or Codes Detected On This Plugboard Setting (either change plugboard or change possible part of message)";
- }else {
- System.out.println("");
- return messages + " Possible Messages and Codes Detected!";
- }
- }
- //End of Enigma Code
- /*
- To Encrypt message:
- First setup plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with
- .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
- Then setup rotors with .setRotors(int num1, int num2, int num3) or .randomizeRotors()
- Then encrypt message with encrypt(String message)
- [messages can only be allcaps and without any special characters, unless you want the message to not be decryptable]
- [EX. "THIS MESSAGE WITH WORK WITH THE ENIGMA MACHINE"]
- [EX. "THIS MeSSAGE will Not WORK WITH THE ENIGMA MACHINE!1!!!!"]
- To Decrypt message:
- First setup plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with
- .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
- Then decrypt message with encrypt(String message, String key)
- [key is string out rotors) (EX. "01 01 01"]
- To Guess Code:
- First setup you want to test plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
- Then use .guessCode(String message, String part_of_message)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement